Forums Register Login

How can i get specific button in JOptionaPne

+Pie Number of slices to send: Send
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 ...
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1700 times.
Similar Threads
COMMUNICATE WITH A PRINTER
Retrive variable from JavaScript to JSP page
getting text from textarea
Save output of command prompt in JTextarea.
insert text from servlet to textarea of html
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:20:48.