[英]JTable cell editing doesnt change when cell is validated
我有一个自定义单元格编辑器,该编辑器可以验证输入的值是否为数字并且其长度为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;
}
}
从重写的stopCellEditing()
方法成功返回时,需要调用super.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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.