Solved

getting user input from custom JOptionPane

Posted on 2004-09-29
26
983 Views
Last Modified: 2007-12-19
since i wanted different texts for input dialog i've created a custom option pane

JButton button[] = new JButton[2];
        btnOk = new JButton(ok);
        btnCancel = new JButton(cancel);
       
        button[0] = btnOk;
        button[1] = btnCancel;
       
        btnOk.addActionListener(this);
        btnCancel.addActionListener(this);
       
        jOptionPane = new JOptionPane(insert,JOptionPane.QUESTION_MESSAGE,    JOptionPane.OK_CANCEL_OPTION, null, button);
        jOptionPane.setWantsInput(true);
       
        inputDialog = jOptionPane.createDialog(this, dialogTitle);

Now how do i get the use input text. getInputValue() returns uninitialized value.
0
Comment
Question by:sharath_kulal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
  • +3
26 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12177352
Instead of using JOptionPane, use JDialog, it will give you more flexibilty
0
 
LVL 35

Expert Comment

by:girionis
ID: 12177356
What is the methdo that returns the input? That one you should use to get it.
0
 
LVL 1

Author Comment

by:sharath_kulal
ID: 12177378
but i don't need too much flexibility. i just wanted to change the button text. thats all.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 12177391
Can you post the error message and the line that throws it? Your custom joptionpane should return a value in the same way that the defautl joptionpane does.
0
 
LVL 1

Author Comment

by:sharath_kulal
ID: 12177401
no it doen't throw any message but i get uninitializedValue when i call getInputValue().
but i've input some text in the text box.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12177433
Can't you use JOptionPane.showInputDialog()?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12177441
static Object showInputDialog(Component parentComponent,
                                                 Object message,
                                                 String title,
                                                 int messageType,
                                                 Icon icon,
                                                 Object[] selectionValues,             // Here you can give in your options
                                                 Object initialSelectionValue)
0
 
LVL 35

Expert Comment

by:girionis
ID: 12177444
> no it doen't throw any message but i get uninitializedValue when i call
> getInputValue().
> but i've input some text in the text box.

You probably do not read it proeprly then. Post the source code.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12177591
this is from here:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html


Show a dialog asking the user to type in a String:
String inputValue = JOptionPane.showInputDialog("Please input a value");


Show a dialog asking the user to select a String:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);


Hope this could help you.
Bye, Giant.
0
 
LVL 1

Author Comment

by:sharath_kulal
ID: 12177607
i basically wanted to display different text instead of ok and cancel. other than that the input dialog should work the same. i couln't find any way to do that so i created a custom option pane.
it solvs the problem of displaying different texts instead of OK and Cancel, and it also shows a textbox for user input. but the problem is how do i get the user input?
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12177629
see my post.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12177630
Why don't you just show us your code?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12177635
> but the problem is how do i get the user input?

As already suggested why don't you use one fo the default JOptionPane to do that? For more info:

http://javaalmanac.com/egs/javax.swing/frame_Dialog.html
0
 
LVL 1

Expert Comment

by:princevark
ID: 12177639

Instead of creating a optionpane and then creating dialog , try the following :

int value = JOptionPane.showOptionDialog(null,insert,"Title",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null, button,null);

This will create  blocking dialog and return the user input to "value".

regds
Rajoy
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12177655
>i basically wanted to display different text instead of ok and cancel. other than that the input dialog should work the same. i couln't find any way to do that so i created a custom option pane.
>it solvs the problem of displaying different texts instead of OK and Cancel, and it also shows a textbox for user input. but the problem is how do i get the user input?

As I told before:
Show a dialog asking the user to type in a String:
String inputValue = JOptionPane.showInputDialog("Please input a value");

Show a dialog asking the user to select a String:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);

Copy and paste and tell us if doesn't go.
inputValue and selectedValue are the user input value.

Bye, Giant.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 12177920
I think this might be what you want:

/*
 *  OptionDemo.java
 *
 */
import javax.swing.*;
import java.awt.*;

public class OptionDemo {

        public static void main(String[] args)
        {
            JPanel fields = new JPanel();
            JLabel label = new JLabel("Some text:");
            JTextField t1 = new JTextField(10);
            fields.add(label);
            fields.add(t1);

            String[] options = {"My OK!","My Cancel"};
            int input = JOptionPane.showOptionDialog(
                null,   // parentComponent
                fields, // message
                "Enter your input", // title
                JOptionPane.OK_CANCEL_OPTION,   // optionType
                JOptionPane.QUESTION_MESSAGE,   // messageType
                null,   // icon
                options,    // options
                options[0]  // initialValue
            );

            if (input == 0) {
                System.out.println("OK pressed. Text entered = "+t1.getText());
            }
            else if (input == 1) {
                System.out.println("Cancel pressed. Text entered = "+t1.getText());
            }

            System.exit(0);
        }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12399867
The author's last question was:
>>it solvs the problem of displaying different texts instead of OK and Cancel, and it also shows a textbox for user input. >>but the problem is how do i get the user input?
I think my last comment exactly shows what the author wanted.
It uses JOptionPane, the button texts are user definable and it shows how to get the user input.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12408076
Agree with zzynx,
A split (zzynx, Giant2) could be better than Delete.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12408383
Sorry Giant,
but in your code I don't see the combination of
- user definable buttons
- shows a textbox for user input
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12420790
I suggest:
>Show a dialog asking the user to type in a String:
>String inputValue = JOptionPane.showInputDialog("Please input a value");
>
>Show a dialog asking the user to select a String:
>Object[] possibleValues = { "First", "Second", "Third" };
>Object selectedValue = JOptionPane.showInputDialog(null,
>"Choose one", "Input",
>JOptionPane.INFORMATION_MESSAGE, null,
>possibleValues, possibleValues[0]);
>Copy and paste and tell us if doesn't go.
>inputValue and selectedValue are the user input value.

because the last question was:
>it solvs the problem of displaying different texts instead of OK and Cancel, and it also shows a textbox for user input. but the problem is how do i get the user input?
So he/she has not only button on the JOptionPane, but even other objects.
So I suggest to use Object[] and Object and not String or other particular objects. The mine was more general solution. Instead of Object can be used other kind of object.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12421126
Not convinced.
Let's analyze what he told us:

>> i basically wanted to display different text instead of ok and cancel.
>> other than that the input dialog should work the same.
So basically he wants an JOptionPane asking for input with different texts on the buttons.
But,
>> i couln't find any way to do that so i created a custom option pane.
And what does that custom pane do? (=what did he initially wanted the JOptionPane to do?)
    >> it solvs the problem of
1) >> displaying different texts instead of OK and Cancel, and
2) >> it also shows a textbox for user input.

So in fact he simply wanted a JOptionPane with special button texts + a text box to enter some user input text
And a solution to:
>> how do i get the user input?
Again, that's the combination my comment shows, which I don't see in yours.

This
     >>Show a dialog asking the user to type in a String:
     >>String inputValue = JOptionPane.showInputDialog("Please input a value");
only illustrates 2)
while your other code is about selecting items in a combo box (<> text box)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12423216
zzynx,
no problem for me if accept only your answer.
I say only that my code, as I told, came from the SunSite java-doc:
>this is from here:
>http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html
and try to explain the asker to furnish a reference on how he/she can use the JOptionPane. So, my code was an example. I try to give him/her an idea on the way to use, not the full code (My fear is to not do homework. Giving hime/her only the way I was not surely in this warning to do homework for something).

Hope now is clear where and what my answer cover.
Bye bye, Giant.
;)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12503424
I would rather accept my comment of Date: 09/29/2004 11:21AM CEST
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question