java 使 JTable 单元格可编辑 - 但*不能*通过双击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1268201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:52:53  来源:igfitidea点击:

Making a JTable cell editable - but *not* by double clicking

javaswingjtabletablecelleditor

提问by finnw

I am trying to add a column to a JTablewith the following behaviour (similar to Windows Explorer and similar shells):

我正在尝试向JTable具有以下行为的a 添加一列(类似于 Windows 资源管理器和类似的外壳):

  • The cell can be clicked once to select it, as usual.
  • The cell can be double-clicked to perform a separate action (launching an external program.)
  • The cell value (a string) can still be edited, by single-clicking a second time (after a pause) or by pressing F2when the cell is highlighted.
  • 可以像往常一样单击该单元格一次以将其选中。
  • 可以双击该单元格以执行单独的操作(启动外部程序。)
  • 仍然可以编辑单元格值(字符串),方法是再次单击(暂停后)或F2在单元格突出显示时按下。

Double-clicking must nottrigger editing of the cell, but I would like to leave any other default editing triggers operational if possible.

双击不能触发单元格的编辑,但如果可能,我想保留任何其他默认编辑触发器的可操作性。

I have tried adding a MouseListenerto the table, and consuming all MouseEvents, but this does not work - if I return truefrom isCellEditable()then my listener never receives any click events but if I return falsethen F2no longer works.

我曾尝试向MouseListener表中添加 a并消耗所有MouseEvents,但这不起作用 - 如果我从那时返回trueisCellEditable()我的侦听器永远不会收到任何点击事件,但如果我返回,falseF2不再起作用。

Can this be achieved using only event listeners? I would prefer not to mess with the PLAF functions if I can avoid it.

这可以仅使用事件侦听器来实现吗?如果可以避免的话,我宁愿不要弄乱 PLAF 功能。

采纳答案by Peter

You will have to make your own cellEditor and ovveride

您将不得不制作自己的 cellEditor 和 ovveride

public boolean isCellEditable( EventObject e )

You can distinguish between single and double click with the clickCount on the eventObject

您可以使用 eventObject 上的 clickCount 区分单击和双击

If its a single Click and its on a selected cell you can return true otherwise return false;

如果它是一次单击并且它在选定的单元格上,则可以返回 true,否则返回 false;

retrieve row and column with

检索行和列

int row = ( (JTable) e.getSource() ).rowAtPoint(e.getPoint());
int column = ( (JTable) e.getSource() ).columnAtPoint(e.getPoint());

to enable F2 you can add custom inputMap en actionMap entries

要启用 F2,您可以添加自定义 inputMap en actionMap 条目

similar too
table.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "doMyArrowDown");
table.getTable().getActionMap().put("doMyArrowDown", new ArrowDownAction()); 

and from your action you can then fire the cellediting yourself

然后从你的动作中你可以自己启动细胞编辑

table.editCellAt(row, column );

回答by camickr

The DefaultCellEditor has a setClickCountToStart() method to control mouse clicks for editing. The default is 2. Changing this will have no effect on F2 functionality.

DefaultCellEditor 有一个 setClickCountToStart() 方法来控制鼠标点击进行编辑。默认值为 2。更改此值不会影响 F2 功能。

Therefore you can set editing to be a triple click.

因此,您可以将编辑设置为三次单击。

Not sure exactly how to handle two single clicks to start editing but I guess you would use a Timer to keep track of the first click and then do the editing if the second single click is within you time interval.

不确定如何处理两次单击以开始编辑,但我想您会使用计时器来跟踪第一次单击,然后如果第二次单击在您的时间间隔内进行编辑。

回答by finnw

I have solved this by wrapping the existing CellEditorwith a Proxyand intercepting calls to isCellEditable, returning false for all mouse events and delegating all other calls to the original CellEditor.

我已经解决了这个问题,方法是CellEditor用 a包装现有的Proxy并拦截对 的调用isCellEditable,为所有鼠标事件返回 false 并将所有其他调用委托给原始CellEditor.

This is slightly more complex than camickr's solution but works for all editors (I have 4 in all.)

这比 camickr 的解决方案稍微复杂一些,但适用于所有编辑器(我总共有 4 个。)