Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a Swing component 'displayable'?

Is there a way (e.g., via an event?) to determine when a Swing component becomes 'displayable' -- as per the Javadocs for Component.getGraphics?

The reason I'm trying to do this is so that I can then call getGraphics(), and pass that to my 'rendering strategy' for the component.

I've tried adding a ComponentListener, but componentShown doesn't seem to get called. Is there anything else I can try?

Thanks.

And additionally, is it OK to keep hold of the Graphics object I receive? Or is there potential for a new one to be created later in the lifetime of the Component? (e.g., after it is resized/hidden?)

like image 865
Joe Freeman Avatar asked May 18 '09 16:05

Joe Freeman


People also ask

What does @component do in Java?

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them.

What is a Swing explain the component hierarchy of swings?

Swing is a set of program component s for Java programmers that provide the ability to create graphical user interface ( GUI ) components, such as buttons and scroll bars, that are independent of the windowing system for specific operating system . Swing components are used with the Java Foundation Classes ( JFC ).


1 Answers

Add a HierarchyListener

public class MyShowingListener {
private JComponent component;
public MyShowingListener(JComponent jc) { component=jc; }

public void hierarchyChanged(HierarchyEvent e) {
    if((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED)>0 && component.isShowing()) {
        System.out.println("Showing");
    }
}
}

JTable t = new JTable(...);
t.addHierarchyListener(new MyShowingListener(t));
like image 157
KitsuneYMG Avatar answered Oct 01 '22 11:10

KitsuneYMG