我想为JTable提供多单元格编辑功能:双击仍将编辑所选单元格中的值(标准行为),而右键单击则应打开一个带有“编辑所选单元格”条目的弹出菜单”
当用户点击此菜单条目时,所选范围中的最后一个单元格变为可编辑状态。其他选定的单元格保持选中状态然后他们编写新值,当编辑完成时(通常按Enter键),所有选定的单元格都会得到这个值。
为简单起见,我们假设所有单元格都包含相同的值类型,例如整数。
以下是显示弹出对话框的代码,以便开始使用:
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setCellSelectionEnabled(true);
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
doPop(e);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
doPop(e);
}
}
private void doPop(MouseEvent e) {
MultiEditPopUp menu = new MultiEditPopUp(tblRanges);
menu.show(e.getComponent(), e.getX(), e.getY());
}
});
class MultiEditPopUp extends JPopupMenu {
JMenuItem menuItem;
MultiEditPopUp(JTable table) {
menuItem = new JMenuItem("Edit selected");
menuItem.setAction(new BulkEditAction(table));
add(menuItem);
}
}
class BulkEditAction extends AbstractAction {
private final JTable table;
public BulkEditAction(JTable table) {
this.table = table;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
// TODO: let the user edit the last cell, and then apply to the others
}
}
我怎么能这样做?
答案 0 :(得分:2)
还不太确定问题是什么。基本方法是
我看到的唯一棘手的部分可能是“在最后”检测(因为编辑的生命周期没有过分定义)。一些代码片段
public class BulkEditAction extends AbstractAction {
JTable table;
List selectedCells;
public BulkEditAction(JTable table) {
this.table = table;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
// store, here rows only, refine for cell selection
selectedCells = Arrays.asList(table.getSelectedRows());
final int rowToEdit = // ...
final int columnToEdit = // ...
table.editCellAt(rowToEdit, columnToEdit);
CellEditorListener l = new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
((AbstractCellEditor) e.getSource()).removeCellEditorListener(this);
propagateEditedValue(rowToEdit, columnToEdit);
}
@Override
public void editingCanceled(ChangeEvent e) {
((AbstractCellEditor) e.getSource()).removeCellEditorListener(this);
}
};
table.getCellEditor().addCellEditorListener(l);
}
private void propagateEditedValue(final int row, final int column) {
// need to invoke to be sure that the table has updated itself after
// editingStopped
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// foreach selectedCell (with coordinates selectedRow/-column
table.setValueAt(table.getValueAt(row, column), selectedRow, selectedColumn);
}
});
}
}
答案 1 :(得分:0)
我会扩展JTable来创建MultiCellEditJTable
public class MultiCellEditJTable extends JTable{
public MultiCellEditJTable(){
setColumnSelectionAllowed(true);
getSelectionModel().setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
@Override
public Component prepareEditor(TableCellEditor editor, int row, int column){
Component component = super.prepareEditor(editor, row, column);
if(component instanceof JTextField){
JTextField textField = (JTextField)component;
textField.selectAll();
}
return component;
}
@Override
public void editingStopped(ChangeEvent e){
int editingRow = getEditingRow();
int editingColumn = getEditingColumn();
super.editingStopped(e);
if(1 < getSelectedRowCount() && 1 == getSelectedColumnCount() && editingColumn == getSelectedColumn()){
Object value = getValueAt(editingRow, editingColumn);
Arrays.stream(getSelectedRows()).filter(row->row != editingRow).forEach(row->
setValueAt(value, row, editingColumn)
);
}
}
}
当选择一行的多行并完成编辑时,所有选定的单元格都将设置为编辑产生的值。
作为一个额外的好处,我让编辑器selectAll提供了在选择一系列单元格后能够输入所需值的功能。否则,必须首先对当前值进行退格。