CS 235 - Week 4 Lecture

*   Trying to get to GUIs
    *   GUI - graphical user interface
    *   pulling material for course text, Chapters 6, 7, and 8!

*   Java Swing package - the graphical user interface
    package we'll be primarily using this semester
    *   javax.swing

*   because many of Swing's classes are subclasses
    of existing classes, many of their names begin
    with J --
    JFrame,
    JPanel,
    JButton,
    etc.
    *   (and even some that aren't subclasses of
        existing classes, to follow the pattern...)

*   top-level window -- one NOT within another window --
    is called a frame in Java
    *   Swing's frame class is named JFrame

    *   Unlike many Swing containers and components,
        a JFrame is "drawn" by the user's windowing
	system (the operating is more directly involved)

*   our first example: see SimpleFrameTest
    (adapted from p. 340, in Core Java)

*   it is NOT considered good practice to put components
    directly ONTO a frame's content pane --

    ...instead, you add them to a sub-container, and add
    THAT to the frame;

    typically/often the sub-container added to is a panel;
    using Swing, this will usually be a subclass of JPanel

    *   create one or more JPanel (or JPanel subclass)
        instances, and add them to the frame

    *   see example SimpleLabelTest to see this in action;

*   in SimpleTLabelTest, why do the JLabels appear where
    they do?
    *   that's the work of the panel's default layout
        manager
    *   why does the JPanel appear where it does?
        *   that's the work of the frame's default
	    layout manager

    *   the panel's default layout manager is called
        FlowLayout
        *   it adds components to the container
            in the order added,
	    from left-to-right,
	    top-to-bottom,
	    determining how many things fit on each
	       "row" and then centering them

*   just for fun, an example of another Swing component
    (that isn't a subclass of something from AWT):
    
    JOptionPane

    ...dialog boxes of various flavors!

*   playing with JOptionPane a bit from DrJava's
    interactions window:

    Welcome to DrJava.  Working directory is /Users/smtuttle/humboldt/f15cs235/235lectures/235lect04
    > import javax.swing.*;
    > JOptionPane simpleDialog = new JOptionPane();
    > simpleDialog.showMessageDialog(null, "Welcome to\n Swing Spam!");

    *   showMessageDialog's first argument is a reference
        to the frame this dialog is to be "attached" to --
	it is null here because we don't want it to be
	attached to a particular frame

        ...and its second argument is the String whose
	contents are to be shown in the message dialog;

    *   ...and a message dialog appears on-screen,
        with the text 

*   JOptionPane also has some static methods for
    getting some standard dialogs:

    ...here, demonstrating its showInputDialog static
    method -- it expects a String, and returns a String;

    > // NOTE -- when you click OK for showInputDialog's option pane,
    > //     you get a String version of whatever has been typed
    > //     into its text field;
    > // BUT if you click cancel, you get null! (EVEN IF something has
    > //     been typed in!)

    *   ...here, I typed red into its textfield, and then
        clicked OK -- note what choice variable then contains:

    > String choice = JOptionPane.showInputDialog("Please enter a color");
    > choice
    "red"

    *   this time, I didn't type ANYTHING into the textfield;
        I just clicked OK (the contents of the textfield were
	returned, and so here that was an empty String):

    > choice = JOptionPane.showInputDialog("Please enter a flavor");
    > choice
    ""

    *   this time, I typed moooo into the textfield,
        but clicked Cancel -- a reference to a String is
	NOT returned when you click Cancel here; null
	is returned instead!:

    > choice = JOptionPane.showInputDialog("Please enter a size");
    > choice
    null

    *   tried typing \ and n, and clicking OK:

    > choice = JOptionPane.showInputDialog("Please enter a size");
    > choice
    "\n"

    *   and note that it always returns a (reference to a) String --
        if you happen to type digits, it is a String with 
	digit characters:

    > choice = JOptionPane.showInputDialog("Please enter a size");
    > choice
    "23"
    > 

*   JOptionPane is nice -- but it isn't REALLY user
    event handling;
    ...we want buttons that do things! etc.

    we will see that we need INTERFACES for Java
    event handling.

    an Interface is NOT a class,
    BUT! it is a SET of requirements for classes
    that want to conform to that interface.

    SINCE an interface is also considered to be a type,
    the instances of a class that implement an interface 
        are also considered to be of that type

    ...more on this on Wednesday! (also more on this in
    Chapters 6 and 8)