ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-当滚动窗格包装文本窗格时,如何防止JScrollPane箭头键处理移动插入符号

2019-10-10 03:07:11  阅读:13  来源: 互联网

标签:jtextpane java swing jscrollpane caret



我有以下要求:

我需要一个可滚动的JTextPane.用户可以在此文本窗格中键入,也可以将用户未键入的文本插入其中.想像即时消息窗口.尽管窗口必须是可滚动的,以允许用户查看先前键入的文本,但插入符号绝不能从其在文本末尾的位置移开.用户输入的任何文本将始终显示在末尾.

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

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

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

我想要的是向TextPane键盘映射添加一个动作,该动作只是为向上和向下箭头调用ScrollPane动作.

做到这一点的最佳方法是什么?

我想到的一种可能是实现KeyListener(该监听器在键映射之前接收键击)以捕获这些键,然后手动实现滚动.
但这似乎需要我计算字体大小等.是否有更简单的方法?

理想的事情是,如果有某种方法可以将插入符“锚定”到文本的结尾.

解决方法:

您将需要修改KeyBindings

试试这个开始

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

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

现在您已经可以工作了,您需要担心所有这些

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


标签:jtextpane,java,swing,jscrollpane,caret

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有