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
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.
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