Chapter 9 - Graphical User Interfaces

Chapter Objectives:

  • Acquire familiarity with the Swing component set.
  • Understand the relationship between the AWT and Swing.
  • Have a better understanding of Java's event model.
  • Be able to design and build simple Graphical User Interfaces (GUI)s.
  • Appreciate how object-oriented design principles were used to extend Java's GUI capabilities.

Index :

AWT vs Swing

Figure 9-1 Swing Classes Part 1.
 
 

Swing Objects of note:


Figure 9-2 Swing Classes Part 2

More Swing Objects:


Creating a frame



How Interfaces Work -  Model-View-Controller (MVC) :

Handling Events


Click here to view the code
 

Click here to view the second example
 

Other Notes on Events:

         f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
         });


Designing a JAVA GUI

Basic User Interface Tasks: Example Swing objects for input, output, control, guidance: Choosing the top level Window Choosing a Layout Manager

 LayoutManagers provide the intelligence to arrange and size components in a container. There are several different predefined LayoutManagers available for you to use:

Default Layout Managers


Containment Hierarchies


figure 9-14


Figure 9-15
 

Click here to see the source code for this application

Question for thought : How could this application be rewritten to have the same interface, but make the Keypad reusable?

Answer - Create a separate class named Keypad, it would inherit JPanel, reside in its own file (Keypad.java), and handle setting up the buttons on the grid and handling the button events.
 


Decimal Format Object



CheckBoxes, Radio Buttons, Borders


 

click here to see the source code for this application


Menus

Click here for the code for this sample.


Scrollbars, JTables, Tabbed Panels

  • Adding scroll bars around objects.
  • click here to see all the code


    Tool Bars

    Click here to see the source for this example


    Creating Forms using the GridBagLayout Manager

    To create a Form  you need to:

    click here to see the code


    Popup Forms / Modal Dialog boxes

    Creating a modal dialog box


    click here to see the code for this example


    Text Editor Example (differs from the book)

    Click here to see all the code
    Click here for a zip file with all the code


    FileDialog, JOptionPane


    click here for the source code for this example


    Key Terms:
    content pane,controller, event model, layout manager, lightweight component, listener, model, Model-View-Controller(MVC), peer model , view, widget hierarchy.
    Suggested Exercises:  1,2, 5,6, 7, 8, 9, 10, 14, 15.