0

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

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

how can i find this point?

4

1 回答 1

4

不要使用 JEditorPane。JEdi​​torPane 最适合用于显示简单的 HTML。

相反,我建议您将 aJTextPane用于格式化文本或 aJTextArea用于纯文本。

您可以根据插入符号的位置显示弹出窗口:

int offset = textComponent.getCaretPostion();

然后你得到代表文本组件中插入符号位置的 Rectangle:

Rectangle location = textComponent.modelToView(offset);

然后可以根据矩形设置窗口的位置:

window.setLocation(location.x, location.y + location.height);
于 2015-07-31T15:00:08.447 回答