2
나는에 따라 in a fashion like this이라는 JDialog를 만들었습니다. JOptionPane의 생성자를 사용하여 일시적으로 JDialog의 ok 버튼을 비활성화합니다. 따라서 JTextField 유효성 확인
: 그들은이 JOptionPane의 생성자에 의해 생성되기 때문에optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
나는 "예"와 "아니오"버튼에 대한 언급은 없었다.
지금, 나의 대화에서 나는 continuosly 텍스트 필드의 입력의 유효성을 검사하는 날에 의해 생성 된 InputValidator와 JFormattedText 필드가 :
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);
}
}
나는이 질문에 대한 매우 관련이없는에도 경우가 InputVerifier 코드를 기록했다.
이제 필드의 유효성이 검사 될 때까지 일시적으로 "확인"버튼을 비활성화하고 싶지만 참조가 없습니다.
어떻게하면됩니까?
JButton b = optionPane.getOkButton();
if (myFieldNotValidate)
b.setEnabled(false);
포인터 [여기 (http://stackoverflow.com/questions/5877994/how-to-change-the-button-backgrounds-inside-joptionpane) –
. 나는 camickr 솔루션으로 해결했습니다. 멋지다! – Heisenbug