2012-11-22 85 views
4

我有一個用於我的JTable的密碼字段編輯器。如果當用戶單擊編輯另一個字段時文本長度小於8位,我想顯示一條錯誤消息。我已經嘗試過重點聽衆。但它不工作。請幫助我,因爲我剛剛開始使用java swing進行工作。使用編輯器驗證表格單元格

class PasswordEditor extends DefaultCellEditor 
{ 

    TextBox m_passWord = new TextBox(); 
    public PasswordEditor() { 
     super(new TextBox()); 
    } 

    @Override 
    public Object getCellEditorValue() { 

     return this.m_passWord.getText(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, 
      Object value, boolean isSelected, int row, int column) { 

     Object fieldValue = value; 
     if(null == fieldValue) 
      fieldValue = Constants.EMPTY_STRING; 

     this.m_passWord.addInputMethodListener(new InputMethodListener() { 

      @Override 
      public void inputMethodTextChanged(InputMethodEvent event) 
      { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void caretPositionChanged(InputMethodEvent event) 
      { 
       // TODO Auto-generated method stub 

      } 
     }) 
     this.m_passWord.addFocusListener(new FocusListener() { 

      @Override 
      public void focusLost(FocusEvent e) 
      { 
       if (!e.isTemporary()) { 
         String content = PasswordEditor.this.m_passWord.getText(); 
         System.out.println((content)); 
       } 

      } 

      @Override 
      public void focusGained(FocusEvent e) 
      { 
       //TODO init 
      } 
     }); 

     this.m_passWord.setEditable(true); 
     this.m_passWord.setText(fieldValue.toString()); 
     return this.m_passWord; 
    } 


} 
+0

我該如何改進? – Nikhil

+0

你可以試試這個... ** String content = this.m_passWord.getText(); ** –

+0

thenafter whats TextBox ??? – mKorbel

回答

5

重寫stopCellEditing()並實現其中的條件。

class PasswordEditor extends DefaultCellEditor 
{ 

    TextBox m_passWord = new TextBox(); 
    public PasswordEditor() { 
     super(new TextBox()); 
    } 

      @Override 
    public boolean stopCellEditing() 
    { 
     if(getCellEditorValue().toString().length() < 8) 
     { 
      JOptionPane.showMessageDialog(UsmUserView.this.m_Parent, "Password Must Be 8 Bytes Long !! Please Check"); 
      return false; 
     } 
     fireEditingStopped(); 
     return true; 
    } 

    @Override 
    public Object getCellEditorValue() { 

     return this.m_passWord.getText(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, 
      Object value, boolean isSelected, int row, int column) { 

     Object fieldValue = value; 
     if(null == fieldValue) 
      fieldValue = Constants.EMPTY_STRING; 

     this.m_passWord.setEditable(true); 
     this.m_passWord.setText(fieldValue.toString()); 
     return this.m_passWord; 
    } 


} 
9

據我瞭解的問題,這是關於驗證的編輯(模型保護自己免受無效值是另一個故事,IMO)的輸入,並通知用戶關於他/她的錯誤時,他/她嘗試提交輸入。

這樣做的一種簡單方法是使用InputVerifier:

  • 執行其驗證方法驗證規則
  • 執行其shouldYieldFocus
  • 子類DefaultCellEditor通知並覆蓋其stopCellEditing調用shouldYieldFocus並返回結果(又名:拒絕提交編輯)

一些代碼片段:

final InputVerifier iv = new InputVerifier() { 

    @Override 
    public boolean verify(JComponent input) { 
     JTextField field = (JTextField) input; 
     return field.getText().length() > 8; 
    } 

    @Override 
    public boolean shouldYieldFocus(JComponent input) { 
     boolean valid = verify(input); 
     if (!valid) { 
      JOptionPane.showMessageDialog(null, "invalid"); 
     } 
     return valid; 
    } 

}; 
DefaultCellEditor editor = new DefaultCellEditor(new JTextField()) { 
    { 
     getComponent().setInputVerifier(iv); 
    } 

    @Override 
    public boolean stopCellEditing() { 
     if (!iv.shouldYieldFocus(getComponent())) return false; 
     return super.stopCellEditing(); 
    } 

    @Override 
    public JTextField getComponent() { 
     return (JTextField) super.getComponent(); 
    } 

}; 

JTable table = new JTable(10, 2); 
table.setDefaultEditor(Object.class, editor); 
+0

+1'InputVerifier'和API的區別的一個很好的例證擴展了JTextField,裝箱類關於_side effects_。 Nikhil:不要害怕展開選項窗格的錯誤消息。 :-) – trashgod

1

覆蓋stopCellEditing(),你可以嘗試下面的代碼來獲取錯誤單元格的焦點。

class PasswordEditor extends DefaultCellEditor 
{ 

    private TextBox m_passWord = new TextBox(); 
    public PasswordEditor() { 
     super(new TextBox()); 
    } 

      @Override 
    public boolean stopCellEditing() 
    { 
     if(getCellEditorValue().toString().length() < 8) 
     { 
      // Text box will get the focus and will shown in Red line as border for that cell. 
      TextBox aTextBox = (TextBox)getComponent(); 
      aTextBox.setBorder(new LineBorder(Color.red)); 
      aTextBox.selectAll(); 
      aTextBox.requestFocusInWindow(); 
      JOptionPane.showMessageDialog(UsmUserView.this.m_Parent, "Password Must Be 8 Bytes Long !! Please Check"); 
      return false; 
     } 
     return super.stopCellEditing(); 
    } 

    @Override 
    public Object getCellEditorValue() { 

     return this.m_passWord.getText(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, 
      Object value, boolean isSelected, int row, int column) { 

     Object fieldValue = value; 
     if(null == fieldValue) 
      fieldValue = Constants.EMPTY_STRING; 

     this.m_passWord.setEditable(true); 
     this.m_passWord.setText(fieldValue.toString()); 
     return this.m_passWord; 
    } 


}