弹出窗口中的 JScrollPane 不会以编程方式向上滚动
我有一个弹出菜单,可在 JScrollPane 的 JPanel 中显示动态创建的自定义 JPanel 对象。弹出菜单向用户显示推荐,最上面的元素是最相关的推荐。我正在使用 JPopupMenu 显示窗口:
JPanelTemplatePopup jptep = new JPanelTemplatePopup();
JPopupMenu popup = new JPopupMenu();
popup.add(jptep);
popup.show(this, 500, 100);
问题是,我无法使 JScrollPane 滚动到最上面的元素以首先显示它。我尝试过:
- .getViewPort().setViewPosition(new 点(0,0));
- .scrollRectToVisible(firstelement.getBounds());
validate() 之前和之后。无论我做什么,当窗口弹出时,滚动窗格总是停留在同一个位置。
我什至怀疑在显示窗口之前发生的操作被忽略了,所以我从类中创建并调用了一个公共方法,以使窗口在显示后向上滚动。一切都没有改变。
请帮助,
埃姆雷
I have a popup menu that displays dynamically created custom JPanel objects in a JPanel in a JScrollPane. The popup menu displays recommendations to the user and the topmost element is the most relevant recommendation. I am using JPopupMenu to display the window:
JPanelTemplatePopup jptep = new JPanelTemplatePopup();
JPopupMenu popup = new JPopupMenu();
popup.add(jptep);
popup.show(this, 500, 100);
The problem is, I can't make the JScrollPane scroll to the topmost element to display it first. I have tried:
- .getViewPort().setViewPosition(new
Point(0,0)); - .scrollRectToVisible(firstelement.getBounds());
before and after validate()s. No matter what I do, when the window pops up, the scroll pane always stays at the same place.
I have even suspected that the operations that took place before displaying the window were ignored, so I created and called a public method from the class to make the window scroll it up after being displayed. Nothing changed.
Please help,
Emre
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,好吧,找到了解决方法。
我已将问题范围缩小到自定义 JPanel 对象中的 JEditorPane。其内容由程序动态更新。在生成对象时,我通过 setText 方法设置 JEditorPanes 的内容。通过将文本设置为字符串来更新插入符位置,强制滚动窗格向下滚动。
我刚刚将其插入到构造函数中,问题就解决了:
Yeah, well, found a workaround for this.
I have narrowed the problem down to a JEditorPane that was in the custom JPanel objects. Its contents were dynamically updated by the program. While generating the objects, I was setting JEditorPanes' contents by their setText methods. Updating the caret position by setting the text to a string forced the scroll pane to scroll below.
I just inserted this to the constructor and the problem was fixed: