validate a table's cell using editors
Asked Answered
B

3

4

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


}
Bakke answered 22/11, 2012 at 8:52 Comment(3)
can you try this... String content = this.m_passWord.getText();Mcnamee
@Srinivas B please to click to tag JPasswordField and to check how to get value from JPasswordFieldVadnee
TextBox is a class which i have crated by extending JTextField for my application's useBakke
B
6

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


}
Bakke answered 28/11, 2012 at 5:26 Comment(0)
A
11

As I understand the question, it's about validating the input in the editor (the model protecting itself against invalid values is another story, IMO) and notifying the user about his/her error when s/he tries to commit the input.

A simple means of doing so is using an InputVerifier:

  • implement the validation rule in its verify method
  • implement the notification in its shouldYieldFocus
  • subclass DefaultCellEditor and override its stopCellEditing to call shouldYieldFocus and return its result (aka: refuse to commit the edit)

Some code snippet:

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);
Asch answered 22/11, 2012 at 10:34 Comment(1)
+1 for InputVerifier and a good illustration of the API distinction regarding side effects. Nikhil: Don't be afraid to expand the option pane's error message. :-)Fahlband
B
6

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


}
Bakke answered 28/11, 2012 at 5:26 Comment(0)
G
1

Override the stopCellEditing() and u can try the below code to get the focus for the error cell.

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


}
Garfieldgarfinkel answered 11/11, 2013 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.