temporarily disable ok button on a JDialog, accordingly to a JTextField validation
I have a JDialog created in a fashion like this, accordingly to Oracle's tutorial.
using the JOptionPane constructor:
optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
I have no reference to the "yes" and "no" buttons, because they are created by the JOptionPane constructor.
Now, in my dialog I have a JFormattedText field with a InputValidator created by me that continuosly validate text field's input:
public class ValidatedDoubleField extends InputVerifier implements DocumentListener {
private JTextField field;
private Border defaultBorder;
public ValidatedDoubleField(JFormattedTextField f){
this.field = f;
this.defaultBorder = f.getBorder();
f.getDocument().addDocumentListener(this);
}
@Override
public boolean verify(JComponent input) {
//System.out.println("verify");
if (input instanceof JTextField){
JTextField f = (JTextField)input;
try{
Double value = Double.parseDouble(f.getText().replace(',', '.'));
return true;
}catch (NumberFormatException e){
return false;
}
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
try{
Double value = Double.parseDouble(f.getText().replace(',', '.'));
return true;
}catch (NumberFormatException e){
return false;
}
}
return false;
}
public boolean shouldYieldFocus(JComponent input){
boolean inputOK = verify(input);
if (inputOK) {
if (input instanceof JTextField){
JTextField f = (JTextField)input;
f.setBorder(defaultBorder);
return true;
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
f.setBorder(defaultBorder);
return true;
}else
开发者_开发知识库return false;
} else {
if (input instanceof JTextField){
JTextField f = (JTextField)input;
f.setBorder(BorderFactory.createLineBorder(Color.red));
Toolkit.getDefaultToolkit().beep();
return false;
}else if (input instanceof JFormattedTextField){
JFormattedTextField f = (JFormattedTextField)input;
f.setBorder(BorderFactory.createLineBorder(Color.red));
Toolkit.getDefaultToolkit().beep();
return false;
}else
return false;
}
//return true;
}
@Override
public void changedUpdate(DocumentEvent e) {
this.field.getInputVerifier().shouldYieldFocus(field);
}
@Override
public void insertUpdate(DocumentEvent e) {
this.field.getInputVerifier().shouldYieldFocus(field);
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
this.field.getInputVerifier().shouldYieldFocus(field);
}
}
I posted the InputVerifier code even if it's not so relevant for the question.
Now what I would like to do is temporarily disable the "ok" button until the field will be validated, but I haven't a reference to it.
How can I do that?
I'm looking for something like:
JButton b = optionPane.getOkButton();
if (myFieldNotValidate)
b.setEnabled(false);
You can try something like this to locate buttons at JOptionPane Dialog.
public class Snippet {
public static void main(String[] args) {
JOptionPane optionPane = new JOptionPane("Test",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null);
List<JButton> buttons = new ArrayList<JButton>();
loadButtons(optionPane, buttons);
}
public static void loadButtons(JComponent comp, List<JButton> buttons) {
if (comp == null) {
return;
}
for (Component c : comp.getComponents()) {
if (c instanceof JButton) {
buttons.add((JButton) c);
} else if (c instanceof JComponent) {
loadButtons((JComponent) c, buttons);
}
}
}
}
精彩评论