验证表的使用单元格编辑器(validate a table's cell using edi

2019-07-04 01:35发布

我有我的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;
    }


}

Answer 1:

据我了解的问题,这是关于在编辑器中验证输入(模型保护自己免受无效值是另一个故事,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);


Answer 2:

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


}


Answer 3:

重写stopCellEditing()和u可以尝试下面的代码以获取错误单元格的焦点。

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


}


文章来源: validate a table's cell using editors