验证单元格后,JTable单元格编辑不会更改

时间:2019-03-19 10:11:07

标签: java swing user-interface jtable

我有一个自定义单元格编辑器,该编辑器可验证输入的值是否为数字并且其长度为3。

现在,我可以确保如果输入了无效值,则当前单元格保持可编辑状态,并且焦点不会移至下一个单元格。

但是,当输入有效值时,当前单元格仍然保持可编辑状态,并且焦点仅移至下一个单元格。

也显示警报的评论部分也不起作用。整个应用程序挂起,我相信提示会在后台出现。

下面是编辑器的代码

public class DepartmentCellEditor extends DefaultCellEditor{


public DepartmentCellEditor()
{
    super( new JTextField() );
}

public boolean stopCellEditing()
{
    JTable table = (JTable)getComponent().getParent();

    try
    {           
         boolean isValid = true;
         String s = getCellEditorValue().toString();
         if ( s.length() == 3 ) {
             for ( int i = 0; i < s.length(); i++ ) {
                 if ( !Character.isDigit( s.charAt( i ) ) ) {
                     isValid = false;
                     break;
                 }
             }
         } else {
             isValid = false;
         }
         if ( !isValid ) {

           JTextField textField = (JTextField)getComponent();
           textField.setBorder(new LineBorder(Color.red));
           textField.selectAll();
           textField.requestFocusInWindow();
           /*JOptionPane.showMessageDialog(
          null,
         "Please enter a 3 digit number.",
           "Alert!",JOptionPane.ERROR_MESSAGE);*/
         } else {
             JTextField textField = (JTextField)getComponent();
             textField.setBorder(new LineBorder(Color.black));
         }
         return isValid;
    }
    catch(ClassCastException exception)
    {
        JTextField textField = (JTextField)getComponent();
        textField.setBorder(new LineBorder(Color.red));
        textField.selectAll();
        textField.requestFocusInWindow();
        return false;
    }
}

public Component getTableCellEditorComponent(
    JTable table, Object value, boolean isSelected, int row, int column)
{
    Component c = super.getTableCellEditorComponent(
        table, value, isSelected, row, column);
    ((JComponent)c).setBorder(new LineBorder(Color.black));

    return c;
}

}

1 个答案:

答案 0 :(得分:1)

从覆盖的super.stopCellEditing()方法成功返回后,您需要调用stopCellEditing()

请参阅以下我使用您的单元格编辑器编写的示例程序。我添加了super.stopCellEditing(),现在可以使用了。

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class DepartmentCellEditor extends DefaultCellEditor
{
  public DepartmentCellEditor()
  {
    super( new JTextField() );
  }

  public boolean stopCellEditing()
  {
    JTable table = (JTable)getComponent().getParent();

    try
    {
      boolean isValid = true;
      String s = getCellEditorValue().toString();
      if ( s.length() == 3 ) {
        for ( int i = 0; i < s.length(); i++ ) {
          if ( !Character.isDigit( s.charAt( i ) ) ) {
            isValid = false;
            break;
          }
        }
      } else {
        isValid = false;
      }
      if ( !isValid ) {

        JTextField textField = (JTextField)getComponent();
        textField.setBorder(new LineBorder(Color.red));
        textField.selectAll();
        textField.requestFocusInWindow();
           /*JOptionPane.showMessageDialog(
          null,
         "Please enter a 3 digit number.",
           "Alert!",JOptionPane.ERROR_MESSAGE);*/
      } else {
        JTextField textField = (JTextField)getComponent();
        textField.setBorder(new LineBorder(Color.black));
      }
      return isValid && super.stopCellEditing(); //THIS IS THE CHANGE
    }
    catch(ClassCastException exception)
    {
      JTextField textField = (JTextField)getComponent();
      textField.setBorder(new LineBorder(Color.red));
      textField.selectAll();
      textField.requestFocusInWindow();
      return false;
    }
  }

  public Component getTableCellEditorComponent(
      JTable table, Object value, boolean isSelected, int row, int column)
  {
    Component c = super.getTableCellEditorComponent(
        table, value, isSelected, row, column);
    ((JComponent)c).setBorder(new LineBorder(Color.black));

    return c;
  }

  public static void main(String[] args)
  {
    JTable table = new JTable(new String[][] {{"111", "222"}, {"", ""}}, new String[] {"A", "B"});
    table.getColumn("A").setCellEditor(new DepartmentCellEditor());
    table.getColumn("B").setCellEditor(new DepartmentCellEditor());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JScrollPane(table));
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
  }
}