wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How can i get specific button in JOptionaPne Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can i get specific button in JOptionaPne" Watch "How can i get specific button in JOptionaPne" New topic
Author

How can i get specific button in JOptionaPne

Bhagya Tangutur
Ranch Hand

Joined: Oct 21, 2002
Posts: 88
I want to set the default Focus for OK button in JOptionpane.but the problem is I am showing TextArea in JOptionPane,so as one knows TextArea consumes enterEvent.i want to some how get OKButton reference and programatically say doCLick() on it,inside of KeyListener classes.
Any thoughts???Please respond ...


Sun Certified Java Programmer
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi,
use JOptionPane.showOptionDialog(...)
you can specify the components/objects (whatever) as argument "Object[] options". The API on JOptionPane provides some information at the description of the class.

options
A more detailed description of the set of option buttons that will appear at the bottom of the dialog box. The usual value for the options parameter is an array of Strings. But the parameter type is an array of Objects. A button is created for each object depending on its type:
Component
The component is added to the button row directly.
Icon
A JButton is created with this as its label.
other
The Object is converted to a string using its toString method and the result is used to label a JButton.
initialValue
The default selection (input value).

I'd write a little test program to play around with this option dialog. and to see what happens when you set the "initialValue".
Chantal
Bhagya Tangutur
Ranch Hand

Joined: Oct 21, 2002
Posts: 88
Chantal,
NIce to seee your reply.Infact i thought the same way in the beginning and played with it..It didn't work out.Please let me know if u are able to achieve this.or any other thoughts will be good either
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi,
I wrote a little test case. here is my conclusion:
- providing JButtons as options argument to the showOptionDialog method does not work as expected:
the buttons are displayed and if an initialValue is given, the appropriate button is focused, but the option pane does not return a value, and thus, not close. to use this solution you would have to add actionlisteners to all the buttons and somehow simulate the return of option pane values and close the option pane. I don't think, this is easy - at least, it's not easier than using JOptionPane directly.
- provding strings as button texts works as expected (though I didn't test this with more than 2 options (ok and cancel), and I didn't try out what happens if you give more strings as options than you specify through the optionType argument (which is one of OK_CANCEL_OPTION, YES_NO_CANCEL_OPTION etc.)
I think the difficulty JOptionPane has to cope with is - that you specify any components and it has to attach listeners to it to return the right value and close the dialog. this obviously does not work. as you don't need any other components than buttons as options (normally?), this possibility of OptionDialog is not used (at all?), I suppose. At least I have not found any entries complaining on it, in the bugparade (I will do that, next).
You wanted to put you're own buttons in the dialog to set the focus to the button you wish. this works very well using Strings as optons and the initialValue, but the initialValue has to be in the array of options. (see the test case)

Chantal

TEST CASE:
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi,
sorry, I just reread you're first posting.
Concerning the simulation of "ok". Maybe you don't need a reference to the ok button but you rather have to make the JOptionPane return the value OK_OPTION.
1. either you create you're own dialog where you specify explicitly when it shall return. (the API gives some sample on this.)
2. or you listen to the INPUT_VALUE_PROPERTY. though I don't know if you can make the JOptionPane close.

If a JOptionPane has configured to all input setWantsInput the bound property JOptionPane.INPUT_VALUE_PROPERTY can also be listened to, to determine when the user has input or selected a value.

3. What does work is: hit Enter inside a JTextField -> the Option Pane returns ok and closes. it might work to make the JTextArea send an ActionPerformed event on "Enter" (as this is what JTextField does).
hope this was of some help?!
Chantal
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Bhagya
Welcome to the JavaRanch! Please adjust your display name to meet
the JavaRanch Naming Policy. You can change it here.
Thanks!
and welcome to the JavaRanch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i get specific button in JOptionaPne