在 JTable 中编辑单元格时提供附加行为
我正在用 Java 创建一个应用程序。在编辑 JTable 中的单元格时,我需要提供额外的行为。因此,理想情况下,当单元格在编辑后失去焦点时,就会发生这种情况。根据某些后处理,我可能会重置单元格的值。我尝试使用单元格编辑器,但它没有给我所需的行为。
在默认的 JTable 中,仅当我双击单元格时,它才变得可编辑。但在我的 CellEditor 实现中,单元格一旦成为焦点就变得可编辑。
这是我的自定义 CellEditor 的代码,
public class ParameterDefinitionEditor
extends AbstractCellEditor
implements TableCellEditor{
private JTable table;
private DefaultTableModel defaultTableModel;
public ParameterDefinitionEditor(DefaultTableModel defaultTableModel,
JTable table) {
super();
this.table = table;
this.defaultTableModel = defaultTableModel;
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setCellEditor(this);
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
if (isSelected) {
// Do some processing.
}
((JTextField)component).setText((String)value);
// Return the configured component
return component;
}
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
任何帮助将不胜感激。谢谢。
I am creating a app in Java. I need to provide additional behavior when editing of a cell in a JTable. So ideally this will happen when the cell loses focus after editing. Depending upon some post processing I might reset the value of the cell. I tried using a a Cell Editor but it is not giving me the desired behavior.
In the default JTable only when I Double click a cell it becomes editable. But in my implementation of CellEditor the cell becomes editable as soon as it comes into focus.
Here is the code for the My custom CellEditor,
public class ParameterDefinitionEditor
extends AbstractCellEditor
implements TableCellEditor{
private JTable table;
private DefaultTableModel defaultTableModel;
public ParameterDefinitionEditor(DefaultTableModel defaultTableModel,
JTable table) {
super();
this.table = table;
this.defaultTableModel = defaultTableModel;
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setCellEditor(this);
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
if (isSelected) {
// Do some processing.
}
((JTextField)component).setText((String)value);
// Return the configured component
return component;
}
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
Any help will be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意,可以通过重写 stopCellEditing() 方法在单元格编辑器中正确执行此操作。
扩展 DefaultCellEditor。这是由 setClickCountToStart() 方法控制的。
我同意另一个建议,即您可能应该将 TableModelListener 添加到 TableModel。尽管根据您的要求,您可能需要考虑使用 Table Cell Listener 。
You can do this right in the cell editor if you desire by overriding the stopCellEditing() method.
Extend the DefaultCellEditor. This is controlled by the setClickCountToStart() method.
I agree with the other suggestion that you should probably be adding the TableModelListener to the TableModel. Although depending on your requirement you may want to consider using the Table Cell Listener.
我认为提供自定义单元格编辑器不能满足您的目的。
如果您想根据用户操作进行一些处理,那么您的表模型应该具有
一组侦听器(实现 TableModelListener)和您的逻辑应该放在
在“tableChanged”方法中。
另请查看有关 Swing 教程的本节:
http://java.sun.com/docs/books/教程/uiswing/components/table.html
I don't think by providing custom cell editor serves your purpose.
If you want to do some processing based on user actions then your table model should have
a set of listeners (that implement TableModelListener) and your logic should be put
in the "tableChanged" method.
Check this section on Swing tutorial as well:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
我通过重写 stopCellEditing 实现了这种类型的行为(我使用 AbstractCellEditor 的自定义实现)
i achieved that type of behaviour by overriding stopCellEditing ( i use a custom implementation of AbstractCellEditor )