JMU
Windowing
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Windows
Windows in Java

Class Hierarchy

images/java-window-hierarchy.gif
Windows in Java (cont.)
Windows in Java (cont.)

A Simple Example

        JFrame frame = new JFrame("Amazing!");
        frame.setSize(400, 400);
        frame.setVisible(true);
  
Menus

Using Something Like the Composite Pattern

images/menu-menuitem.gif

It would be better if there were a JMenuComponent interface that both the JMenuItem and the JMenu implemented (realizations of which could be added to a JMenu).

Menus (cont.)

An Example

javaexamples/layout/MenuBarDriver.java
import javax.swing.*;

/**
 * An example that uses a menu bar
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison Univeristy
 */
public class MenuBarDriver
{

    /**
     * The entry point of the example
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
        JFrame        frame;        
        JMenuBar      menuBar;
        JMenu         editMenu, fileMenu, helpMenu, searchMenu;
        JMenuItem     item;

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        menuBar = new JMenuBar();


        // The File menu
        fileMenu = new JMenu("File");

        item = new JMenuItem("Exit");
        fileMenu.add(item);
        menuBar.add(fileMenu);


        // The Edit menu
        editMenu = new JMenu("Edit");
        
        item = new JMenuItem("Cut");
        editMenu.add(item);
        
        item = new JMenuItem("Paste");
        editMenu.add(item);

           // The Search menu (in the Edit Menu)
           searchMenu = new JMenu("Search");

           item = new JMenuItem("Backward");
           searchMenu.add(item);
       
           item = new JMenuItem("Forward");
           searchMenu.add(item);

           editMenu.add(searchMenu);
        
           menuBar.add(editMenu);


        
        // The Help menu
        helpMenu = new JMenu("Help");

        item = new JMenuItem("About");
        helpMenu.add(item);

        menuBar.add(helpMenu);



        // The menu bar
        frame.setJMenuBar(menuBar);

        
        frame.setSize(400,200);
        frame.setVisible(true);
    }
}
        
Dialog Boxes
Dialog Boxes (cont.)

An Example Message Dialog

javaexamples/usinggui/MessageDialogDriver.java
import javax.swing.*;


/**
 * An example that uses a standard message dialog box
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class MessageDialogDriver
{


    /**
     * The entry point
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
        ImageIcon     icon;


        System.out.println("Before the dialogs...");


        JOptionPane.showMessageDialog(null, 
                       "The power will be shut off in 5 minutes!"
                   );


        JOptionPane.showMessageDialog(null,
                       "Please do it right!",
                       "Error",
                       JOptionPane.ERROR_MESSAGE
                   );
        
        // Possible types:
        //
        // PLAIN_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSGAE
        // QUESTION_MESSAGE, ERROR_MESSAGE



        icon = new ImageIcon("madison.gif");


        JOptionPane.showMessageDialog(null,
                       "Welcome to James Madison University",
                       "JMU",
                       JOptionPane.INFORMATION_MESSAGE,
                       icon
                   );
        
        System.out.println("After the dialogs...");
    }
}
        
Dialog Boxes (cont.)

An Example Option Dialog

javaexamples/usinggui/OptionDialogDriver.java
import javax.swing.*;


/**
 * An example that uses a standard option dialog box
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class OptionDialogDriver
{


    /**
     * The entry point
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
       int result;



       result = JOptionPane.showOptionDialog(null, 
                                             "Do you want to retry?",
                                             "Error!",
                                             JOptionPane.YES_NO_OPTION,
                                             JOptionPane.ERROR_MESSAGE,
                                             null, null, null
          );


       if (result == JOptionPane.CLOSED_OPTION) 
       {
          System.out.println("Closed the window");
       } 
       else if (result == 0) 
       {
          System.out.println("Yes");
       } 
       else if (result == 1) 
       {
          System.out.println("No");
       }
    }
}
        
Dialog Boxes (cont.)

An Example Input Dialog

javaexamples/usinggui/InputDialogDriver.java
import javax.swing.*;


/**
 * An example that uses a standard input dialog box
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class InputDialogDriver
{


    /**
     * The entry point
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
       ImageIcon     icon;
       String        response;
       String[]       schools;


       response = JOptionPane.showInputDialog(
          "What is your favorite University"
          );



       System.out.println("\n\n\n\n");
       if (response == null) 
       {
          System.out.println("I asked a question and"+
                             "I expect an answer!");
       } 
       else 
       {
          if (response.equals("JMU")) 
          {
             System.out.println("Yes! JMU rules!");
          } 
          else 
          {
             System.out.println("Are you kidding? " + 
                                response + "?");

             icon = new ImageIcon("madison.gif");

             schools = new String[5];
             schools[0] = "George Mason University";
             schools[1] = "James Madison University";
             schools[2] = "Old Dominion University";
             schools[3] = "University of Virginia";
             schools[4] = "Virginia Institute of Technology";
                
             response = (String)JOptionPane.showInputDialog(
                null, 
                "What is your favorite University?",
                "University Selection",
                JOptionPane.QUESTION_MESSAGE,
                icon,
                schools,
                schools[1]
                );

             System.out.println(response);
          }
       }

       System.out.println("\n\n\n\n");

    }
}
        
Dialog Boxes (cont.)

An Example File Dialog

javaexamples/usinggui/FileDialogDriver.java
import java.awt.*;
import javax.swing.*;

/**
 * An example that uses a JFileChooser dialog box
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class FileDialogDriver
{

    /**
     * The entry point
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
       JFrame               f;
       Container            contentPane;
       int                  retval;
       JFileChooser         jfc;



       f = new JFrame();
       f.setSize(400,200);

       contentPane = f.getContentPane();

       f.setVisible(true);

       jfc = new JFileChooser();
       retval = jfc.showOpenDialog(f);

       if (retval == JFileChooser.APPROVE_OPTION) 
       {
          System.out.println(jfc.getSelectedFile().getName());
       }
    }

}
        
Dialog Boxes (cont.)

An Example Color Dialog

javaexamples/usinggui/ColorDialogDriver.java
import java.awt.*;
import javax.swing.*;

/**
 * An example that uses a JColorChooser dialog box
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ColorDialogDriver
{

    /**
     * The entry point
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
       JFrame               f;
       Color                color;
       Container            contentPane;



       f = new JFrame();
       f.setSize(400,200);

       contentPane = f.getContentPane();

       f.setVisible(true);

       color = JColorChooser.showDialog(f,
                                        "Choose a color",
                                        Color.blue);

       if (color != null) 
       {
          System.out.println(color);
       }
    }

}