Re: Windows Decoration Listeners? - Problem Solved



Knute Johnson wrote:
Michael Dunn wrote:
there doesn't seem to be any way to see actions on the title bar on a JFrame.

JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame f = new JFrame();

f.addMouseListener(new MouseAdapter(){

That doesn't work in my WinXP SP2 box running Sun's 1.6 JDK.

JFrame.setDefaultLookAndFeelDecorated isn't guaranteed to work on all PL&Fs. Indeed it is only supposed to be for cross-platform PL&Fs (that sucks, but then I haven't seen any patches submitted to fix it). Anyway, I don't think that is the problem in this case.

Consider JComboBox. Add a focus listener to that, and you don't receive the events. They all go to the contained text field.

So, what you need to do is add a listener onto the component, onto its children, and their children, and so on. You also need to add container listener to check for any components added or taken away.

So why did it work in the first place? Mouse events are different from other events. They bubble up if no mouse listeners are present. So adding mouse listeners changes the behaviour of components. An absolutely criminal piece of design. The quoted code only works by chance.

It has broken because internally Swing adds mouse listeners differently in 1.6 fcs. Swing in this case works correctly by adding listeners to the components it wants to listen to(!).

So for 1.6 fcs the correct code works!

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4854950

1.6.0u1 backs out this fix, so the broken code works again, but the correct code doesn't!

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6491619

There a couple of ways of tackling the problem that are both legitimate and work.

Firstly you can use Toolkit.addAWTEventListener. However, that requires security permissions.

Alternatively, push and EventQueue that overrides dispatchEvent. This still requires permission. However, under the JNLP spec and applet implementation, the security manager subclass grants it.

Tom Hawtin

You might notice that the first bug and fix is from me. Sorry...
.