Swing Defaults

So I have been working on another Java Swing component this week, this time a subclass of JButton. I’ve had a surprising amount of problems with it, mostly because of the strange defaults that Java Swing sets.

For example why when you subclass JButton is the default size 1 by 1? Why when you set the size of said subclass of JButton does it not use the value set by .setSize() in preference to the 1 by 1 default. You mean you don’t want a button of 1 by 1 size? Why ever not?…

Why do I have to test all of the JButton size methods .setSize() .setMinimumSize() .setMaximumSize() to find out that the only one that makes any difference to the actual visual size of the JButton is .setPrefferedSize()? Is it just me or does having preferred in the method title not suggest that this value may or may not be used, it certainly gives me no inclination that this is the only method which will make my button visible.

My second gripe with the defaults was actually caused by defaults set by the Apple Look & Feel. I noticed by pure chance when debugging that my paint method in ButtonUI was getting called, like, all the time.

Thanks to Apple, and in a way I am actually being non-sarcastic, their L&F gives a “default capable” button periodic events to repaint. In this way you get calls which allow you to create a button that pulsates. It’s fantastic that Apple has given this capability for free, saving anyone who wants a pulsating button from spawning another thread to send the periodic repaint messages, and all the joys that come with handling your own threads in Java.

However how often does one actually want a pulsating button?

I put it to you that it is way more probable that someone wants to create a button that does very little by the way of custom drawing, and actually just wants button functionality that doesn’t hideously cripple any application that uses it.

So fantastic functionality, but can anyone tell me why setDefaultCapable() isn’t set false by default?

2 Responses to “Swing Defaults”

  1. Chris Miller says:

    The whole setPreferredSize() always confused me as logically it seems that this size could be used but may not. The size may be affected by layout managers and the other set size methods used to set the limits.

    As for 1×1 size buttons, this may just be so that the button will expand to fit the contents. Perhaps the button will expand to at least the value set by setMinimumSize() and at most the value set by setMaximumSize().

  2. Scott Porter says:

    I don’t know, a pulsating button sounds interesting. I was recently looking for such a button for an applet I put on a page where all the text is in ‘blink’ tags… :) I think they would blend well. ;)

Leave a Reply