Jtextpane 单击创建弹出菜单效果选择不符合预期

发布于 2024-10-04 06:30:35 字数 743 浏览 1 评论 0原文

我有一个位于 JFrame 中的 JTextPane,其中有一个通过 JTextPane.setComponentPopupMenu 方法分配给 JTextPane 的弹出菜单。

我想为 JTextPane 提供“类似 Word”的弹出行为。我的意思是,如果您在当前文本选择之外单击鼠标右键,插入符号将重新定位到您右键单击的位置,并禁用影响文本选择的菜单选项(例如剪切、复制或粗体)。如果您右键单击当前的文本选择,将出现弹出窗口,其中包含启用启用文本选择的选项,文本选择将保留,并且插入符号不会移动。

问题是我似乎找不到在哪里可以放置处理选择更改的代码。我尝试过:

  • 使用在弹出窗口可见之前触发的“PopupMenuWillBecomeVisible”事件。传递到此方法的事件不包含任何鼠标事件信息,因此我无法使用 viewtomodel 来找出如何修改选择。我可以使用 MouseInfo,但这充其量似乎是可疑的。
  • 在 JTextPane 或 JFrame 中使用 MousePressed/MouseReleased 事件。显然,当触发弹出菜单时,这两个事件都不会被调用。事实上,我仍然无法确定弹出菜单的父组件是什么。 (我确实读到,在Windows中“MouseReleased”是弹出触发器,而在其他系统中“MousePressed”是触发器。我尝试了两者,但都不起作用)。

所以,我想问题是我似乎找不到一个地方来放置在弹出菜单变得可见之前调用的代码,但知道触发弹出菜单的 mouseEvent。我一定在这里遗漏了一些东西。

I have a JTextPane sitting in a JFrame, with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.

I want to give the JTextPane a "Word-like" popup behavior. By that I mean, if you right click outside of your current text selection, the caret will reposition to where you right clicked, with menu options that affect a text selection (such as cut, copy, or bold) disabled. If you right click within your current text selection, the popup will appear with options that effect text selection enabled, the text selection will persist, and the caret will not move.

The problem is I cannot seem to find where I can put the code that handles the selection change. I tried:

  • Using the "PopupMenuWillBecomeVisible" event which is triggered before a popup becomes visible. The event passed into this method does not contain any mouse event information so there is no way for me to use viewtomodel to find out how to modify the selection. I could use MouseInfo but that seems dubious at best.
  • Using MousePressed/MouseReleased events in the JTextPane or JFrame. Apparently, neither of these events are invoked when a popup menu is triggered. In fact, I still can't determine what the parent component of my popup menu is. (I did read that in windows "MouseReleased" is the popup trigger, while in other systems "MousePressed" is the trigger. I tried both and neither worked).

So, I guess the problem is that I can't seem to find a place to put code where it would be called before the popup menu becomes visible, but has awareness of the mouseEvent that triggered the popup menu. I must be missing something here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2024-10-11 06:30:35

通过 JTextPane.setComponentPopupMenu 方法将弹出菜单分配给 JTextPane。

您可以使用基于您自己的自定义 MouseListener 来显示弹出窗口的旧方法。

请参阅 Swing 教程中有关调出弹出菜单。现在您可以访问 MouseEvent,以便将该点转换为文档中的点,以便您知道在选定或未选定的文本上单击的位置。

with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.

You can use the older approach of displaying the popup based on your own custom MouseListener.

See the section from the Swing tutorial on Bringing Up a Popup Menu. Now you have access to the MouseEvent so you can convert that point to a point in the Document so you know where the click was made, on selected or unselected text.

你穿错了嫁妆 2024-10-11 06:30:35

DefaultEditorKit 类

该类有一个 嵌套类,用于管理编辑器文本中的基本简单操作,例如复制、粘贴...。例如

 JPopupMenu namePopMenu = new JPopupMenu();
 JMenuItem copy = new JMenuItem("copy");
 copy.addActionListener(new DefaultEditorKit.CopyAction());
 namePopMenu.add(copy);
 textArea.setComponentPopupMenu(namePopMenu);

DefaultEditorKit class

This class have a nested classes that manage the basic a simple actions like copy, paste... in editor text. E.G

 JPopupMenu namePopMenu = new JPopupMenu();
 JMenuItem copy = new JMenuItem("copy");
 copy.addActionListener(new DefaultEditorKit.CopyAction());
 namePopMenu.add(copy);
 textArea.setComponentPopupMenu(namePopMenu);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文