Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...