编辑 JTable / TableModel 中列的单元格限制
Editing cell restrictions of a column in a JTable / TableModel
我希望修改我的 JTable 中处理单元格输入的方式。目前我正在使用如下所示的 DefaultTableModel。
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int col) {
return col == 1 || col == 2 || col == 6;
}
@Override
public Class getColumnClass(int col) {
if (col == 6) {
return Integer.class;
} else {
return String.class;
}
}
};
使用此代码,第 6 列强制用户输入整数。我想更进一步,不允许数字超出范围(例如 1-100),并且该字段永远不会为空(该列将在用户交互之前包含数据)。我已经阅读了文档,但似乎找不到任何管理列类型行为的内容。谢谢!
您需要创建自定义编辑器。
以下示例演示了强制数据为 5 个字符的编辑器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableFiveCharacterEditor extends DefaultCellEditor
{
private long lastTime = System.currentTimeMillis();
public TableFiveCharacterEditor()
{
super( new JTextField() );
}
public boolean stopCellEditing()
{
JTable table = (JTable)getComponent().getParent();
try
{
String editingValue = (String)getCellEditorValue();
if(editingValue.length() != 5)
{
JTextField textField = (JTextField)getComponent();
textField.setBorder(new LineBorder(Color.red));
textField.selectAll();
textField.requestFocusInWindow();
JOptionPane.showMessageDialog(
table,
"Please enter string with 5 letters.",
"Alert!",JOptionPane.ERROR_MESSAGE);
return false;
}
}
catch(ClassCastException exception)
{
return false;
}
return super.stopCellEditing();
}
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;
}
private static void createAndShowUI()
{
JTable table = new JTable(5, 5);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
// Use a custom editor
TableCellEditor fce = new TableFiveCharacterEditor();
table.setDefaultEditor(Object.class, fce);
JFrame frame = new JFrame("Table Five Character Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( scrollPane );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
您需要修改编辑器以确保:
- 通过使用 Integer.parseInt(...) 方法
,数据是一个整数
- 整数值在您想要的范围内
编辑:
how can I apply this to influence one column rather than the entire table.
您可以将编辑器添加到 TableColumn
:
table.getColumnModel().getColumn(...).setCellEditor(...);
覆盖 DefaultCellEditor 的行为可实现预期结果。
DefaultCellEditor editor = new DefaultCellEditor(new JTextField()) {
@Override
public boolean stopCellEditing() {
JTable table = (JTable) getComponent().getParent();
try {
String value = (String) getCellEditorValue();
if (Integer.parseInt(value) < 1) {
JTextField textField = (JTextField) getComponent();
textField.setBorder(new LineBorder(Color.red));
textField.selectAll();
textField.requestFocusInWindow();
return false;
}
} catch (NumberFormatException ex) {
return false;
}
return super.stopCellEditing();
}
};
然后添加到需要的列中table.
table.getColumnModel().getColumn(6).setCellEditor(editor);
编辑:如果抛出 NumberFormatException,if 语句内的行为将不会发生,您可能希望将相同的代码行放在 catch 中。
我希望修改我的 JTable 中处理单元格输入的方式。目前我正在使用如下所示的 DefaultTableModel。
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int col) {
return col == 1 || col == 2 || col == 6;
}
@Override
public Class getColumnClass(int col) {
if (col == 6) {
return Integer.class;
} else {
return String.class;
}
}
};
使用此代码,第 6 列强制用户输入整数。我想更进一步,不允许数字超出范围(例如 1-100),并且该字段永远不会为空(该列将在用户交互之前包含数据)。我已经阅读了文档,但似乎找不到任何管理列类型行为的内容。谢谢!
您需要创建自定义编辑器。
以下示例演示了强制数据为 5 个字符的编辑器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableFiveCharacterEditor extends DefaultCellEditor
{
private long lastTime = System.currentTimeMillis();
public TableFiveCharacterEditor()
{
super( new JTextField() );
}
public boolean stopCellEditing()
{
JTable table = (JTable)getComponent().getParent();
try
{
String editingValue = (String)getCellEditorValue();
if(editingValue.length() != 5)
{
JTextField textField = (JTextField)getComponent();
textField.setBorder(new LineBorder(Color.red));
textField.selectAll();
textField.requestFocusInWindow();
JOptionPane.showMessageDialog(
table,
"Please enter string with 5 letters.",
"Alert!",JOptionPane.ERROR_MESSAGE);
return false;
}
}
catch(ClassCastException exception)
{
return false;
}
return super.stopCellEditing();
}
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;
}
private static void createAndShowUI()
{
JTable table = new JTable(5, 5);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
// Use a custom editor
TableCellEditor fce = new TableFiveCharacterEditor();
table.setDefaultEditor(Object.class, fce);
JFrame frame = new JFrame("Table Five Character Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( scrollPane );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
您需要修改编辑器以确保:
- 通过使用 Integer.parseInt(...) 方法 ,数据是一个整数
- 整数值在您想要的范围内
编辑:
how can I apply this to influence one column rather than the entire table.
您可以将编辑器添加到 TableColumn
:
table.getColumnModel().getColumn(...).setCellEditor(...);
覆盖 DefaultCellEditor 的行为可实现预期结果。
DefaultCellEditor editor = new DefaultCellEditor(new JTextField()) {
@Override
public boolean stopCellEditing() {
JTable table = (JTable) getComponent().getParent();
try {
String value = (String) getCellEditorValue();
if (Integer.parseInt(value) < 1) {
JTextField textField = (JTextField) getComponent();
textField.setBorder(new LineBorder(Color.red));
textField.selectAll();
textField.requestFocusInWindow();
return false;
}
} catch (NumberFormatException ex) {
return false;
}
return super.stopCellEditing();
}
};
然后添加到需要的列中table.
table.getColumnModel().getColumn(6).setCellEditor(editor);
编辑:如果抛出 NumberFormatException,if 语句内的行为将不会发生,您可能希望将相同的代码行放在 catch 中。