[英]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.