如何检测JPanel子组件绘制事件?

时间:2017-08-04 08:32:06

标签: java jpanel paint

我有一个JPanel,我在其中放置组件。每次重新绘制子组件(甚至嵌套)时,我都需要得到通知。是否有一个事件,我可以在组件添加到JPanel时注册?

我的目的是在JPanel的右边缘和底边绘制一个滚动条,但是当重新绘制子组件时,它会重叠滚动条并使滚动条的一部分不可见。我想在组件刷新其视图时重绘滚动条,使其保持在滚动条后面。

为了使它更清晰一点,这两个是UI的截图。

When components overlay the scroll bar When scroll bar overlays the components

第一张图片不正确但只有当我用鼠标悬停组件时才会发生这种情况。否则,它看起来不错。右侧的图像是装入容器的时间。我希望组件始终显示在滚动条后面,但我无法检测何时重新绘制特定组件。

1 个答案:

答案 0 :(得分:0)

For those who encounter similar problem like this,

I've found the answer within Swing's own painting mechanism. If you want to receive component paint events from a container's child, override isPaintingOrigin() method.

@Override
protected boolean isPaintingOrigin() {
    return true;
}

Only this will be sufficient and painting event will be called by the underlying container.

相关问题