Monday, January 17, 2005

No more getContentPane()!

I just discovered (and tested, on my codebase) that in J2SE5 Swing, you no longer have to get the content pane in order to add something to another component. This was always wierd, because you could call add() and -- even though it was acceptable to the compiler -- nothing would happen; instead you had to call getContentPane().add(). (There's probably something in here about the failure of static type checking to solve the problem of calling the wrong method.)

At the same time (and this may have been the case for awhile; I might easily have missed a meeting about Swing), all the examples in the Swing Tutorial now have main()s that look like this:


public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); }
});
}

That is, now you are not supposed to start up a Swing UI directly from main(), but instead you must add a task to the event queue.

After all these years, apparently someone discovered a race condition from starting it directly in main(). I wonder how many people were bitten by this? (And how many new fundamental issues like this will show up over time?) Well, Swing is a large, complex library that has bitten off a huge challenge, so there are bound to be issues like this. But after spending the last few days with SWT, that library seems more straightforward in a lot of ways, probably because it's trying to leverage the underlying OS rather than replace it. In general, SWT feels like it's trying to "just build GUIs" rather than "create a whole new world for GUIs." I suspect that each has its place depending on what you're trying to do, but I'd be tempted to use SWT unless there was some compelling need for Swing, if only to provide a more familiar experience for the end user. I've seen a number of designers say "we think the standard UI (usually Windows) is bad, so we've reinvented it," without discovering whether the end user wants their UI to be reinvented. The point of a standard UI is that you don't have to go up a learning curve every time you run a new application. People want every application to look roughly the same, because "figuring out your cool new UI" is not the problem they want to solve.