I'm creating a dialog via JOptionPane.showOptionDialog
. If I set Object[] options = null
and set int optionType = JOptionPane.OK_CANCEL_OPTION
, I get nice OK
and Cancel
buttons, with text and icons (perhaps look-and-feel specific?).
However I want to add a third button with my own Icon, so I'm explicitly specifying Object[] options = new Object[] {"OK", "Cancel", <some Icon object>}
. The Icon object looks great, but now I have ugly text buttons for OK and Cancel.
Is there a way to keep the "built-in" OK and Cancel buttons while also adding my own?
Instead of using strings for "OK" and "Cancel" options, use JLabels with icons, or plain icons.
It's right about here that I usually delve into the source code of showOptionDialog
to see how it sets up the JOptionPane
(after all, it's just a convenience method), and that shows what is needed to do to accomplish the desired result.
This seems to work:
JPanel panel = new JPanel();
panel.add(new JButton("OK"));
panel.add(new JButton("Cancel"));
panel.add(new JButton("Custom"));
JOptionPane.showOptionDialog(null, "Message", "Title", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, new Object[] { panel }, null);
Of course I didn't add icons for this test (and I just ran it from main
, so there's no parent component); adding them to a button is trivial. You'll have to supply your own ActionListener
s to set the selection and close the dialog.
If you want the stock icons, you'll have to get them from UIManager.getIcon(String)
-- finding its name will be the harder part. I don't see icons on the stock JOptionPane buttons on a Mac, even using the Metal theme.