Solved

getting user input from custom JOptionPane

Posted on 2004-09-29
26
962 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
  • 8
  • 6
  • 4
  • +3
26 Comments
 
LVL 18

Expert Comment

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

Expert Comment

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

Author Comment

by:sharath_kulal
Comment Utility
but i don't need too much flexibility. i just wanted to change the button text. thats all.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
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
Comment Utility
Can't you use JOptionPane.showInputDialog()?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
see my post.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

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

Expert Comment

by:girionis
Comment Utility
> 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
Comment Utility

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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
Agree with zzynx,
A split (zzynx, Giant2) could be better than Delete.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I would rather accept my comment of Date: 09/29/2004 11:21AM CEST
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now