File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes I'm trying to learn Swing.  I'm self taught.  Whats the best way to get a handle on this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "I Watch "I New topic
Author

I'm trying to learn Swing. I'm self taught. Whats the best way to get a handle on this

Bill Jillians
Greenhorn

Joined: May 01, 2012
Posts: 22
Hi there.

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.

Many thanks for your help.

Best regards
Bill
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I shall move this discussion to the Swing forum.

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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Hi Bill,

I recommend going through the Oracle tutorials and doing all your GUI coding by hand instead of relying on the IDE. This is especially important in order to get the hang of layout managers.

Introduction to Swing
Swing Tutorial (but skip the NetBeans part until you're comfortable with hand coding)

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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Campbell Ritchie wrote:H&C haven’t updated their book for Java7 yet, but I don’t think that will make any difference to the Swing programmer.

These are the Java 7 Swing enhancements that I'm aware of:
  • A new class, JLayer
  • Window transparency and shape, achievable via proprietary classes since 6u10, are now in the public API
  • JList, ListCellRenderer and ListModel use Generics; a new method or two added to make use of the Collections framework


  • luck, db
    There are no new questions, but there may be new answers.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    Sorry, I wasn’t aware of them. Actually I think I have heard about the generics thing.
    Randall Twede
    Ranch Hand

    Joined: Oct 21, 2000
    Posts: 4347
        
        2

    i also recommend writing your code "by hand" using a simple text editor(i use and recommend Notepad++). then you compile it and run it from the "command line". the API is your best friend.


    SCJP
    Visit my download page
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19785
        
      20

    Darryl Burke wrote:
  • JList, ListCellRenderer and ListModel use Generics; a new method or two added to make use of the Collections framework

  • So do JComboBox and ComboBoxModel. Disappointingly, JSpinner and SpinnerModel don't. Probably because SpinnerNumberModel would be too hard to convert, supporting both int and float in one class.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: I'm trying to learn Swing. I'm self taught. Whats the best way to get a handle on this