[英]Finding the X and Y coordinates of end of text in JeditorPane
我已经在我正在处理的文本编辑器项目中创建了一个弹出列表作为自动完成功能,并且在JeditorPane组件中键入文本时设置此弹出列表的位置时遇到了问题
这是我的代码(现在列表在框架底部打开):
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();
}
对象“ autoSuggestionPopUpWindow”是JWindow类的实例
我如何找到这一点?
不要使用JEditorPane。 JEditorPane最好用于显示简单的HTML。
相反,我建议您将JTextPane
用于格式化文本,或将JTextArea
用于纯文本。
您可以根据插入符号的位置显示弹出窗口:
int offset = textComponent.getCaretPostion();
然后,获得代表文本符号在文本组件中位置的矩形:
Rectangle location = textComponent.modelToView(offset);
然后,您可以根据矩形设置窗口的位置:
window.setLocation(location.x, location.y + location.height);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.