I recently turned my attention to GUI and designing professional looking interfaces. I've got netbeans 7.1.1 and I've been going through the Oracle tutorial pages to get up to speed with Java. When advising users on Swing programming the notes tell you how to operate design options in Netbeans and I have no trouble with that. However when it gives you the code for the Actions that the components on your Frame have it seems to pull them magically out of mid-air without explaining what they do. The following section is more bogged down in detail and looking at the Java docs on the subject ... there is so much content. I don't know how to get to grips with it. Can someone recommend a website that shows the workings of various GUIs in Java so that I can take the code and adapt it to my purposes while I am getting started. Alternatively a good book on learning Swing at beginner to intermediate level would be greatly appreciated. Something I will still need for coding after I have got to grips with the essentials.
It is probably a bad idea to use NetBeans at this stage, because its GUI-builder produces code which looks like nothing else on earth. You are probably better off writing code by hand. How about a copy of Horstmann and Cornell’s book. It covers a lot more than Swing, and is well worth reading, but I don’t think it is a beginner’s book.
Horstmann and Cornell also don’t teach addActionListener(this); or similar, which you find in some books, and the Java™ Tutorials. It is convenient to show the syntax, but I don’t think it is object‑oriented programming. H&C haven’t updated their book for Java7 yet, but I don’t think that will make any difference to the Swing programmer.
I will give you a few important pointers that might help you to find your own way through Swing.
- AWT uses so called heavy-weight components to achieve its goals. I believe the distinction lies in that these heavyweight components are driven by events produced by the operating system directly.
- Swing builds on top of AWT. The only heavyweight components in Swing are top-level containers: JFrame, JDialog, JApplet and JWindow. When they get events from the operating system, they perform some calculations to determine which lightweight components they contain should get updated.
- Always use lightweight components inside Swing containers. Never mix AWT and Swing components. Except for the top level containers, all javax.swing.* components are lightweight.
- Always handle the layout of your components through layout managers. Consider what should happen to the layout if containers resize. You can then reason about what LayoutManager to use.
- All operations that manipulate the GUI (such as getting or setting text in a text field, or changing the caption of a button) should happen on the Event Dispatch Thread. Long calculations can be done in separate threads to keep the GUI responsive, but once they're finished their results should be dealt with on the EDT.
Please let us know when you have specific issues you have a hard time with.