简体   繁体   中英

How to detect JPanel child component paint event?

I have a JPanel where I place components in it. I need to get notified every time when a sub component (even nested) is repainted. Is there an event for it that I can register when a component is added to the JPanel?

My purpose is to draw a scroll bar on the right and bottom edges of the JPanel but when the sub component is redrawn, it overlaps the scroll bars and makes a part of scroll bar invisible. I want to redraw the scroll bar when a component refreshes its view so it stays behind the scroll bar.

To make it a little bit more clear, this two are the screenshots of the UI.

当组件覆盖滚动条时 当滚动条覆盖组件时

The first image is incorrect but this happens only if I hover the components with the mouse. Otherwise, it's looking good. The image on the right is when the container is loaded. I want the components always appear behind the scroll bar but I just cannot detect when a specific component is repainted.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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