Search code examples
javaswingjtabletablecelleditorjpasswordfield

validate a table's cell using editors


I have a Password field editor for my JTable. I want to display an error message if the text length is less than 8 bit when the user clicks to edit another field. I have tried focus listeners. But its not working. Please help me because i have just started workin with 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;
    }


}

Solution

  • Override stopCellEditing() and implement the condition inside it.

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