JScrollPane 自动将 ViewPort 位置设置为 (0,0) [英] JScrollPane automatically sets ViewPort position to (0,0)

查看:26
本文介绍了JScrollPane 自动将 ViewPort 位置设置为 (0,0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JScrollPane,它的视口是 JPanel 的子类.这个面板是用 BoxLayout 布局的,在用户交互时,组件的水平宽度可以改变.当有足够的组件需要滚动条时,更改组件的水平大小,然后在 JPanel 子类(视口视图)上调用 revalidate() 将总是导致滚动窗格重置为 (0,0).

I have a JScrollPane whose viewport is a subclass of JPanel. This panel is laid out with a BoxLayout and upon user interaction the horizontal width of the components can change. When there are sufficient components such that the scroll bars are needed, changing the horizontal size of the component and then calling revalidate() on the JPanel subclass (the viewport view) will always cause the scroll pane to reset to (0,0).

我想保留当前视图,因此我设置了一个组件侦听器,并在 componentMoved() 上将值存储在 getLocation()(这都在 JPanel 子类或视口视图中).在调用 revalidate() 之后,我立即使用存储的位置(在事件调度线程上)调用 setLocation().但是,这会导致视图位置设置为 (0,0) 的跳动,然后立即跳转到存储的位置.有没有办法阻止 JScrollPane 回到 (0,0)?有没有更好的方法告诉它它的视口视图的大小已经改变?

I would like to keep the current view, so I've set up a component listener and on componentMoved() I store the value at getLocation() (this is all in the JPanel subclass or the viewport view). Immediately after calling revalidate() I call setLocation() with the stored location (on the event dispatch thread). However, that causes jumpiness where the view position is set to (0,0) then jumps immediately to the stored location. Is there a way to stop the JScrollPane from going back to (0,0)? Is there a better way to tell it that the size of it's viewport view has changed?

我试图通过子类化 JScrollBar 并覆盖 setValue() 来找出 (0,0) 的来源并放置一个断点,但它没有被击中(但是当我移动滚轮时)所以我认为它是在内部设置的.有人知道会发生什么吗?

I've tried to figure out where the (0,0) is coming from by subclassing the JScrollBar and overriding setValue() and putting a breakpoint, but it's not being hit there (but is when I move the scroll wheel) so I assume it's being set internally. Does anyone know what might be happening?

推荐答案

尝试另一种方式调用 repaint() 之前的 revalidate() buf 之后的代码.

Try another way to call the code after revalidate() buf before repaint().

scrollPane.getViewport().setViewPosition(new Point(x,y));

或者你可以尝试调用 panel.scrollRectToVisible(Rectangle)

这篇关于JScrollPane 自动将 ViewPort 位置设置为 (0,0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆