Forums Register Login
How can i get specific button in JOptionaPne
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 ...
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.

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:
The component is added to the button row directly.
A JButton is created with this as its label.
The Object is converted to a string using its toString method and the result is used to label a JButton.
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".
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
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)


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?!
Welcome to the JavaRanch! Please adjust your display name to meet
the JavaRanch Naming Policy. You can change it here.
and welcome to the JavaRanch!
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 1419 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 15, 2018 15:24:21.