验证单元格后,JTable单元格编辑不会更改 [英] JTable cell editing doesnt change when cell is validated

查看:103
本文介绍了验证单元格后,JTable单元格编辑不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom cell editor which validates if the entered value is a number and its length is 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.

下面是编辑器的代码

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

}

推荐答案

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

You need to call super.stopCellEditing() when you successfully return from your overridden stopCellEditing() method.

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

See below example program I've written using your cell editor. I have added super.stopCellEditing() and now it works.

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

这篇关于验证单元格后,JTable单元格编辑不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆