更改JTable关键操作的行为 [英] Change behavior of JTable key actions
问题描述
我有一个带有可编辑单元格的JTable
.当我单击一个单元格时,它将进入编辑模式.当我使用方向箭头在单元格中移动时,也会发生同样的情况.
现在,我要选择单元格而不是开始编辑,并且仅在按下 Enter 键时才编辑单元格.
I have a JTable
with editable cells. When I click in a cell, it enters edit mode; the same happens when I'm moving through cell using the directional arrows.
Now I want to select the cell instead of start editing, and edit the cell only when the Enter key is pressed.
如果需要任何其他信息,请索取.
If any other information is needed, please just ask for it.
Enter 键的操作
class EnterAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
JTable tbl = (JTable) e.getSource();
tbl.editCellAt(tbl.getSelectedRow(), tbl.getSelectedColumn());
if (tbl.getEditorComponent() != null) {
tbl.getEditorComponent().requestFocus();
}
}
}
现在这是用于向左箭头动作,从中很难推断出其余3个:
Now this is for left arrow action the rest of 3 are not hard to deduce from this one:
class LeftAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
JTable tbl = (JTable)e.getSource();
tbl.requestFocus();
tbl.changeSelection(tbl.getSelectedRow(), tbl.getSelectedColumn() > 0 ? tbl.getSelectedColumn()-1:tbl.getSelectedColumn(), false, false);
if(tbl.getCellEditor()!=null)
tbl.getCellEditor().stopCellEditing();
}
}
这是绑定此操作的方式:
And this is how you bind this actions:
final String solve = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
table.getActionMap().put(solve, new EnterAction());
final String sel = "Sel";
KeyStroke arrow = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(arrow, sel);
table.getActionMap().put(sel, new LeftAction());
哦,我差点忘了,要选择单元格而不是在鼠标单击上进行
Oh,i almost forgot,to select the cell instead of edit on Mouse Click:
public static MouseListener mAdapterTable = new MouseListener()
{
@Override
public void mousePressed(MouseEvent e)
{
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing())
{
tbl.getCellEditor().stopCellEditing();
}
}
@Override
public void mouseClicked(MouseEvent e) {
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing() )
tbl.getCellEditor().stopCellEditing();
}
@Override
public void mouseReleased(MouseEvent e) {
JTable tbl=((JTable)e.getComponent());
if(tbl.isEditing() )
tbl.getCellEditor().stopCellEditing();
}
};
必须将EventListner
添加到表中,如下所示:
The EventListner
must be added to table like so:
table.addMouseListener(mAdapterTable);
推荐答案
使用 键绑定 .大多数外观感觉实现已经将 F2 绑定到表的startEditing
动作,但是您添加了一个不同的绑定:
Use Key Bindings for this. Most Look & Feel implementations already bind F2 to the table's startEditing
action, but you add a different binding:
tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
这将有效地替换先前 Enter 与表的selectNextRowCell
操作的绑定.
This will effectively replace the previous binding of Enter to the table's selectNextRowCell
action.
这篇关于更改JTable关键操作的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!