I hate Swing
I'm trying to build a UI in Java. As those of you who've dealt with Java know, there are two ways of doing it. Just like everything else in the Java libraries, there's the initial botched attempt (AWT) and then there's the replacement, horribly-overengineered version to replace it (Swing). You would think that they would be using a separate branch of the object tree to do it, but no! All Swing objects are also AWT objects, even though the behavior of them is quite different. No sense in letting the rule "inheritance means substitutability" get in the way of a little code reuse. It only means that a window (JFrame) is the sixth layer down in the inheritance tree (Object -> awt.Component -> awt.Container -> awt.Window -> awt.Frame -> swing.JFrame). Of course, JFrames are AWT Components, but not Swing JComponents. Swing JComponents are (of course!) a subclass of awt.Container.
As a Mac programmer, I've had the luxury of using tools like ResEdit, Resorcerer, and Interface Builder to graphically build an interface. I guess it's too much to ask for the ability to graphically build UIs.
I've developed for InDesign, which has no UI tools. Interfaces are text files compiled into resources. That text file is a complicated nested structure listing the pieces of the UI, their initial locations, and how they bind to their parents so that they can resize. I can deal with that. But with Java you need to build your UI programmatically.
You're not supposed to specify any locations. What you should do is drop the widgets into a frame, and the frame's Layout Manager lays them out in some order. If you have a grid, use a GridLayout. If you have some parts that have to grow as the window grows, you need a BorderLayout. SpringLayout is new in 1.4, and is really only usable with a tool to build everything it needs. So I picked the BoxLayout, which runs vertically or horizontally, and lays out the items as it goes. It's even nice enough to respect the minimum and maximum size of items, which is more than other layout managers do.
Problem 1: I have localization files in my jar. When I run the app from the debugger, everything works wonderfully. Run it manually, and none of my buttons have labels.
Problem 2: If I don't call pack() on the window, I can resize the window down to nothingness despite the supposed "minimum sizes" that the box layout is supposed to keep. So I do. But now the window starts up at this small size which is less than my window's explicitly overridden getMinimumSize() function.
Problem 3: And that's ignoring the list box's minimum size, set by clusterList.setMinimumSize(new Dimension(200, 200)). The box always starts up fully collapsed.
Problem 4: When laying out the components vertically, I want them all run up along the left edge of their parent. setAlignmentX(LEFT_ALIGNMENT)? No, that only aligns them relative to each other. They still don't move to the parent's left edge.
At MacHack, Nicholas Riley showed me his HipTop. They use Java for development, but they have their own UI classes (danger.ui). There's a reason for that. AWT and Swing need to be ripped out, destroyed, and reimplemented. Sanely, this time.
[edit: Answer #1: Make sure the Class-Path entry in the manifest is present and is ".". Otherwise, Java doesn't bother to check in the jar for stuff. Not like there's a reason I packaged it, eh?]
[edit: Answer #4: If all the components have left-alignment specified, they move to the left. Otherwise, no. Wonderful. And I still can't get any minimum sizes to be honored.]
Comments
I've had similar problems with Swing (I think everybody has), and I can't disagree with your opinion. However I do want to point out that, although Swing doesn't respect the *MinimumSize() settings (which is really stupid), it DOES try very hard to respect the *PreferredSize() settings. So calling "setPreferredSize()" on your controls will usually aleviate any problems after calling pack(), while setMinimumSize() does absolutely nothing but waste your time. The effectiveness of this technique, however, can vary from one machine (i.e. one "Look-and-Feel") to another.
Posted by: Kevin | September 24, 2003 11:19 AM