简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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