【问题标题】:caret position of an embedded swing component in a JTextPaneJTextPane 中嵌入式摆动组件的插入符号位置
【发布时间】:2013-05-17 15:38:15
【问题描述】:

我有一个 JTextPane,它在使用 HTML 标记时使用自定义 HTMLEditorKit 嵌入一个摆动组件。当我单击此组件时,我希望将插入符号位置更新为该组件的位置。我可以在控件中获取焦点事件,但我不知道如何将插入符号位置设置为该组件的位置。有人知道怎么做吗?

【问题讨论】:

标签: java swing jtextpane caret htmleditorkit


【解决方案1】:

您可以获得被点击组件的父级并使用parent.getBounds()。返回的矩形是JTextPane 中的位置。使用JTextPaneviewToModel()方法获取坐标位置的插入符号位置。

【讨论】:

    【解决方案2】:

    您需要 JTextPane 内容中嵌入组件的字符位置;如果没有更多细节,就无法猜测您将如何确定。但是,一旦知道了组件的字符位置,就很简单了:

    myTextPane.setCaretPosition(characterIndexOfEmbeddedComponent);
    

    见:http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2023-03-15
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多