Help using JOptionPane

please tell me how can i do following:
I want to add strings using JOptionPane.showInputDialog( ) method. in an ArrayList() of strings only.
I want to show input dialog box to user continously till user presses "enter" from keyboard.
please tell me the way to stop the loop when user presses "Enter" from keyboard.
//what i have done:
//created new ArrayList()
myStrings = new ArrayList();
while(1)
    String theString = JOptionPane.showInputDialog("Enter string ");
    myStrings.add(theString);
}

I want to show input dialog box to user continously
till user presses "enter" from keyboard.
please tell me the way to stop the loop when user
presses "Enter" from keyboard.You probably want to end the loop when the user clicks the cancel button on the dialog. Clicking the enter button invokes the button that has focus.

Similar Messages

  • Using JOptionPane to exit program & Using toString method

    Hello,
    In my program I have to use a toString method below
    public String toString(Hw1NCBEmployee[] e)
            String returnMe = String.format(getStoreName() + "\n" + getStoreAddress() + "\n" + getCityState() +  "\nYou have " + getEmpCount() + "employee(s)");
            return returnMe;
    }Now, at first, when I initially tested my code, it was working just fine, printing out the right way
    Point is -- now that Im practically finished with my program, and am going back and running it - calling the toString method gives me the address rather than the information . . . Can anyone help me with this issue??
    Also, Im using JOptionPane to display everything, but -- at first, it worked fine, then (around the same time I started having the above problem) it stopped exiting the do-while loop:
    while(go)
                 int option = JOptionPane.showConfirmDialog(null, "Do you want to enter employee information?", "Welcome!", JOptionPane.YES_NO_OPTION);
                     if(option == JOptionPane.NO_OPTION)
                        go = false;  //exit loop
            

    ncjb wrote:
    well, for the toString - I have to print out the info inside the array -- am I right in thinking that the only way to do this is to pass in the array ??If the array is part of the class then there is no need to pass it as a parameter. If it is being passed in from outside the class then I have to question your design. Why should class X being formatting data that is not a representation of itself?
    and i have 2 other files that go with this program . . . I just want to know if there is a way to make sure that when the user presses the NO option, it will exit the loop -- the piece of code you see if apart of an entire method (not long) -- do you want me to include this??You read that link I provided REAL quick!

  • Help with JOptionPane.showInputDialog

    Hi!!
    People here helped me before so trying my luck again:)
    In order to get information from people I use JOptionPane.showInputdialog,
    problem is if I need to get 2 different types of info I have to use
            naam = JOptionPane.showInputDialog(null, "Geef speler naam:");
            land = JOptionPane.showInputDialog(null, "Geef Land Speler:");Is there anyway to make this happen in 1 screen?
    Or do I have to make a new frame for that with textfields and such?
    if so, anyone got an example?

    im new here:) so.. New to Java Technology suits me
    well I guess.No it doesn't. New to Java forum is for classpath questions, simple compile time problems and lazy students to cross post their assigments into.
    Swing questions, such as the one you asked, should be asked in the Swing forum because the people who answer questions in there are more knowledgeable about Swing. Plus you will almost 100% likely to find working example codes from previous posts in the Swing forum for Swing related questions.
    Much more likely than in here anyway.

  • Using JOptionPane in a non-event dispatching thread

    I am currently working on a screen that will need to called and displayed from a non-event dispatching thread. This dialog will also capture a user's selection and then use that information for later processing. Therefore, the dialog will need to halt the current thread until the user responds.
    To start, I tried using the JOptionPane.showInputDialog() method and set the options to a couple of objects. JOptionPane worked as advertised. It placed the objects into a combobox and allowed the user to select one of them. However, it also gave me something unexpected. It actually paused the non-event handling thread until the user had made a selection. This was done without the JOptionPane code being wrapped in a SwingUtilities.invokeLater() or some other device that would force it to be launched from an Event Handling Thread.
    Encouraged, I proceeded to use JOptionPane.showInputDialog() substituting the "message" Object with a fairly complex panel (JRadioButtons, JLists, JTextFields, etc.) that would display all of the variety of choices for the user. Now, when the screen pops up, it doesn't paint correctly (like what commonly occurs when try to perform a GUI update from a non-event dispatching thread) and the original thread continues without waiting for a user selection (which could be made if the screen painted correctly).
    So, here come the questions:
    1) Is what I am trying to do possible? Is it possible to create a fairly complex GUI panel and use it in one of the static JOptionPane.showXXXDialog() methods?
    2) If I can't use JOptionPane for this, should I be able to use JDialog to perform the same type function?
    3) If I can use JDialog or JFrame and get the threading done properly, how do I get the original thread to wait for the user to make a selection before proceeding?
    I have been working with Java and Swing for about 7-8 years and have done some fairly complex GUIs, but for some reason, I am stumped here. Any help would be appreciated.
    Thanks.

    This seems wierd that it is functioning this way. Generally, this is what is supposed to happen. If you call a JOptionPane.show... from the Event Dispatch Thread, the JOptionPane will actually begin processing events that normally the Event Dispatch Thread would handle. It does this until the user selects on options.
    If you call it from another thread, it uses the standard wait and notify.
    It could be that you are using showInputDialog for this, try showMessageDialog and see if that works. I believe showInputDialog is generally used for a single JTextField and JLabel.

  • Deleting a MS Access database table record using JOptionPane

    hi!
    just want some help here..
    is there a way to delete a MS Access database record through using JOptionPane?? i've gone through some, probably alot of examples on SELECT, INSERT, UPDATE, DELETE statements but none came up to the stuff i need.
    so far i've seen the DELETE statement like this and also this is a common DELETE statement..
    statement.executeUpdate("DELETE FROM tableName WHERE fieldName01 = 'blablabla' AND fieldName02 = 'etcetcetc'");like INSERT, there's a coding somewhat like this:
    String asd = JOptionPane.showInputDialog(null, "Enter blablabla stuff");
    statement.executeUpdate("INSERT INTO whatEverTableName (tableFieldName) VALUES('"+asd.getText()+"')");so, is it possible using the same method for DELETE??
    help and advices are appreciated in advanced..

    Here's how I did it. Research does help, but sometimes looking at others code does too... You do have to have a dummy file that you made with access though. You can't just make a file file.mdb (it will be corrupt)
         public void createDatabase(String database) throws SQLException{
              try{
                   // This file needs to have been created with MS Access
                   File dbfile = new File(this.dataBaseDir + "dummy.mdb");
                   // This is the new database file being made
                   File newFile = new File(this.dataBaseDir + database + ".mdb");
                   // Copy all bytes from dummy file to new DB file.
                   FileInputStream instream = new FileInputStream(dbfile);
                   FileOutputStream ostream = new FileOutputStream(newFile);
                   int numBytes = instream.available();
                   byte inBytes[] = new byte[numBytes];
                   instream.read(inBytes, 0, numBytes);
                   ostream.write(inBytes, 0, numBytes);
              catch(FileNotFoundException e) { e.printStackTrace();}
              catch(IOException e) { e.printStackTrace();}
              if(DEBUG) System.out.println("creating the " + database + " database");
         }

  • Comple errors using JOptionPane

    I'm trying to write a Java program using JOptionPane dialog/display boxes that reads an integer and checks whether it is even using true/false statements. I also want the program must check if the number the user entered is between 1 and 1000.
    I haven't found alot of help in the book I have and i've found some help in another forum but it doesn't seem to compile well, I get the following errors:
    IntegerRead.java:39: cannot find symbol
    symbol : method valueOf(java.lang.String)
    location: class Integer
    int number = Integer.valueOf(s);
    ^
    Integer.java:13: cannot find symbol
    symbol : class Scanner
    location: class Integer
    Scanner input = new Scanner( System.in );
    ^
    Integer.java:13: cannot find symbol
    symbol : class Scanner
    location: class Integer
    Scanner input = new Scanner( System.in );
    Here is the code:
    // Assignment 2.1: IntegerRead.java
    // This program reads an integer, checks if it's even and is between 1 and 1000.
    import javax.swing.JOptionPane; // program uses class JOptionPane
    public class IntegerRead
    public static boolean isNumber(String args)
    if(args.length() > 3)
    throw new IllegalArgumentException("Invalid argument");
    else
    boolean isNumber = false;
    for(int i = 0; i < args.length(); i++)
    char c = args.charAt(i);
    if(Character.isDigit(c))
    isNumber = true;
    else
    isNumber = false;
    return isNumber;
    public static boolean isEven(int aNumber)
    int a = aNumber & 1;
    return (a == 0);
    public static void main(String args[])
    String s = JOptionPane.showInputDialog("Please enter number between 0 to 999");
    System.out.println("Is Number " + isNumber(s));
    if(isNumber(s))
    int number = Integer.valueOf(s);
    System.out.println("Number " + number + "is Even " + isEven(number));
    for(int i = 0; i < 999; i++)
    System.out.println(i + " is " + isEven(i));
    I know i'm missing very simple. I'm new and have tried to build upon what I have so far, but I can't figure it out.
    Thanks....
    Edited by: Scott.Dishman on Jul 9, 2008 4:07 PM

    You have a class named Integer somewhere on your class path. Get rid of it or rename it. If you've already renamed your Integer.java file, you still need to delete the Integer.class file. You should never create your own class that has the same name as a class in the standard java.lang package.

  • Reading Integer using JOptionPane

    I'm trying to write a Java program using JOptionPane dialog/display boxes that reads an integer and checks whether it is even using true/false statements. I also want the program must check if the number the user entered is between 1 and 1000.
    I haven't found alot of help in the book I have, so I'm hoping somone has some example modules.
    Thank you....
    Scott

    Hi,
    Hope this helps you a bit. Just Uncomment the for your even number testing.
    import javax.swing.JOptionPane;
    public class OptionPaneTest {
         public static boolean isNumber(String args)
              if(args.length() > 3)
                   throw new IllegalArgumentException("Invalid argument");
              else
              boolean isNumber = false;
              for(int i = 0; i < args.length(); i++)
                   char c = args.charAt(i);
                   if(Character.isDigit(c))
                        isNumber = true;
                   else
                      isNumber = false;
              return isNumber;
         public static boolean isEven(int aNumber)
              int a = aNumber & 1;
              return (a == 0);          
          * @param args
         public static void main(String[] args) {          
              String s = JOptionPane.showInputDialog("Please enter number between 0 to 999");
              System.out.println("Is Number " + isNumber(s));
              if(isNumber(s))
              int number = Integer.valueOf(s);
              System.out.println("Number " + number + "is Even " + isEven(number));          
    //          for(int i = 0; i < 999; i++)
    //          System.out.println(i + " is " + isEven(i));
    }

  • Using JoptionPane.ShowInternalDialog

    I'm using the following code to open an JInternalFrame as model from another JinternalFrame.
    partNumberMaintenanceFrame = new PartNumberMaintenance_4_1 (this,null,data);
    JPanel glass = new JPanel();
    glass.setOpaque(false);
    glass.setLayout(null);
    glass.add(partNumberMaintenanceFrame);
    mainDesktopFrame.setGlassPane(glass);
    glass.setVisible(true);
    partNumberMaintenanceFrame.setVisible(true);
    Now, the problem is , When i try to use JoptionPane.ShowInternalDialog from the partNumberMaintenanceFrame the messagedialog goes behind the frame.
    I guess this is bcoz i use glasspane. What shoup i do to make the dialog to show on the frame?
    Thanks

    You might need to post a little more code for people to help you.

  • Help using jar file!

    Help using jar file!
    Hello
    I have created a jar file by using
    jar cvmf Manifest.txt myjar.jar 1.class Mydirectory
    In 1.java file :I have used
    JEditorPane editor;
    editor.setPage(getClass().getResource("/Mydirectory/default.htm"));
    If I am only giving myjar.jar file to the client without Mydirectory then also it is working fine by showing default.htm in JeditorPane.
    But my problem is I want to use
    Runtime.getRuntime().exec("cmd /c start IEXPLORE"+targetstr) ;
    targetstr will be the filename with path of the default.htm
    If I am giving myjar.jar file with Mydirectory to the client it is working but I don't want to give Mydirectory to the client .
    What should I do?
    Is there any solution for this?
    Using another jar file which will include mydirectory can solve this problem?
    Is there any othe concept in java so that I wll be able to hide Mydirectory from client?
    Please help.

    It seems like you could extract the .htm file from the jar, either with Runtime.exec or using the Jar API classes.

  • Help using file sharing with different users accounts on same Macbook

    Hi, I just wanted a little help using the file sharing option. My wife and I had different users accounts on the same MacBook, but when I am trying to share my baby's picture from my documents folder for my wife to have the same picture, I can't find the way to do that; I am not been able to find my pictures on my wife's account. Is there any way that I could share a folder from my account and my wife to have the same files at the same time on her account?
    I mean, something like the smart play list on the itunes?
    Thank you

    You can't do that directly unless you change permissions on your whole Documents folder which is not a good idea. Your wife can see your Documents folder from her account by going to Users/yourusername/Documents. However, this folder has no read privileges for others except yourself by default so she won't be able to open it.
    rather than changing permissions on your Documents folder, you can place the pictures you want to share in Public folder in your home directory or in Users/Shared folder.

  • How to assign search help using ovs for select options for ALV in web dynpr

    how to assign search help using ovs for select options for ALV in web dynpro

    Hi,
    refer http://wiki.sdn.sap.com/wiki/display/WDABAP/InputhelpofObjectValueSelectioninWDABAP
    http://www.****************/Tutorials/WebDynproABAP/OVS/page1.htm
    and http://wiki.sdn.sap.com/wiki/display/Snippets/WebDynproAbap-OVSsearch+help
    Thanks,
    Chandra

  • How to create Search Help using more than 1 table

    Hi all,
    I need to create a search help using more than 1 table.
    Eq:-   Itable1 contains the data and Table2 contains the description of a field.
    In my search help i require A field from Table1 and For the corresponding field description from Table2.

    Hi,
    You can do this with the help of collective search help.
    Collective search helps:- Combination of elementary search helps. When we need to fetch data based on multiple selection criteriau2019s. More than one tables are Selection from multiple tables 
    Steps for creating collective search help.
    1) Enter the search help name and click on create.
    2) Choose Collective search help radio button option as the search help type.
    3) Enter the search help parameters.
    Note that there is no selection method to be entered for a collective search help.
    4) Instead of the selection method, we enter the included search helps for the collective search help.
    5)We need to assign parameters for each of the included search helps.
    6) Complete the parameter assignment by clicking on the push button.
    7) Collective search help offers the user to obtain F4 help using any of the included search helps.
    Hope this will help you:
    Reagrds:
    Alok

  • How to use JOptionPane in jsp, instead of javascript message alert box?

    HI,
    How to use JOptionPane in jsp,
    instead of javascript "message alert box"?
    I hate javascript,
    I'd like to only use java in jsp. don't use javascript.
    javascript is client side,
    jsp is server side. i know that.
    how to... instead of javascript box?
    how to use ... message box in webpage?
    don't use applet,,,, don't use javascript,,,
    hm...zzzZzz
    I hate javascript..T.T
    <SCRIPT language=JavaScript>
    alert("hate javascript");
    </SCRIPT>
    ===>>>>
    In this way,,
    JOptionPane.showOptionDialog(null,"I love java")
    I'd like to only use jsp and java and html...in webpage.
    don't use javascript....
    Why? don't sun provide message box in jsp, instead of javascrip box?
    Why?
    Edited by: seong-ki on Nov 4, 2007 8:38 PM

    Drugs are bad, m'kay?

  • Trying to print a list using JOptionPane

    I am very new to Java and programming, so please forgive me if this should be apparent. I wrote a program for school which was for ordering pizzas. I wrote it as a console program and after much frustration it finally worked. Now I am trying to change it to a GUI program using JOptionPane. I was hoping that since I already had the logic written and working, I could focus on the GUI aspect. I was wrong. I want to print a list of enums on a JOptionPane and cannot figure out how to do it My code in the console program was:
    System.out.println("The available crust styles are: ");
         for(Pizza.Style style : Pizza.Style.values())
              System.out.println(style+" ");
    So how can I iterate through the for loop to list all of the styles in the JOptionPane? Do I need to turn it into a method with a return and then cal the method like
    JOptionPane.showMessageDialog(null, crustMethod()); ??

    OP said:
    ... Now I am trying to change it to a GUI program using JOptionPane. I was hoping that since I already had the logic written and working, I could focus on the GUI aspect. I was wrong. ...I wouldn't use JOptionPane. You need JFrame and maybe, but I doubt it, a JDialog. OP, the GUI is more than likely to be nothing more than a form that allows users to set certain properties or fields in a Java class. For example let's say we have this class.
    public class Point
      public int x;
      public int y;
    }Then a GUI for this would be something like a JPanel with two (2) JLabels and two (2) JTextFields. Maybe something like so...
    import java.awt.*;
    import javax.swing.*;
    public class PointGUI extends JPanel
      public JLabel xLabel = new JLabel("Coordinate X:");
      public JLabel yLabel = new JLabel("Coordinate Y:");
      public JTextField xField = new JTextField(4);
      public JTextField yField = new JTextField(4);
      public PointGUI(){
        setLayout(new GridLayout(2, 2));
        add(xLabel);
        add(xField);
        add(yLabel);
        add(yField);
    }Then to run the program...
    import javax.swing.*;
    public class Test
      public static void main(String[] args){
        SwingUtitlities.invokeLater(new Runnable(){
          public void run(){
            PointGUI gui = new PointGUI();
            JFrame f = new JFrame("PointGUI");
            f.getContentPane().add(gui, "Center");
            f.pack();
            f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
            f.setVisible(true);
    }... actually all the program does is display the GUI. But, can you see that the GUI's JLabels and JTextFields describe all the public properties of the Point class? Can you see that the entire GUI can be placed anywhere in a JFrame, or maybe even in a JDialog? From this point on, you can add other functions and features to your classes, GUI's and the main apps that run them.
    A hint on your project, you may need to write a Pizza class that decribes all the things a single pizza can be - then come up with a GUI for that and an app that runs it.

  • Using JOptionPane.showInputDialog to return an integer

    I am currently using JOptionPane to return a string from the Object array i made with all possible choices.
         private static boolean determineService(){
              JLabel label;
              label=new JLabel("Please select which Service Model to use.",JLabel.CENTER);
              Object[] possibilities = {"AddressCleanse", "VIN", "PostalCode"};
              serviceModel = (String)JOptionPane.showInputDialog(
                                  null,
                                  label,
                                  "Select Service",
                                  JOptionPane.PLAIN_MESSAGE,null,
                                  possibilities,
                                  "AddressCleanse");
    //          If a string was returned, say so.
              if ((serviceModel != null) && (serviceModel.length() > 0)) {
                  System.out.println("You've selected " + serviceModel + "!");
                  setVariables();
                  return true;
              }else{
    //               If you're here, the return value was null/empty.
                   System.out.println("Null or Bad service selected!\n Exiting program.");
                   return false;
              }I would like to instead return the response as an integr so I can then use a switch, rather than recursive if, else if logic.
    Apparently java doesnt allow switches to use strings (like PHP does)
    any ideas?

    Just a question more than an answer: would using a hashmap ever work here? where the key is a string and the value is an (???) object that implements an interface that would allow you to call an appropriate method? Perhaps one could use the keySet() and toArray to build the Object array for the JOptionPane. I thought I read something about this somewhere, but am not sure....

Maybe you are looking for

  • Removing spaces in cross tab

    Hello...               I created one cross tab with 10 rows and 2 columns.(In Crosstab Expert) Based on grouping  i got 5 set of cross tab reports,Each cross tab contains 4 records .But in between these crosstabs blank spaces are there ,How can i avo

  • Is there a limit to Number of Tracks in DVDSP

    I have a client who want to put package 80 seperate interviews. Is there a limit to the number of video tracks that DVDSP can handle? Help appreciated

  • How to ban extracting from .jar file?

    Hello Problem of .jar file is: .class files can be extracted (with winrar ...) and decompiled. I don't want my classes to be decompiled. But I still need my jar executable of course. Is some solution which bans extraction without any impact on execut

  • How to change password in the default realm programtically?

    Hello, Does any one have experience in how to change password in the default realm programtically in WebLogic 8.1? Thanks. Bimal Patel

  • Ghost or Raid Drive ???

    i want to set up a couple of drives to operate as one, mainly for backup and safety purposes. how can i do this? i know on the pc side of things there is an application called ghost. is there a similar thing on the mac side? az