JOptionPane.showInputDialog Help!!!!

Hello,
I'm using JOptionPane.showInputDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon,
Object[] selectionValues,
Object initialSelectionValue ).
which prompts the user to add some text and the same will be stored for later retrieval.
In this case, JOptionPane pops up with a label, textfield,OK & Cancel buttons.
Default focus is given to to the TextField.
Is there anyway to get access to the TextField via code ?
For example, if the user types "Hello World" in the given TextField, then I need to get the Text entered in
the TextField in the following manner.
String myStr = myJOptionPaneTextField().getText();
System.out.println("Entered Text is "+ myStr);
How can I Achieve the same ?
Thanks in advance
Regards
vargav

Why? At what point do you know you should manipulate the text? Usually, it would be when the user clicks OK, at which point it doesn't matter because the dialog is dismissed. So what kind of operations do you expect to be able to do?
Aside from that, I'm not sure how to get the text field in the option pane, per se... It can be gotten by digging thru the pane's components til you find a text field. But you'd need to do that in a separate thread. So it's not really feasible to do that.... There is an alternative:
The message is an object, and it can be an array of objects, which will all be added, and thus can include a message string and a text field...
JTextField myfield = new JTextField();
Object[] obj = new Object[2];
obj[0] = "Enter some text here:";
obj[1] = myfield;
if(JOptionPane.showOptionDialog(obj, "Enter Text", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
   String value = myfield.getText();
}Then you can do whatever you want with the text field... but the same problem is there, you have lost the dialog before you can do anything.

Similar Messages

  • 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.

  • HELP --JOPtionPane.showInputDialog

    I was developing a employee information system in swing.
    On the way of development,the major problem the is occuring
    is that two JOPtionPane.showInput Dialog boxes are appearing
    at a time.When I fetching the data to the JOPtionPane.showInput Dialog box and then clicking the "ok" option button,another JOPtionPane.showInput Dialog box appears making the program difficult to
    bug.
    Please suggest ways to solve this bug.

    It is more likely that your program has logical errors, just search for all JOptionPane.showInputDialog(...) statements and insert a println statement before it to trace the source of the problem.
    Visual Paradigm - http://visual-paradigm.com/

  • JOptionPane.showInputDialog and Focus

    I have a program that allows the user to type in a String in a text box and when they hit the enter key the text will appear in a text area. I've also added a menu bar to the program. When the user clicks on File and then Connect from the menu bar I want a JOptionPane to pop up that asks for the user to type in a username and then prompt for a password. And I've gotten all of that to work. Now for the problem. I click on file from the menu bar and then connect and it prompts me for the username as it should. I am able to just type in a phrase in the text field of the username dialog box and hit the enter key and then it prompts me for my password. I am still able to type in a phrase in the text field of the password Dialog box, however when I hit the enter key, the Dialog box does not close as it did for the username Dialog box. It will still close if I click on the OK button, but just pressing the enter key does not close the Dialog box as it did when it prompted for a username. I'm thinking I need to direct the focus to the password dialog box, but not sure how to go about doing this. Any help in solving this problem would be greatly appreciated.
    Here is my code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class TestJOptionPane extends JFrame implements ActionListener{
         private static int borderwidth = 570;
         private static int borderheight = 500;
         private JPanel p1 = new JPanel();
         private JMenu m1 = new JMenu("File");
         private JMenuBar mb1 = new JMenuBar();
         private JTextArea ta1 = new JTextArea("");
         private JTextField tf1 =new JTextField();
         private Container pane;
         private static String s1, username, password;
         private JMenuItem [] mia1 = {new JMenuItem("Connect"),new JMenuItem("DisConnect"),
              new JMenuItem("New..."), new JMenuItem ("Open..."), new JMenuItem ("Save"),
              new JMenuItem ("Save As..."), new JMenuItem ("Exit")};
    public TestJOptionPane (){
         pane = this.getContentPane();
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
         dispose();
    System.exit(0);
         setJMenuBar(mb1);
    mb1.add(m1);
    for(int j=0; j<mia1.length; j++){
         m1.add(mia1[j]);
    p1.setLayout( new BorderLayout(0,0));
    setSize(borderwidth, borderheight);
    pane.add(p1);
              p1.add(tf1, BorderLayout.NORTH);
              p1.add(ta1, BorderLayout.CENTER);
              this.show();
              tf1.addActionListener(this);
              for(int j=0; j<mia1.length; j++){
                   mia1[j].addActionListener(this);
         public void actionPerformed(ActionEvent e){
              Object source = e.getSource();
              if(source.equals(mia1 [0])){
                   username = JOptionPane.showInputDialog(pane, "Username");
                   password = JOptionPane.showInputDialog(pane, "Password");
              if(source.equals(tf1)){
                   s1=tf1.getText();
                   ta1.append(s1+"\n");
                   s1="";
                   tf1.setText("");
         public static void main(String args[]){
              TestJOptionPane test= new TestJOptionPane();
    }

    But using JOptionPane doesn't get the focus when you call itworks ok like this
    import javax.swing.*;
    import java.awt.event.*;
    class Testing
      public Testing()
        final JPasswordField pwd = new JPasswordField(10);
        ActionListener al = new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            pwd.requestFocusInWindow();}};
        javax.swing.Timer timer = new javax.swing.Timer(250,al);
        timer.setRepeats(false);
        timer.start();
        int action = JOptionPane.showConfirmDialog(null, pwd,"Enter Password",JOptionPane.OK_CANCEL_OPTION);
        if(action < 0)JOptionPane.showMessageDialog(null,"Cancel, X or escape key selected");
        else JOptionPane.showMessageDialog(null,"Your password is "+new String(pwd.getPassword()));
        System.exit(0);
      public static void main(String args[]){new Testing();}
    }

  • JOptionPane.showInputDialog error checking?

    menu = JOptionPane.showInputDialog("What would you like to do next: \n" +
    "1: Enter another person (" + (data.length - index - 1) + " entries left)\n" +
    "2: Display data sorted by First Name\n" +
    "3: Display data sorted by Last Name\n" +
    "4: Display data sorted by Birth Date\n" +
    "5: Search data by First Name\n" +
    "6: Search data by Last Name\n" +
    "7: Search data by Birth Date\n\n" +
    "0: Exit");How can I error check that to make only those values will be entered. I thought I would be able to say something like, if(menu != "0") and so on, but that doesn't seem to work, neither does if(menu == null). Thanks in advance for any help, I am probably just doing something stupid.

    You will get more chance on a suitable answer if you post this Swing related question in the Swing forum: http://forum.java.sun.com/forum.jspa?forumID=57

  • Custom icon for JOptionPane.showInputDialog

    As a novice who's wading through Deitel's 'Java how to program', using java2 and j2sdk1.4.1 on JCreator Pro. I want to use my own icons on a small proggie i've written. I know that the format is:
    showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)
    however this doesn't tell me how to write the code. how do i change the default '?' icon for my own icon?
    thanks for any help
    rgds
    adambi

    This should help you also
                ImageIcon ico = new ImageIcon("drag2.jpeg");
                Frame frame = new Frame();
                Object[] options = {"Yes, please",
                        "No, thanks",
                        "No eggs, no ham!"};
              JOptionPane.showInputDialog(frame,
              "What some eggs ?.",
              "My title",JOptionPane.ERROR_MESSAGE,ico,options,
        options[2]);also here is a good link for ya on dialogs
    http://java.sun.com/j2se/1.4/docs/api/
    GOOD LUCK

  • JOptionPane.showInputDialog cant see displayed message

    I need some help.
    In my program i try to display a JOptionPane.showInputDialog(f,"Save file as ","save as",JOptionPane.QUESTION_MESSAGE);
    The JOptionPane displays but I can't see the message text and the textfield does not display until I write something in the window?
    Whats wrong, How can I fix it?
    Please help me!

    Well I tried it out, and it works on my machine.
    Try compiling this:
    import javax.swing.*;
    public class JForumTester
    extends Object
         public static void main(String[] args)
              JOptionPane.showInputDialog(null,"Save file as ","save as",JOptionPane.QUESTION_MESSAGE);
              System.exit(0);
    to see if it's the dialog that's the problem or something else.
    //Java Rules!
    --RXon
    PS; if you e-mail me, be sure to put the word "JDNForum" at the end, so it doesn't get caught in my spam guard;

  • Strange problem with JOptionPane.showInputDialog

    I have a strange problem with JOptionPane.showInputDialog in that when I run a program anything that is typed in is displayed backwards, for example: I type this character > and this < is what is displayed.
    I've uninstalled the sdk and reinstalled it twice but that hasn't solved my problem and the java plug-in in the control panel is displaying the same symptoms.
    I did a search before posting this but all I got really was posts from people who wanted to reverse their text.
    Thanks for your help on this.

    Is it just the < and > characters?

  • JOptionPane.showInputDialog

    Hi,
    I have a question about JOptionPane.showInputDialog(...), there is a method that accepts an object array for its parameter, when the array size exceeds a certain bound the final dialog will contains a JList in it. When I click the item of the JList then click cancel the null will be returned, but when double clicking item and then cancel the item will be returned. Partial of my code is:
    String[] available = toSortedStringArray(mapColumn.keys());
    if (available.length == 0)
         return;
    Object selected =
         JOptionPane.showInputDialog(
              FilterControl.this,
              "Select Column",
    JOptionPane.QUESTION_MESSAGE,
              null,
              available,
              null);
    if (selected != null)
    When the dialog shows, double click one of the items in the JList and then click cancel, the selected will be the item, that's what we unexpected, the null returned will be expected.
    Who can help giving a hint about how to solve this problem?
    PS: One of the solutions is don't call this static method but write our own code or to create a class to extend the JOptionPane, but I think it is ineffective.

    It's a bug:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6428694

  • JOptionPane.showMessageDialog() help

    I am trying to do a search on names inwhich i input into a file and then show them on a joptionpane. My problem is that I can't get it to run b/c it says that i have incompatable types. Please help if at all possible.
    String name, custid, num, result;
    int id, element;
    public void search(){
    num = JOptionPane.showInputDialog("Enter Customer ID:");
    id = Integer.parseInt(num);
    name = tree.lookUp(id);
    result = JOptionPane.showMessageDialog(null,new JTextArea(name));
    }

    result = JOptionPane.showMessageDialog(null,new JTextArea(name));your wrong if the JOptionPane.showMessageDialog()'return type is void,it means it hasn't a return value and you want to put a value to "result",you should use like this
    JOptionPane.showMessageDialog(null,new JTextArea(name));

  • Question about JOptionPane.showInputDialog

    How do I know when the CANCEL button is pressed in response to JOptionOptionPane.showInputDialog?
    Thanks for the help!

    Try this
    import javax.swing.JOptionPane;
    class TestJOptionPane
      public TestJOptionPane()
        String response = JOptionPane.showInputDialog("enter something");
        if(response == null) JOptionPane.showMessageDialog(null,"cancel/escape or 'x' selected");
        else if(response.equals("")) JOptionPane.showMessageDialog(null,"OK button, but nothing entered");
        else JOptionPane.showMessageDialog(null,"OK button, you entered "+response);
        System.exit(0);
      public static void main(String args[]){new TestJOptionPane();}
    }

  • JOptionPane.showInputDialog cancel brings up a blank box

    String name = JOptionPane.showInputDialog(
        null,
        "My message",
        "My title",
        null,
        null,
        null)  // real list of options deleted for brevityThis works okay, but when I press the cancel button, I get a blank warning JOptionPane (message box thingy)... how do I tailor the message in that? Been looking through the search results and API with little luck.

    handle it like so
    try
        String s = new JOptionPane.showInputDialog(null, "Enter input");
    catch(Exception ee)
        JOptionPane.showMessageDialog("Invalid input");
    }

  • Setting focus in JOptionPane.showInputDialog

    Hi!
    For usability I want to set the focus to the dialog that pops up if i call JOptionPane.showInputDialog(...).
    Someone an idea how this works?
    cu

    Hi...
    I am facing exactly the same problem... If you have found a solution, please contact me at:
    [email protected]
    Regards.

  • 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....

  • Catch user entry error in JOptionPane.showInputDialog()

    i just want to know if the user entered a String instead of a int...
    i tryed instanceof but didnt worked..
    m_sQuantite = JOptionPane.showInputDialog("How much do you need : " + itmSelect.toString());

    what r u about to ask, what the statement returns.

Maybe you are looking for

  • How Can I Prevent Emailed PDF (created in Acrobat)From Opening in Preview?

    When I email my PDF page, if it is opened in another default viewer ('Preview', etc..) it opens up at the wrong size, (it is designed to open full screen and does not in 'Preview"), and the hot links do not work and are cold.  Is this due to people h

  • How to use a SelectAll box / checkbox to select multiple checkboxes!!

    I have a question regarding checkboxes. My jsp page has the following layout generated dynamically based on a drop down selection box. ID State Initiated Released Locked US Initiated X Arg Released X X Ger Released X X Ita Locked X X X The Initated,

  • "Milky" or faint results when exporting from AE to FCP

    (Accidently posted to FCP Express forum-- whoops!) I've been using After Effects for years now to create graphics for our Avid projects, and the QT results have always been great. Clean, sharp moving graphics. But now that I've been using FCP, I'm no

  • Design view broken in CS5

    In the last week or so, Design view in CS5 has become completely broken for me. I usually use Code View, but sometimes it's handy to switch to Design view or Split view, which are now always grayed out. I had installed the HTML 5 pack, not sure if th

  • Omit one field in maintance view

    HI I Have table with 4 fields and i want to create maintenance view but just for 3 fields on the maintenance view how i can do that ? Regards Joy