Java:为什么 JTable 不使用 TableCellEditor?
MyTableCellEditor 的目的是使 JTable 单元格的行为类似于 Excel 单元格,IOW,单击单元格后输入一个值会覆盖而不是附加到现有值。 我没想到以下代码会在第一遍中起作用,但我确实希望在 getTableCellEditorComponent 和 getCellEditorValue 中命中调试断点。 当我使用 jTable 时,为什么没有调用 getTableCellEditorComponent 或 getCellEditorValue?
public class MyTable extends javax.swing.JFrame implements TableModelListener {
private static final MyTableCellEditor tableCellEditor =
new MyTableCellEditor();
...
public MyTable() {
initComponents();
jTable.getModel().addTableModelListener(MyTable.this);
...
private void initComponents() { // Generated by the Form Editor.
jTable = new javax.swing.JTable();
jTable.setCellEditor(tableCellEditor);
...
public class MyTableCellEditor extends AbstractCellEditor implements
TableCellEditor {
JComponent component = new JTextField();
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex) {
if (isSelected) {
((JTextField)component).selectAll();
}
((JTextField)component).setText((String)value);
return component;
}
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
The intention of MyTableCellEditor is to make a JTable cell behave like an Excel cell, IOW, entering a value after single clicking in a cell overwrites rather than appends to the existing value.
I didn't expect the following code to work on the first pass, but I did expect to be hit debug breakpoints in getTableCellEditorComponent and getCellEditorValue.
Why isn't getTableCellEditorComponent or getCellEditorValue called when I use jTable?
public class MyTable extends javax.swing.JFrame implements TableModelListener {
private static final MyTableCellEditor tableCellEditor =
new MyTableCellEditor();
...
public MyTable() {
initComponents();
jTable.getModel().addTableModelListener(MyTable.this);
...
private void initComponents() { // Generated by the Form Editor.
jTable = new javax.swing.JTable();
jTable.setCellEditor(tableCellEditor);
...
public class MyTableCellEditor extends AbstractCellEditor implements
TableCellEditor {
JComponent component = new JTextField();
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex) {
if (isSelected) {
((JTextField)component).selectAll();
}
((JTextField)component).setText((String)value);
return component;
}
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setCellEditor(TableCellEditor)
仅为当前活动单元格设置编辑器(一旦不再活动,该编辑器就会丢失。您需要调用JTable.setDefaultEditor()
为特定类设置默认编辑器。或者,您可以通过
TableColumnModel
为列设置编辑器,例如。setCellEditor(TableCellEditor)
sets the editor for the currently active cell only (and will be lost once it's no longer active. You want to callJTable.setDefaultEditor()
to set the default editor for a specific class.Alternatively, you can set an editor for the column through the
TableColumnModel
, eg