Please send questions to st10@humboldt.edu .
Random notes for CIS 480 - Advanced Java Programming, Week 5, 2-19-01

Topic: Intro to SWING, Part 2

* (last time: painting on an AWT GUI, intro to Swing ---
compare/contrast to AWT, the four panes on a JFrame, components added
to content pane)

* discussion of some of the differences between Swing and the AWT:

Frame   ===> JFrame
Applet  ===> JApplet
Dialog  ===> JDialog
        * remember: add components, setLayout, setBackground
        top the content pane, NOT the container itself;

        * (getContentPane())

        * you don't override paint() to paint; more on that
        later

        * Swing components are not currently thread-safe;

Applet  ===> JApplet
        * default layout manager for JApplet is 
        BorderLayout rather than FlowLayout

        * do your users have Swing-capable browsers???

        * remember: you do not paint a JApplet by 
        overriding paint()! (more on that later)

        * note, also: Swing components, including JApplet, 
	  are not thread-safe;

Button  ===> JButton
        * a JButton can disply an image as well as text

TextField ===> JTextField
        *   for simple uses, the two are quite compatible;
        (see ManyJButtons2, TryButtons2 examples)

        * (can set JTextField to be un-editable by user);

        * text listeners do NOT work the same with JTextField
        as with TextField;

Label   ===> JLabel
        * JLabel can have image as well as text

        * From Java's Swing Tutorial,  
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html:
        " To support accessibility, use setLabelFor() 
        to associate each label with
        the component it describes (if any)."

Choice  ===> JComboBox
        * note that a JComboBox is, by default, uneditable

        * MIGHT have to re-write code that handles item events

List    ---> JList
        * these differ a LOT.

        * does your list-of-your-dreams have multiple
        columns? Use a JTable!

        * does it have hierarchical information? use a
        JTree!

* Checkbox ===> JCheckBox, JRadioButton
        * note the b vs. B

* Scrollbar ===> JScrollPane or JSlider or JProgressBar
        
* TextArea ---> JTextArea
        * typically requires some re-coding to convert

        * a JTextArea does not contain scrollbars by default;

* Dialog ---> JDialog or JOptionPane
        * yes, the Swing JDialog does have a content pane

* ScrollPane ===> JScrollPane
        * JScrollPanes can have custom decorations,
        including row and column headings;

* Menu          ===> JMenu
* MenuBar       ===> JMenuBar
* MenuItem      ---> JMenuItem
        * Swing menu components are true components

* IN GENERAL...
        * Swing components are not thread-safe!

        * Swing lets you specify a particular look-and-feel

        * you can change, add the borders drawn around
        most Swing components

        * mouse events on Swing components "fall through"
        to their parents; that is not so for AWT components.

        * if you are mixing AWT and Swing components and they
        overlap, the AWT component is always on top...

        * Swing components are designed so that assistive
        technologies (like screen readers) can easily
        get information from them (remember the JFC
        main features)

        * Swing components with state use models can keep 
        their state. 

* now --- new goodies, capabilities in Swing

* for example: Swing has a number of components that
are "pop-up window" like ---
        JPopupMenu
        JCombiBox
        JMenuBar

* some components particular to Swing
        color choosers
        editable combo boxes
        progress bars
        split panes
        tabbed panes
        tables
        tool tips
        trees

Now, some examples? 
* how to place an image on a JButton (or a JLabel)

        * BlueJ note: even if you put the .gif file in
        the same directory as your class file --- which
        should work, and does, when running an application
        from the DOS prompt --- BlueJ cannot find it,
        because its "current directory" is where the BlueJ
        batch file or shortcut is, rather than the project
        directory.

                * note that you can type in and compile the
                code under BlueJ, however, and then run it
                from the DOS prompt, as demo'd in class today.

        * TryImageButton1.java shows an example of adding
        an image to a JButton.

        * Icon is an interface --- ImageIcon is one 
        implementation of that interface.
                * (from Java 1.2.2 API): ImageIcon is "An 
                implementation of the Icon interface that 
                paints Icons from Images."
        
        * You can then obtain an ImageIcon instance from a 
        desired .gif file as follows:

ImageIcon myImageIcon = new ImageIcon("desiredGifFile.gif");

        * JButton can then use this myImageIcon resulting as 
        a parameter to its constructor;

                JButton myButton = new JButton(myImageIcon);

                * (there's also a constructor that takes image 
                icon AND text, etc.)


* JLabel aside:
        * (seen in TryImageButton1.java)

        * one can set the horizontal alignment in a JLabel 
        (that is, is the text *within* the label left-justified
        (the default), right-justified, or centered? See the 
        Java 1.2.2 API for more details).

        * here's an example:

JLabel myLabel = new JLabel("this is label text", SwingConstants.CENTER);

* We have already mentioned that one cannot paint on a JApplet
by writing one's own paint() method --- what DO you do, then?

        * (in fact, note that JFrames, JApplets, JDialogs do NOT 
        like to be painted on!)

        * they do not even like it if you try to paint on their
        content panes;

        * instead, it is recommended practice to draw onto 
        another component, and then add THAT to
        the content pane;

        * and, a recommended component to paint upon is a JPanel.
                * a "custom" JPanel --- that is, one that you
                create by extending JPanel;

                * you extend JPanel, and override (write your own
                version of) paintComponent().

        * Important note, for JPanel and *any* Swing component:
                * you MUST call the paintComponent() method
                of the SUPERclass WITHIN your new
                paintComponent() method;

                * (superclass might be required to erase old
                contents of the panel, handle borders, etc.)

                *   how do you call the paintComponent() of the 
                superclass? With:

                        super.paintComponent(g);

        *   now, look at TryJAppletPainting.java; it is very simple, but 
        it WORKS, and gives you a WORKING example!

        *   and, TryJAppletPainting2.java is only a little more, but includes
	  repainting;

* a small beginning to Borders:
        * from Java's Swing Tutorial,
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html:

        "You can easily add or change the borders drawn around most 
        Swing components. For example, it's easy to put a box around 
        the outside of a container or label." 

        * for example, in TryBorder1.java, you see a small EtchedBorder
        TitledBorder added to the panel that we paint on;

        * note: need to import javax.swing.border.* to do this!

        * Just call setBorder() for the component for which you want a
        border --- for example, in TryBorder1.java, you'll see:

// parameters of this version of setBorder: an anonymous
// instance of a TitledBorder. The TitledBorder constructor
// here is using an anonymous instance of EtchedBorder
// followed by the desired title in String form.
paintOnMe.setBorder(new TitledBorder(new EtchedBorder(), "I am a Border"));
   
* see TryJTable1.java for a very simple JTable example, too; do not have detailed notes for (that I can find) just now, though;