Can I disable a button in JOptionPane

I'm new to javaand this post is of no help for mehttp://forum.java.sun.com/thread.jspa?threadID=560682&tstart=45Does someone has code that can show me how can I disable a Button on JOptionPane?
[226 byte] By [juanchia] at [2007-11-26 18:30:01]
# 1

import java.awt.*;

import javax.swing.*;

public class DisableButton {

public static void main(String[] args) {

JOptionPane pane = new JOptionPane("Enter some text:");

pane.setInitialSelectionValue("This is the default text");

pane.setMessageType(JOptionPane.PLAIN_MESSAGE);

pane.setOptions(new String[] {"OK", "Cancel"});

pane.setWantsInput(true);

JDialog dialog = pane.createDialog(null, "Disable button test");

findAndSetButton(0, dialog);

dialog.setVisible(true);

System.exit(0);

}

static public void findAndSetButton(int level, Container cont) {

level++;

for (int i=0; i<cont.getComponentCount(); i++) {

Component cc= cont.getComponent(i);

if (cc instanceof JButton){//JButton is instance of Container; so chk 1st.

JButton b= (JButton)cc;

//if (b.getText().equals("OK")) b.setEnabled(false);

if (b.getText().equals("Cancel")) b.setEnabled(false);

}

else if (cc instanceof Container) {

findAndSetButton(level, (Container)cc);

}

}

level--;

}

}

>

Joerg22a at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks a lot Joerg22
juanchia at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...
# 3

Which will spectacularly fail on an application running under non-English locale. As a rule of thumb, any time you find yourself iterating over components of some container that doesn't expose its internal structure via an API, there's a good reason behind it and you should rethink your code.

kirillga at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hello,

at least concerning the locale I cannot follow your objection. I have this piece of code slightly modified implemented in a multilingual application with three non-English locales, and it works perfectly. Of course, the language dependant different labeling of components has to be taken into account, but this is not a problem and doesn't change the algorithm at all.

Regards

J鰎g

Joerg22a at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...
# 5

I can only reiterate what i said already - if you find yourself in the need to change the behavior of a predefined dialog (option pane), then that predefined dialog is not for you. The internal implementation which is not exposed via an API is subject to change at any time and you can end up having major maintenance issues.

kirillga at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...
# 6

I don't argue this point and think you are generally right. In the concrete case of a JOptionPane and from what I can see of Java's development history I wonder, however, whether cycling through its containers will NOT find its buttons in the near future. But what can we tell about the future?

So to be on the safe side, we should recommend Juanchi to write his own JDialog. But beware, Juanchi, some day Java may not exist any more at all.

Joerg22a at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...
# 7
Instead of literals like "OK" or "Cancel", use UIManager keys like OptionPane.okButtonText, OptionPane.cancelButtonText, OptionPane.noButtonText, OptionPane.yesButtonText, and their getString() results.
hiwaa at 2007-7-9 6:04:11 > top of Java-index,Desktop,Core GUI APIs...