Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JOptionPane - Shortcuts

456 views
Skip to first unread message

Michael Joos

unread,
Nov 13, 2001, 4:57:45 AM11/13/01
to
Hallo,

we use the class javax.swing.JOptionPane. It works fine, but now I want
implement shortcuts for the button which are shown in the Dialogbox, e.g.
for the button "Yes" the Y.

Have anybody an idee to solve these problem.

Thanks
Michael


Greg Faron

unread,
Nov 13, 2001, 11:47:45 AM11/13/01
to

Use the following constructor of class JOptionPane:

JOptionPane(Object message,
int messageType,
int optionType,
Icon icon,
Object[] options,
Object initialValue);

The 'options' argument can be an array of JButton objects, each
of which you've preassigned a KeyListener. When you detect a "y",
doClick() on the Yes Button, when you detect "n", doClick() on the
No Button. The same listener should be added to all of the buttons,
as focus could be on any of them when a key press occurs.

Here's an example using this constructor (and getting the return
value) from some code I have. Be aware that I use an array of
Strings instead of JButtons in my constructor. It should be a
simple task for you to replace them as described above.

JEditorPane message = new JEditorPane("text/html", htmltext);
message.setEditable(false);
String title = "Copyright Notice";
ImageIcon icon = new ImageIcon("somefile.gif");
String website = "Visit Website";
String ok = "Ok";
JOptionPane pane = new JOptionPane(
message,
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
icon,
new Object[] { website, ok }, // <---- this one
null);

message.setBackground(pane.getBackground());
pane.setPreferredSize(new Dimension(420, 275));

JDialog dialog = pane.createDialog(app, title);
dialog.setBackground(pane.getBackground());
dialog.show();

Object selectedValue = pane.getValue();
if (selectedValue == website)
doSomething();

--
Greg Faron
Integre Technical Publishing Co.

Joe

unread,
Nov 13, 2001, 12:09:23 PM11/13/01
to
psst did you hear what Michael Joos said
yes there are methods in JOptionPane for doing this I just don't know
them of the top of my head.


oh wait. I thinking of JFileChooser. there doesn't seem to be anything in
JOptionPane to do this if you using the static showXXX methods. sorry.
--
Joe

"I bent my wookie" - Ralph Wiggum

smartaa...@gmail.com

unread,
Oct 29, 2018, 9:14:32 AM10/29/18
to

Rhino

unread,
Oct 29, 2018, 2:44:13 PM10/29/18
to
Inside this post, it says the question was originally asked in 2001. I'm
going to assume the OP sorted out his issue long ago....

--
Rhino
0 new messages