JTable的默认行为是在您开始输入时附加到内容,并在单击时将插入符号放在单击的位置。我希望这两件事的行为都能改变,因此当我编辑单元格时,通过键入或单击然后键入来替换内容。然而,当我单击一个单元格然后更改插入位置时,我希望内容保留,以便我可以更改它。
我知道如何在单元格编辑时选择所有内容,方法是将单元格编辑器替换为在SwingUtilities.invokeLater中选择所有内容的单元格编辑器(请参阅elsewhere),但这会导致输入行为中断。当我这样做并开始键入单元格时,首先将键入的字符附加到字符串,然后选择它(但选择是不可见的!),当键入另一个字符时,内容将被替换。
在高亮显示(但不是编辑)的单元格中输入时是否可以立即替换内容,但在单击单元格时选择全部?
以下是我用于CellEditor的代码:
public class TextFieldCellEditor extends JTextField implements TableCellEditor
{
private CellEditorListener cellEditorListener = null;
private boolean isInteger = false;
private Object oldValue;
// Start editing
@Override
public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int column)
{
Color color2 = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
super.setBackground(color2 != null && (row & 1) == 1? color2 : table.getBackground());
super.setForeground(table.getForeground());
super.setBorder(DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder"));
super.setText(obj.toString());
isInteger = obj instanceof Integer;
if (isInteger)
{
super.setHorizontalAlignment(SwingConstants.RIGHT);
oldValue = obj;
}
// SwingUtilities.invokeLater(new Runnable()
// {
// public void run()
// {
// TextFieldCellEditor.this.selectAll();
// }
// });
return this;
}
// Retrieve e dited value
@Override
public Object getCellEditorValue()
{
if (isInteger)
{
// Try to convert to integer. If input is invalid, revert.
try
{
return new Integer(super.getText());
}
catch (NumberFormatException e)
{
return oldValue;
}
}
return super.getText();
}
@Override
public boolean isCellEditable(EventObject e)
{
return true;
}
@Override
public boolean shouldSelectCell(EventObject e)
{
return true;
}
@Override
public boolean stopCellEditing()
{
cellEditorListener.editingStopped(new ChangeEvent(this));
return true;
}
@Override
public void cancelCellEditing()
{
cellEditorListener.editingCanceled(new ChangeEvent(this));
}
@Override
public void addCellEditorListener(CellEditorListener celleditorlistener)
{
cellEditorListener = celleditorlistener;
}
@Override
public void removeCellEditorListener(CellEditorListener celleditorlistener)
{
if (cellEditorListener == cellEditorListener) cellEditorListener = null;
}
}
答案 0 :(得分:2)
在getTableCellEditorComponent()
实施中,添加以下内容:
if (isSelected) {
this.selectAll();
}
顺便说一句,为什么不延长AbstractCellEditor
或DefaultCellEditor(JTextField textField)
?另请参阅How to Use Tables: Using Other Editors。
答案 1 :(得分:0)
我能找到的最简洁的解决方案是覆盖JTable的editCellAt并告知CellEditor如何触发编辑:
@Override
public boolean editCellAt(int row, int column, EventObject e) {
cellEditor.setKeyTriggered(e instanceof KeyEvent);
return super.editCellAt(row, column, e);
}
这是相关的CellEditor代码:
public class MyCellEditor extends DefaultCellEditor {
private boolean keyTriggered;
public MyCellEditor() {
super(new JTextField());
final JTextField textField = (JTextField) getComponent();
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (!keyTriggered) {
textField.selectAll();
}
}
});
}
});
}
public void setKeyTriggered(boolean keyTriggered) {
this.keyTriggered = keyTriggered;
}
@Override
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
final JTextField textField = (JTextField)
super.getTableCellEditorComponent(table, value, isSelected, row, column);
textField.selectAll();
return textField;
}
}