弹出窗口中的 JScrollPane 不会以编程方式向上滚动

发布于 2024-09-13 08:11:15 字数 601 浏览 1 评论 0原文

我有一个弹出菜单,可在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-09-20 08:11:15

是的,好吧,找到了解决方法。

我已将问题范围缩小到自定义 JPanel 对象中的 JEditorPane。其内容由程序动态更新。在生成对象时,我通过 setText 方法设置 JEditorPanes 的内容。通过将文本设置为字符串来更新插入符位置,强制滚动窗格向下滚动。

我刚刚将其插入到构造函数中,问题就解决了:

DefaultCaret caret = (DefaultCaret) jEditorPaneContents.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

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:

DefaultCaret caret = (DefaultCaret) jEditorPaneContents.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文