如何从JTextPane获取选择 [英] How to get selection from JTextPane
问题描述
我想找出选择了JTextPanel文本的哪一部分.试图调用JTextPane.getSelectionStart()
和JTextPane.getSelectionEnd()
,但是它们总是返回与当前插入符号位置相同的值.
我的问题是什么?
I want to find out which part of JTextPanel text is selected. Tried to call JTextPane.getSelectionStart()
and JTextPane.getSelectionEnd()
, but they always return same value that is equal to current caret position.
What is my problem with that?
如果能获得当前的选择,我将不胜感激.
I would be thankful for any code exapmle that gets current selection.
推荐答案
我发现了我的问题-那是一个自定义的FocusListener
,它在得到keyTyped
事件之前更改了JTextPane的内容(因此删除了选择).
I found my problem - that was a custom FocusListener
which was changing JTextPane content (and so dropping the selection) before I got the keyTyped
event.
无论如何,谢谢大家的例子和评论!
Anyway, thanks everyone for examples and comments!
这篇关于如何从JTextPane获取选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!