Hi all

I am aware of possibility to have an array of objects (usually Strings) as the parameter options, so we can have buttons with custom labels.
Is there any easier way to just for example change the value of OK_OPTION option button label/text from "ok" to "okAy" ?

Thanks in advance

Dani AI

Generated

Good summary so far: found the UI defaults route, correctly pointed out the per-dialog alternatives (custom dialog or the options array), and is right to point you at the UIDefaults so you can discover keys instead of guessing. A few practical tips and examples to make the approach safe and predictable.

To discover every OptionPane-related key for your current look-and-feel, enumerate the UIDefaults rather than hard-coding names:

UIDefaults defaults = UIManager.getDefaults();
for (Enumeration<?> e = defaults.keys(); e.hasMoreElements();) {
    Object k = e.nextElement();
    String name = k.toString();
    if (name.startsWith("OptionPane.")) {
        System.out.println(name + " = " + defaults.get(k));
    }
}

That prints the available keys (labels, mnemonics, icons) so you can find the exact key to change for your LAF.

If you only need to change the label for a single dialog, avoid a global permanent change. Save the old value, set the new one, show the dialog, then restore the old value — and do this on the EDT:

Object old = UIManager.get("OptionPane.okButtonText");
try {
    UIManager.put("OptionPane.okButtonText", "OkAy");
    JOptionPane.showMessageDialog(parent, "Message");
} finally {
    UIManager.put("OptionPane.okButtonText", old);
}

Notes and cautions: UIManager changes affect every Swing dialog (and multiple windows) until restored, and keys can differ between look-and-feels and locales. For one-off dialogs prefer passing a custom options array to showOptionDialog or building a small JDialog if you need layout/mnemonic control or localization.

Recommended Answers

All 4 Replies

Well, you could create your own custom dialog, but I wouldn't say that is easier than using the array for your options with showOptionDialog().

Thanks
But somewhere else i got a good answer :
using

UIManager.put("OptionPane.yesButtonText", "yeAh")

for example cause to show "yeAh" instead of "yes" for all JOption panes.
So, this is resolved.
But I dont know how to find all of swing.properties keys, however thats another subject ...

Thanks
But somewhere else i got a good answer :
using

UIManager.put("OptionPane.yesButtonText", "yeAh")

for example cause to show "yeAh" instead of "yes" for all JOption panes.
So, this is resolved.
But I dont know how to find all of swing.properties keys, however thats another subject ...

Then why don't look the API of the UIManager class. I did and there is a method: getLookAndFeelDefaults. That method returns a UIDefaults object (look the API) which extends the Hashtable (again from the API of the UIDefaults-look at the definition of the class). So it inherits methods that lets you get all the properties. For that you will need to look the API of Hashtable.

Then why don't look the API of the UIManager class. I did and there is a method: getLookAndFeelDefaults. That method returns a UIDefaults object (look the API) which extends the Hashtable (again from the API of the UIDefaults-look at the definition of the class). So it inherits methods that lets you get all the properties. For that you will need to look the API of Hashtable.

I did that...
Because This is completely a seperate subject i should have to start another thread :
http://www.daniweb.com/forums/post1210447.html#post1210447

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.