在JTable的单元格中开始编辑以获得焦点 [英] Start editing in a cell in JTable on gaining focus
问题描述
Java代码:
我的表格中的两列已经定义了单元格编辑器
JComboBox combo = new JComboBox();
//将项目添加到组合框的代码在这里。
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.RIGHT);
TableColumn column = myJTable.getColumnModel()。getColumn(0);
column.setCellEditor(new DefaultCellEditor(combo));
column = myJTable.getColumnModel()。getColumn(1);
column.setCellEditor(new DefaultCellEditor(textField));
我正面临的问题是当焦点移动到表格单元格时,变成自动编辑。所以,当焦点移动到第2列(有一个文本字段作为编辑器)时,插入符号不会出现,除非单元格被双击或用户开始键入。第1列(有一个组合框作为编辑器)的情况与此类似,因为组合框除非单击单元格,否则不会出现。这些行为是违反直觉的,并且对于使用键盘的用户来说是不希望的。:(b / b)
请指出如何解决这个问题的指针。 b
在此先感谢。
-
href =https://stackoverflow.com/a/10067560/230513>示例覆盖
JTable中的
editCellAt()
/ code>有一个DefaultCellEditor
使用JTextField
。 -
您可以将 Space 键绑定到为
JTable $ c $定义的
startEditing
c>:
$ b $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ keyStroke.getKeyStroke(KeyEvent.VK_SPACE,0), startEditing);
I have defined cell editors for the two columns in my table in the following manner:
Java Code:
JComboBox combo = new JComboBox();
//code to add items to the combo box goes here.
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.RIGHT);
TableColumn column = myJTable.getColumnModel().getColumn(0);
column.setCellEditor(new DefaultCellEditor(combo));
column = myJTable.getColumnModel().getColumn(1);
column.setCellEditor(new DefaultCellEditor(textField));
The problem I am facing is that when a focus is moved to a table cell, the cell doesn't become automatically editable. So, when the focus is moved to column 2 (that has a text field as an editor), the caret sign doesn't not appear unless the cell is double-clicked or the user starts typing. Similar is the case for column 1 (that has a combo box as an editor) as here the combo box doesn't appear unless the cell is clicked. These behaviors are counter-intuitive and undesirable for a user operating with the keyboard.:(
Please suggest pointers on how this could be resolved.
Thanks in advance.
This example overrides
editCellAt()
in aJTable
having aDefaultCellEditor
usingJTextField
.You can bind the Space key to the
startEditing
action defined forJTable
:table.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing");
这篇关于在JTable的单元格中开始编辑以获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!