简体   繁体   English

在JeditorPane中查找文本结尾的X和Y坐标

[英]Finding the X and Y coordinates of end of text in JeditorPane

I've created a popup list as an autocomplete feature in text editor project i'm working on, and i'm facing an issue with setting the location of this pop list at the point the text are typed in the JeditorPane component 我已经在我正在处理的文本编辑器项目中创建了一个弹出列表作为自动完成功能,并且在JeditorPane组件中键入文本时设置此弹出列表的位置时遇到了问题

here is my code(right now the list open on the bottom of the frame): 这是我的代码(现在列表在框架底部打开):

    private void showPopUpWindow() {
    autoSuggestionPopUpWindow.getContentPane().add(suggestionsPanel);
    autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
    autoSuggestionPopUpWindow.setSize(tW, tH);
    autoSuggestionPopUpWindow.setVisible(true);

    int windowX =0;
    int windowY =0;

    windowX = container.getX() + textField.getX() + 5;
    if (suggestionsPanel.getHeight() > autoSuggestionPopUpWindow.getMinimumSize().height) {
        windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getMinimumSize().height;
    } else {
        windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getHeight();
    }


    autoSuggestionPopUpWindow.setLocation(windowX, windowY);
    autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
    autoSuggestionPopUpWindow.revalidate();
    autoSuggestionPopUpWindow.repaint();

}

the object "autoSuggestionPopUpWindow" is a instance of JWindow class 对象“ autoSuggestionPopUpWindow”是JWindow类的实例

how can i find this point? 我如何找到这一点?

Don't use a JEditorPane. 不要使用JEditorPane。 A JEditorPane is best used to display simple HTML. JEdi​​torPane最好用于显示简单的HTML。

Instead I would suggest you use a JTextPane for formatted text or a JTextArea for plain text. 相反,我建议您将JTextPane用于格式化文本,或将JTextArea用于纯文本。

You can display the popup based on the location of the caret: 您可以根据插入符号的位置显示弹出窗口:

int offset = textComponent.getCaretPostion();

Then you get the Rectangle that represents the location of the caret in the text component: 然后,获得代表文本符号在文本组件中位置的矩形:

Rectangle location = textComponent.modelToView(offset);

Then you can set the location of the window based on the rectangle: 然后,您可以根据矩形设置窗口的位置:

window.setLocation(location.x, location.y + location.height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM