如何从JTextPane获取选择 [英] How to get selection from JTextPane

查看:98
本文介绍了如何从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屋!

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