将内联 JComponent 插入 JTextPane 中?
我正在开发一个项目,需要能够将 JComponent 与普通文本一起插入到 JTextPane 中。目前,我正在使用 JTextPane.insertComponent(Component) 添加 JComponent,但是,它们似乎是零星添加的,没有真正的组织。我终于发现它会将其插入克拉所在的位置,因此我尝试将其设置在文本末尾。但是,当我执行:
int len = txtConsole.getText().length();
txtConsole.setCaretPosition(len - 1);
我收到一条错误消息,指出该位置无效。是否有一种简单或更好的方法将 JComponent 插入到 JTextPane 最后一行的末尾?
另外,如果上述情况可行,是否有办法更改 JComponent 与文本其余部分内联的位置,例如居中内联?
I'm working on a project that needs to be able to have JComponents inserted into a JTextPane, along with normal text. Currently, I'm using JTextPane.insertComponent(Component) to add the JComponents, however, they seem to be added sporadically with no real organization. I finally figured out that it's inserting it wherever the carat is located, so I tried setting it at the end of the text. However when I perform:
int len = txtConsole.getText().length();
txtConsole.setCaretPosition(len - 1);
I get an error saying that the location is invalid. Is there an easy, or better way to insert JComponents into the end of the JTextPane's last line?
Also, if the above is possible, is there a way to change where the JComponent is located inline with the rest of the text, such as centered inline?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下代码对我有用:
确保您的代码在 EDT 上执行。
如果您还有其他问题,请发布您的 SSCCE 来演示该问题。
The following code works for me:
Make sure your code is executed on the EDT.
If you have further problems then post your SSCCE demonstrating the problem.