[英]Remove row being edited from JTable
我有一个 JTable 和它旁边的一个调用deleteSelectedRows()
的按钮,它的功能与它听起来的完全一样:
public void deleteSelectedRows() {
int[] selected = jTable.getSelectedRows();
for(int i = selected.length - 1; i >= 0; i--) {
model.removeRow(selected[i]);
}
if(model.getRowCount() < 1) {
addEmptyRow();
}
}
但是如果一个单元格在它(和/或它上面的单元格)被删除时处于被编辑的状态,被编辑的单元格将保留而 rest 离开,如下所示:
然后试图退出编辑抛出了ArrayIndexOutOfBoundsException
,因为第 5 行试图被访问并且表中只剩下一行。
然后我尝试了各种有趣的游戏jTable.getEditingRow()
。 起初,在删除之前添加一个if(selected[i] != editing)
似乎可行,但随后删除编辑单元格上方的行导致了问题。
然后我尝试了这个:
public void deleteSelectedRows() {
int[] selected = jTable.getSelectedRows();
int editing = jTable.getEditingRow();
for(int s : selected) { //PS: Is there a better way of doing a linear search?
if(s == editing) {
return;
}
}
for(int i = selected.length - 1; i >= 0; i--) {
model.removeRow(selected[i]);
}
if(model.getRowCount() < 1) {
addEmptyRow();
}
}
但这不会删除任何东西。 从我散布的println
判断,最后一个要突出显示的单元格(在spam
上有特殊边框)被认为是编辑行的一部分,因此触发了我的提前return
。
所以我真的不在乎解决方案是否涉及修复原始问题——当正在编辑的单元格被删除时古怪的结果——或者这个新问题getEditingRow()
的行为不像我预期的那样,它只是我至少需要其中之一发生。 也就是说,出于学术好奇心,我很想听听这两种解决方案。 提前致谢。
在从 model 中删除任何行之前,尝试包含以下行:
if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}
表格停止编辑有一个通用的解决方案,当您需要支持任意数量的按钮时,它会很方便。
正如霍华德所说,在修改model之前,有必要停止单元格编辑。但也有必要检查单元格是否真正被修改,以避免null指针异常。 这是因为如果此时未编辑表, getCellEditor()方法将返回null :
if (myTable.isEditing()) // Only if it's is being edited
myTable.getCellEditor().stopCellEditing();
...
在某些情况下,单元格编辑器可能会拒绝停止编辑,这种情况可能会发生,即如果您正在使用一些正在等待用户在对话框中输入的复杂编辑器。 在这种情况下,您应该添加一个额外的检查:
if (myTable.isEditing())
if (!myTable.getCellEditor().stopCellEditing()) {
// If your update is user-generated:
JOptionPane.showMessageDialog(null, "Please complete cell edition first.");
// Either way return without doing the update.
return;
}
在您的代码中,您试图仅删除未被编辑的行,但是当单元格编辑器停止编辑时,这也会引发 ArrayOutOfBounds 异常。 最好是在刷新之前停止它。
最后,您似乎还可以在表中设置一个属性:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
如此处所述。
虽然在应用任何更改之前停止任何和所有单元格的编辑是有效的,但这有点像使用大锤敲螺母。 例如,如果正在编辑的单元格不是被删除的单元格,会发生什么情况? 这是您将遇到的下一个问题。 出于这个原因和其他原因,有更好的方法。 首先,使用框架为您完成繁重的工作。 将TableModelListener
附加到您的表 model table.getModel().addTableModelListener()...
然后在您的侦听器实现中捕获删除事件并按如下方式处理:
/**
* Implements {@link TableModelListener}. This fine grain notification tells listeners
* the exact range of cells, rows, or columns that changed.
*
* @param e the event, containing the location of the changed model.
*/
@Override
public void tableChanged(TableModelEvent e) {
if (TableModelEvent.DELETE == e.getType()) {
// If the cell or cells beng edited are within the range of the cells that have
// been been changed, as declared in the table event, then editing must either
// be cancelled or stopped.
if (table.isEditing()) {
TableCellEditor editor = table.getDefaultEditor(ViewHolder.class);
if (editor != null) {
// the coordinate of the cell being edited.
int editingColumn = table.getEditingColumn();
int editingRow = table.getEditingRow();
// the inclusive coordinates of the cells that have changed.
int changedColumn = e.getColumn();
int firstRowChanged = e.getFirstRow();
int lastRowChanged = e.getLastRow();
// true, if the cell being edited is in the range of cells changed
boolean editingCellInRangeOfChangedCells =
(TableModelEvent.ALL_COLUMNS == changedColumn ||
changedColumn == editingColumn) &&
editingRow >= firstRowChanged &&
editingRow <= lastRowChanged;
if (editingCellInRangeOfChangedCells) {
editor.cancelCellEditing();
}
}
}
}
}
在上面的示例中,我将我自己的编辑器指定为表的默认编辑器table.setDefaultRenderer(ViewHolder.class, new Renderer()); table.setDefaultEditor(ViewHolder.class, new Editor());
table.setDefaultRenderer(ViewHolder.class, new Renderer()); table.setDefaultEditor(ViewHolder.class, new Editor());
.
此外,我没有使用特定的视图,而是使用了ViewHolder
。 这样做的原因是为了使表格在其显示的视图方面具有通用性。 这是通用的ViewHolder.class
:
/**
* Holds the view in a table cell. It is used by both the {@link Renderer}
* and {@link Editor} as a generic wrapper for the view.
*/
public static abstract class ViewHolder {
private static final String TAG = "ViewHolder" + ": ";
// the position (index) of the model data in the model list
protected final int position;
// the model
protected Object model;
// the view to be rendered
protected final Component view;
// the views controller
protected final Object controller;
/**
* @param view the view to be rendered
* @param position the position (index) of the data
*/
public ViewHolder(int position,
Object model,
Component view,
Object controller) {
this.position = position;
if (view == null) {
throw new IllegalArgumentException("item view may not be null");
}
if (model == null) {
throw new IllegalArgumentException("model may not be null");
}
this.controller = controller;
this.model = model;
this.view = view;
}
现在,每次调用您的渲染器或编辑器时,构造一个ViewHolder
class 并传入您的视图 / controller / position 等,您就完成了。
这里需要注意的重要一点是,您不必在删除或更改事件发生之前捕获它。 实际上,您应该在 model 更改之后捕获它。 为什么? 好吧,在更改之后您知道发生了什么更改,因为TableModelListener
会告诉您,帮助您确定下一步要做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.