CIS-257 Home: http://www.c-jump.com/bcc/c257c/c257syllabus.html

Intro to Java Swing Forms


  1. Programming User Interface in Java
  2. Java Foundation Classes
  3. Swing vs. JFC
  4. Swing Components
  5. InputDialog and MessageDialog
  6. The Swing Class Hierarchy
  7. Swing Component Hierarchy
  8. A1 Getting Started using the Swing
  9. SwingMain.java
  10. Swing Components
  11. Model-View-Controller Design
  12. A2 Adding JPanel
  13. SwingMain.java
  14. A3 Swing Look and Feel
  15. SwingMain.java
  16. A4 Setting the Window Close Box
  17. SwingMain.java
  18. A5 Setting the Window Close Box, Cont.
  19. A5 Setting the Window Close Box, Cont.
  20. A6 Making a Reusable ApplicationFrame Class
  21. A6 Making a Reusable ApplicationFrame Class, cont.
  22. A7 Simple Two-Button Program
  23. Using the Color class
  24. Constants defined in the Color class
  25. A7 Simple Two-Button Program, cont.
  26. JFrame Revisited
  27. Swing Layouts
  28. Using Layout Managers
  29. Border Layout
  30. A8 GridLayout and Radio Buttons
  31. A8 GridLayout and Radio Buttons, cont.
  32. Menus and Actions
  33. Menu Events
  34. A9 Menu Events
  35. A9 Menu Events, cont.
  36. JDialog Input Dialogs
  37. A10 Custom Dialog Example
  38. A10 Custom Dialog Example, cont.

1. Programming User Interface in Java



2. Java Foundation Classes



3. Swing vs. JFC



4. Swing Components



5. InputDialog and MessageDialog



6. The Swing Class Hierarchy



7. Swing Component Hierarchy



8. A1 Getting Started using the Swing




// @topic T11615 Swing demo A1
// @brief Minimal JFrame and JButton example
/*
 * @author ik
 *
 */

package pckg;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class SwingMain
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }//main

    public static void createAndShowGUI() {
        // Swing test
        JFrame frame = new JFrame("JFrame is me");
        frame.setBounds(0, 0, 400, 300);

        JButton button = new JButton("Hello");
        frame.getContentPane().add(button); // add button to layout

        frame.setVisible(true);
    } //createAndShowGUI
} //SwingMain

9. Swing Components



10. Model-View-Controller Design



11. A2 Adding JPanel


  • JPanels are containers that are

    • automatically double buffered

    • repainted more quickly and smoothly.

  • It is recommended that a program

    1. creates a simple JPanel

    2. adds the panel to JFrame object

    3. adds all other components to the panel object.

  • For example,

    
        JPanel jpan = new JPanel();
        frame.getContentPane().add( jpan );
        JButton jbut = new JButton( "Hello" );
        jpan.add( jbut );
    
    
  • See next slide for a complete program.



// @topic T11625 Swing demo A2
// @brief Minimal JFrame, JPanel and JButton example
/**
 * @author ik
 */

package pckg;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class SwingMain
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }//main

    public static void createAndShowGUI() {
        // Swing test
        JFrame frame = new JFrame("JFrame is me");
        frame.setBounds(0, 0, 400, 300);

        JPanel jpan = new JPanel();
        frame.getContentPane().add(jpan);
        JButton jbut = new JButton("Hello");
        jpan.add(jbut);

        frame.setVisible(true);
    } //createAndShowGUI
} //SwingMain

12. A3 Swing Look and Feel

  • Swing programs will start up in their own native look and feel rather than the Windows, Motif or Mac style

  • It is best to set the OS-specific system look and feel

  • The idea is to use UIManager.setLookAndFeel method

  • See next side for complete example

  • 
    // Setting OS-specific look and feel:
    private static void setSystemLookFeel()
    {
        // Force GUI to come up in the OS-specific look and feel
        String laf = UIManager.getSystemLookAndFeelClassName();
        try
        {
            UIManager.setLookAndFeel(laf);
        }
        catch ( UnsupportedLookAndFeelException exc )
        {
            System.err.println( "Unsupported: " + laf );
        }
        catch ( Exception exc )
        {
            System.err.println( "Error loading " + laf );
        }
    }
    
    


// @topic T11635 Swing demo A3 -- OS-specific look and feel
// @brief UIManager.getSystemLookAndFeelClassName( ) .setLookAndFeel( )
/**
 * @author ik
 */

package pckg;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class SwingMain
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }//main

    public static void createAndShowGUI() {
        // Swing test
        setSystemLookFeel();
        JFrame frame = new JFrame("JFrame is me");
        frame.setBounds(0, 0, 400, 300);

        JPanel jpan = new JPanel();
        frame.getContentPane().add(jpan);
        JButton jbut = new JButton("Hello");
        jpan.add(jbut);

        frame.setVisible(true);
    } //createAndShowGUI
    
    // Setting OS-specific look and feel:
    private static void setSystemLookFeel()
    {
        // Force GUI to come up in the OS-specific look and feel
        String laf = UIManager.getSystemLookAndFeelClassName();
        try
        {
            UIManager.setLookAndFeel(laf);
        }
        catch (UnsupportedLookAndFeelException exc )
        {
            System.err.println("Unsupported: " + laf);
        }
        catch (Exception exc)
        {
            System.err.println("Error loading " + laf);
        }
    }//setSystemLookFeel
} //SwingMain


13. A4 Setting the Window Close Box




// @topic T11645 Swing demo A4 -- Close Window Box
// @brief JFrame.addWindowListener() JButton
/**
 * @author ik
 */

package pckg;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class SwingMain
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }//main

    public static void createAndShowGUI() {
        // Swing test
        setSystemLookFeel();
        JFrame frame = new JFrame("JFrame is me");
        frame.setBounds(0, 0, 400, 300);
        frame.addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(java.awt.event.WindowEvent ev)
            { System.exit(0); }
        }
        );

        JPanel jpan = new JPanel();
        frame.getContentPane().add(jpan);
        JButton jbut = new JButton("Hello");
        jpan.add(jbut);

        frame.setVisible(true);
    } //createAndShowGUI
    
    // Setting OS-specific look and feel:
    private static void setSystemLookFeel()
    {
        // Force GUI to come up in the OS-specific look and feel
        String laf = UIManager.getSystemLookAndFeelClassName();
        try
        {
            UIManager.setLookAndFeel(laf);
        }
        catch (UnsupportedLookAndFeelException exc)
        {
            System.err.println("Unsupported: " + laf);
        }
        catch (Exception exc)
        {
            System.err.println("Error loading " + laf);
        }
    }//setSystemLookFeel
} //SwingMain

14. A5 Setting the Window Close Box, Cont.


15. A5 Setting the Window Close Box, Cont.



16. A6 Making a Reusable ApplicationFrame Class



17. A6 Making a Reusable ApplicationFrame Class, cont.



18. A7 Simple Two-Button Program



19. Using the Color class


Color class defines colors

20. Constants defined in the Color class



21. A7 Simple Two-Button Program, cont.



22. JFrame Revisited



23. Swing Layouts



24. Using Layout Managers



25. Border Layout



26. A8 GridLayout and Radio Buttons



27. A8 GridLayout and Radio Buttons, cont.



28. Menus and Actions



29. Menu Events



30. A9 Menu Events



31. A9 Menu Events, cont.



32. JDialog Input Dialogs



33. A10 Custom Dialog Example



34. A10 Custom Dialog Example, cont.