简体   繁体   English

如何阻止JScrollPane中的滚动条更新?

[英]How to block scrollbars update in JScrollPane?

I have my own custom control inside JScrollPane . 我在JScrollPane有自己的自定义控件。 This is endless (in all directions) board with grid on it. 这是(在所有方向上)无尽的木板,上面有网格。

I have tried various approaches to implement it, including extending JViewport and adding by setViewport , and extending JComponent and adding by setViewportView . 我尝试了各种方法来实现它,包括扩展JViewportsetViewport添加,以及扩展JComponentsetViewportView添加。

All approaches cause some problems with endlessness and negative locations. 所有方法都会带来一些无穷无尽的问题。 Although component can freely draw itself in negative coordinates space, bad Swing API design doesn't not allow to transfer this information to container (only dimension is allowed, ie in positive direction). 尽管组件可以在负坐标空间中自由绘制自身,但是糟糕的Swing API设计不允许将此信息传输到容器(仅允许尺寸,即正方向)。

Finally I came to conclusion, that if I totally break the channel which notifies JScrollPane about it's containment size, which updates scroll bars minimums and maximums, I will be allowed to set minimums and maximums by hand, which will solve the task. 最终我得出结论,如果我完全中断通知JScrollPane包含容量的通道,该通道将更新滚动条的最小值和最大值,那么我将可以手动设置最小值和最大值,这将解决任务。

So, this is the question: how to prevent custom component to notify JScrollPane about it's size? 因此,这是一个问题:如何防止自定义组件将其大小通知JScrollPane What is the simple way of such preventing? 这种预防的简单方法是什么?

I would rather separate logical (model) coordinates and component coordinates/size. 我宁愿将逻辑(模型)坐标和组件坐标/大小分开。 Thus when user increases NORTH direction you should add appropriate change value to the component's height and shift rendering (or components) vertical location down. 因此,当用户增加北向时,应在组件的高度上添加适当的更改值,并向下移动渲染(或多个组件)的垂直位置。

As example I have a model element - Rectangle placed in (0,0). 例如,我有一个模型元素-将Rectangle放置在(0,0)中。 JPanel which paints the rectangle has initial paint point=(10,10). 绘制矩形的JPanel具有初始绘制点=(10,10)。 In fact when we paint we just g.translate(10,10) and then paint the model g.draw(myRectangle); 实际上,当我们绘制时,我们只需g.translate(10,10)然后绘制模型g.draw(myRectangle);

So when user increases NORTH (+10) we just increase the shift g.translate(10,10+10) and increase the JPanel's preferred height to reflect the size change in the JScrollPane . 因此,当用户增加NORTH(+10)时,我们只需增加shift g.translate(10,10+10)并增加JPanel's首选高度即可反映JScrollPane的大小变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM