2011-10-12 3 views
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); 
+2

포인터 [여기 (http://stackoverflow.com/questions/5877994/how-to-change-the-button-backgrounds-inside-joptionpane) –

+0

. 나는 camickr 솔루션으로 해결했습니다. 멋지다! – Heisenbug

답변

3

당신은 JOptionPane의 대화 상자에서 버튼의 위치를 ​​이런 식으로 뭔가를 시도 할 수 있습니다 :

내가 좋아하는 뭔가를 찾고 있어요. 감사 : 베어러 @ring

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); 
      } 
     } 
    } 

} 
관련 문제