如何在Scroll Pane包装文本窗格时阻止JScrollPane箭头键处理移动插入符号 [英] How to prevent JScrollPane arrow key handling from moving caret when Scroll Pane wraps Text Pane

查看:187
本文介绍了如何在Scroll Pane包装文本窗格时阻止JScrollPane箭头键处理移动插入符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:

我需要一个可滚动的JTextPane。用户可以键入该文本窗格,或者可以将文本插入到未由用户键入的文本窗格中。想想像IM窗口。尽管窗口必须是可滚动的,以允许用户查看先前键入的文本,但是插入符应该永远不会从文本末尾的位置移动。用户输入的任何文本将始终显示在最后。

I need a scrollable JTextPane. The user may type into this text pane, or text may be inserted into it that is not typed by the user. Think something like an IM window. Although the window must be scrollable to allow the user to review text previously typed, the caret should never move from its position at the end of the text. Any text entered by the user will always appear at the end.

在JTextPane中,当用户使用滚动条滚动时,插入符号不会移动。视口已调整。但是,当用户按下向上和向下箭头键时,JTextPane插入符随之移动(无论窗口是否滚动)。

In JTextPane, when the user scrolls with the scroll bar, the caret does not move. The viewport is adjusted. However, when the user presses the up and down arrow keys, the JTextPane caret moves with it (whether the window scrolls or not).

我想要的是向上箭头键与使用鼠标向上移动滚动条的功能相同。箭头键应该与插入符号无关。

What I want is that an up arrow key would function the same as moving the scroll bar up with the mouse. The arrow keys should have nothing to do with the caret.

我尝试了以下方法,但没有成功:
1)添加No-op对我的文本窗格类的Keymap的操作(使用JTextPane.addKeymap()和Keymap.addActionForKeyStroke())。这会阻止插入符移动,但会阻止操作传递到滚动窗格以滚动视图。
2)从我的文本窗格类的键映射中删除箭头键。这会影响我的应用程序中的所有JTextPanes,这不是我想要的。

I have tried the following approaches, without success: 1) add a "No-op" action to the Keymap for my text pane class (using JTextPane.addKeymap() and Keymap.addActionForKeyStroke()). This stops the caret from moving but prevents the action from being passed on to the Scroll Pane to scroll the view. 2) remove the arrow keys from the keymap for my text pane class. This affects all JTextPanes in my application which is not what I want.

我想要的是向我的TextPane键映射添加一个动作,它只是调用ScrollPane动作为Up和向下箭头。

What I want is to add an action to my TextPane keymap that simply calls the ScrollPane action for Up and Down arrow.

实现这个目标的最佳方法是什么?

What is the best way to accomplish this?

我可能会遇到一个KeyListener (它在键映射之前接收键击)以捕获这些键,然后手动实现滚动。
但这似乎要求我计算字体大小等。有更简单的方法吗?

A possibility that occurs to me is to implement a KeyListener (which receives the key stroke before the keymap) to trap these keys and then to implement the scrolling by hand. But this would seem to require me to compute font sizes, etc. Is there an easier way?

理想的事情是如果有某种方法可以将插入符号锚定到文本的末尾。

The ideal thing would be if there was some way to "anchor" the caret to whatever the end of the text was.

推荐答案

您将不得不修改 KeyBindings

试试这个开头

InputMap im = textArea.getInputMap(WHEN_FOCUSED);
ActionMap am = textArea.getActionMap();

am.get("caret-down").setEnabled(false);
am.get("caret-up").setEnabled(false);

既然你有这个工作,你需要担心所有这些

Now that you have that working, you need to worry about all these

selection-up = shift pressed UP
caret-next-word = ctrl pressed RIGHT
selection-previous-word = shift ctrl pressed LEFT
selection-up = shift pressed KP_UP
caret-down = pressed DOWN
caret-previous-word = ctrl pressed LEFT
caret-end-line = pressed END
selection-page-up = shift pressed PAGE_UP
caret-up = pressed KP_UP
delete-next = pressed DELETE
caret-begin = ctrl pressed HOME
selection-backward = shift pressed LEFT
caret-end = ctrl pressed END
delete-previous = pressed BACK_SPACE
selection-next-word = shift ctrl pressed RIGHT
caret-backward = pressed LEFT
caret-backward = pressed KP_LEFT
selection-forward = shift pressed KP_RIGHT
delete-previous = ctrl pressed H
unselect = ctrl pressed BACK_SLASH
insert-break = pressed ENTER
selection-begin-line = shift pressed HOME
caret-forward = pressed RIGHT
selection-page-left = shift ctrl pressed PAGE_UP
selection-down = shift pressed DOWN
page-down = pressed PAGE_DOWN
delete-previous-word = ctrl pressed BACK_SPACE
delete-next-word = ctrl pressed DELETE
selection-backward = shift pressed KP_LEFT
selection-page-right = shift ctrl pressed PAGE_DOWN
caret-next-word = ctrl pressed KP_RIGHT
selection-end-line = shift pressed END
caret-previous-word = ctrl pressed KP_LEFT
caret-begin-line = pressed HOME
caret-down = pressed KP_DOWN
selection-forward = shift pressed RIGHT
selection-end = shift ctrl pressed END
selection-previous-word = shift ctrl pressed KP_LEFT
selection-down = shift pressed KP_DOWN
insert-tab = pressed TAB
caret-up = pressed UP
selection-begin = shift ctrl pressed HOME
selection-page-down = shift pressed PAGE_DOWN
delete-previous = shift pressed BACK_SPACE
caret-forward = pressed KP_RIGHT
selection-next-word = shift ctrl pressed KP_RIGHT
page-up = pressed PAGE_UP

这篇关于如何在Scroll Pane包装文本窗格时阻止JScrollPane箭头键处理移动插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆