JTable right-click copy/paste menu to copy cell data on one click(JTable右键复制/粘贴菜单一键复制单元格数据)
问题描述
我创建了我的 JPopupMenu
.当我右键单击单元格时,它会出现在我的 JTable
上.但是,我无法复制单元格中的数据,除非我首先双击然后突出显示数据,然后右键单击当前单元格以外的任何位置以显示我的弹出菜单和复制选项.
I created my JPopupMenu
. It appears on my JTable
when I right click on a cell. However, I cannot copy the data in the cell unless I first double click and then highlight the data, and then right click anywhere but this current cell to show my popup menu and copy option.
我想复制单元格中的数据,而不必双击单元格并进入单元格编辑模式,然后我需要选择数据.
I would like to copy the data in a cell without having to double click on a cell and enter into cell edit mode where I then need to select the data.
我该怎么做?
这是我在 MouseListener
中为我的 JTable
提供的代码,在 mouseReleased()
和 mousePressed()
代码>.
Here's the code that I have in my MouseListener
for my JTable
, in mouseReleased()
and mousePressed()
.
推荐答案
两件事...
- 我不确定您希望
DefaultEditorKit.CopyAction
和DefaultEditorKit.PasteAction
如何与JTable
一起使用,这些都是假设与JTextComponents
... 一起使用 JTable
只会在左键按下(或键盘导航更改)时突出显示该行,默认情况下鼠标右键不会执行此操作.
- I'm not sure how you expect a
DefaultEditorKit.CopyAction
andDefaultEditorKit.PasteAction
to work with aJTable
, these are suppose to be used withJTextComponents
... - The
JTable
will only highlight the row on a left button press (or key board navigation change), a right mouse button click doesn't do this by default.
现在,我想使用 JTable
的组件弹出支持,但这似乎会在检测到弹出触发后消耗所有鼠标事件,这使得(几乎)无法突出显示鼠标右键单击行/列.
Now, I wanted to use the JTable
's component popup support, but this seems to consume all mouse events once it detects a popup trigger, which makes it (near to) impossible to highlight the row/column on a right mouse click.
相反,我最终在 MouseListener
中添加了一个 highlight
方法,该方法突出显示有问题的行/列,然后触发弹出窗口.
Instead, I ended up adding a highlight
method into my MouseListener
which highlights the row/column in question and then triggers the popup.
我这样做的原因是,与复制和粘贴相关的Action
除了表格之外没有任何概念,所以他们不知道点击了哪一行/哪一列开.
The reason I did it this way, is the Action
's associated with copying and pasting have no concept of anything other than the table, so they don't know what row/column was clicked on.
这允许这些操作专注于单独担心选择.
This allows these actions to focus on worrying about the selection alone.
内容通过自定义可转移直接复制到系统剪贴板,它保持单元格的原始类型,这意味着您不需要在粘贴时重新构建对象.
Content is copied directly to the system clipboard via a custom transferable, which maintains the cell's original type, which means you don't need to reconstruct the object when it's pasted.
这篇关于JTable右键复制/粘贴菜单一键复制单元格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!