简体   繁体   English

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

[英]JTable cell editing doesnt change when cell is validated

I have a custom cell editor which validates if the entered value is a number and its length is 3. 我有一个自定义单元格编辑器,该编辑器可以验证输入的值是否为数字并且其长度为3。

Right now I am able to make sure that if invalid value is entered the current cell stays editable and focus does not move to next cell. 现在,我可以确保如果输入了无效值,则当前单元格保持可编辑状态,并且焦点不会移至下一个单元格。

But when valid value is entered the current cell still remains editable and the focus alone shifts to next cell. 但是,当输入有效值时,当前单元格仍然保持可编辑状态,并且焦点仅移至下一个单元格。

Also The commented part of showing an alert also doesnt work. 另外,显示警报的注释部分也不起作用。 The whole application hangs and i believe the prompt is coming in the background. 整个应用程序挂起,我相信提示会在后台出现。

Below is the code of the editor 下面是编辑器的代码

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

} }

You need to call super.stopCellEditing() when you successfully return from your overridden stopCellEditing() method. 从重写的stopCellEditing()方法成功返回时,需要调用super.stopCellEditing()

See below example program I've written using your cell editor. 请参见下面我使用单元格编辑器编写的示例程序。 I have added super.stopCellEditing() and now it works. 我添加了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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM