• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1059
  • Last Modified:

getting user input from custom JOptionPane

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
sharath_kulal
Asked:
sharath_kulal
  • 8
  • 6
  • 4
  • +3
1 Solution
 
armoghanCommented:
Instead of using JOptionPane, use JDialog, it will give you more flexibilty
0
 
girionisCommented:
What is the methdo that returns the input? That one you should use to get it.
0
 
sharath_kulalAuthor Commented:
but i don't need too much flexibility. i just wanted to change the button text. thats all.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
girionisCommented:
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
 
sharath_kulalAuthor Commented:
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
 
zzynxSoftware engineerCommented:
Can't you use JOptionPane.showInputDialog()?
0
 
zzynxSoftware engineerCommented:
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
 
girionisCommented:
> 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
 
Giant2Commented:
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
 
sharath_kulalAuthor Commented:
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
 
Giant2Commented:
see my post.
0
 
zzynxSoftware engineerCommented:
Why don't you just show us your code?
0
 
girionisCommented:
> 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
 
princevarkCommented:

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
 
Giant2Commented:
>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
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
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
 
Giant2Commented:
Agree with zzynx,
A split (zzynx, Giant2) could be better than Delete.
0
 
zzynxSoftware engineerCommented:
Sorry Giant,
but in your code I don't see the combination of
- user definable buttons
- shows a textbox for user input
0
 
Giant2Commented:
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
 
zzynxSoftware engineerCommented:
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
 
Giant2Commented:
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
 
zzynxSoftware engineerCommented:
I would rather accept my comment of Date: 09/29/2004 11:21AM CEST
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 8
  • 6
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now