What Event Occurs when a Component is Shown?

Or more precisely, what event occurs to let you know that a component has been layed out (given a size)?

I need to manipulate an image on a button after the button has been set to a SIZE (scaling to fit if you want to know). But, this does not occur until the layout manager lays out the container.

I can't seem to find the event for that. I used to know this but my feeble brain forgot along the way. ComponentShown does not occur in view initialization, only specific calls to setVisible(true). That would have been a good place for it Swing Team.

[569 byte] By [GUI-Majiciana] at [2007-10-2 14:08:11]
# 1
See componentListener + componentAdapter
tjacobs01a at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...
# 2

You are thinking ComponentShown and that does NOT get called unless you explicitly call setVisible(true) on the component.

I think I figured it out anyway. Its HierarchyListener which does get called when a component's hierarchy is made visible which includes the component itself. Thats the one I forgot because its not intuitive.

Thanks.

GUI-Majiciana at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...
# 3

Sorry, my mistake. Hierarchy changes do not fire at the right time either. Component size is still [0, 0]. Seems there is no way to know when a component has been layed out. Thats a big missing piece in AWT/Swing if you ask me.

For example, you cannot maintain the JButton interface's setIcon(...) to set a scaled image onto a JButton (by overriding it and scaling the image when called). Thats bad. I guess you could do the paintComponent thing but thats an ugly non-generic solution plus that probably wouldn't work in a dynamic layout when setIcon(..) gets called and the layout resizes the button to match the image which is the opposite of what we want. Whoops.

Problem unsolved.

GUI-Majiciana at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...
# 4
How about ComponentListener.componentResized?
tjacobs01a at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...
# 5

> Sorry, my mistake. Hierarchy changes do not fire at

> the right time either. Component size is still [0,

> 0]. Seems there is no way to know when a component

> has been layed out. Thats a big missing piece in

> AWT/Swing if you ask me.

>

> I guess you could do the

> paintComponent thing but thats an ugly non-generic

> solution plus that probably wouldn't work in a

> dynamic layout when setIcon(..) gets called and the

> layout resizes the button to match the image which is

> the opposite of what we want. Whoops.

The layout manager doesn't care one bit what you draw in paintComponent. In fact, the graphics passed to paintComponent will generally have been clipped to the component's bounds. Not that it really matters; scaling in paintComponent is still a bad idea.

DaanSa at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...
# 6

ComponentListener.componentResized? Hadn't thought of that one, that might work. I'll try it.

But so far I have only been able to get around the problem by hard coding the size to scale the image to. This only works with static layouts like null and Borland's XYLayout of course. I wouldn't call that a good solution.

Scaling in paintComponent a bad idea? If we were dealing with large high res images I would agree, would cause delay in painting, but our scaling is so fast its imperceptible (250 ms average).

GUI-Majiciana at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...
# 7

> Scaling in paintComponent a bad idea? If we were

> dealing with large high res images I would agree,

> would cause delay in painting, but our scaling is so

> fast its imperceptible (250 ms average).

I'm not so much worried about delaying the EDT, as about general loss in performance due to the fact that paintComponent get's called way more often then setIcon. And I do think that delaying the EDT for 250 milliseconds is something the user might notice.

DaanSa at 2007-7-13 12:19:38 > top of Java-index,Security,Event Handling...