我已经看到了这样做的一些例子,但我仍然无法理解,也无法实现它。
我想做的是在细胞更换(焦点)上,下一个选中的细胞将选择所有文本,准备用户完全改变它..
关于如何做的任何想法?
// //更新 不知何故,我成功地推出了以下课程,但
实施这个 tblLayers.setDefaultEditor(String.class,new Classes.CellEditor());
没有产生任何结果,“尚未支持”。不被抛出..
我应该如何解决这个问题?
import java.awt.Component;
import java.util.EventObject;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
public class CellEditor extends JTextField implements TableCellEditor {
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
// final JTextField ec = (JTextField) editorComponent;
//
// ec.setText((String) value);
//
// // selectAll, so that whatever the user types replaces what we just put there
// ec.selectAll();
//
// SwingUtilities.invokeLater(new Runnable() {
//
// public void run() {
// // make the component take the keyboard focus, so the backspace key works
// ec.requestFocus();
//
// SwingUtilities.invokeLater(new Runnable() {
//
// public void run() {
// // at this point the user has typed something into the cell and we
// // want the caret to be AFTER that character, so that the next one
// // comes in on the RHS
// ec.setCaretPosition(ec.getText().length());
// }
// });
// }
// });
// return editorComponent;
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getCellEditorValue() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isCellEditable(EventObject anEvent) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean shouldSelectCell(EventObject anEvent) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean stopCellEditing() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void cancelCellEditing() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void addCellEditorListener(CellEditorListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void removeCellEditorListener(CellEditorListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
答案 0 :(得分:11)
我想做的是在细胞更换(焦点)上,下一个选中的细胞将选择所有文本,准备用户完全改变它..
解决方案取决于您的确切要求。 JTable有一个渲染器和一个编辑器。
渲染通常只显示单元格中的文本。如果您希望在开始输入时替换文本,则需要做两件事:
a)更改渲染器以显示处于“已选择”状态的文本,以便用户知道键入字符将删除现有文本 b)更改编辑器以在调用时选择所有文本
这种方法相对困难,因为您需要为表中的每种不同数据类型定制一个自定义渲染器(即String,Integer)。
或者另一种方法是在每个单元格获得焦点时自动将其置于编辑模式,因此您只需更改编辑器即可选择文本。
这种方法很简单,你可以这样做:
JTable table = new JTable(data, columnNames)
{
// Place cell in edit mode when it 'gains focus'
public void changeSelection(
int row, int column, boolean toggle, boolean extend)
{
super.changeSelection(row, column, toggle, extend);
if (editCellAt(row, column))
{
Component editor = getEditorComponent();
editor.requestFocusInWindow();
((JTextComponent)editor).selectAll();
}
}
};
答案 1 :(得分:5)
关于
editorComponent
,我在哪里初始化这个变量?
变量editorComponent
是DefaultCellEditor
的字段。
而不是
class CellEditor extends JTextField implements TableCellEditor
考虑
class CellEditor extends DefaultCellEditor
然后你可以这样做:
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
JTextField ec = (JTextField) editorComponent;
if (isSelected) {
ec.selectAll();
}
return editorComponent;
}
附录:根据@ Edoz的建议并在此完整的example中进行了说明,您可以在鼠标单击启动编辑时有选择地重新排队selectAll()
。
JTable table = new JTable(model) {
@Override // Always selectAll()
public boolean editCellAt(int row, int column, EventObject e) {
boolean result = super.editCellAt(row, column, e);
final Component editor = getEditorComponent();
if (editor == null || !(editor instanceof JTextComponent)) {
return result;
}
if (e instanceof MouseEvent) {
EventQueue.invokeLater(() -> {
((JTextComponent) editor).selectAll();
});
} else {
((JTextComponent) editor).selectAll();
}
return result;
}
};
答案 2 :(得分:2)