JamesMadisonUniversity

CS Dept


CS 159 Lab: Graphics Introduction


Objectives:

Students will:
  • Be able to add a menu to the menu bar
  • Be able to use nested menus
  • Be able to add scroll bars, giving the user the option of when they will be seen.
  • Be able to change the look and feel, giving the user the option of which look and feel to use.
  • Experience inner classes

New Terms:

Inner class

Menu

Submenu

JScrollPanel, ScrollPanelConstants

Materials:

Starter File: Notetaker.java

Acknowledgment:

Original lab by Gaddis Lab Manual.

Introduction:

In this lab we will be creating a simple note taking interface. It is currently a working program, but we will be adding features to it. The current program displays a window which has one item on the menu bar, Notes, which allows the user 6 choices. These choices allow the user to locally store and retrieve up to 2 different notes. It also allows the user to clear the text area or exit the program.  


In this program, a Notetaker is a specialization of a JFrame. The constructor takes care of setting the JFrame/window's size, how it reacts when the close is clicked, and setting the content pane's layout. The constructor then creates two menu objects (although at the start of the lab, the second menu is empty). The notes menu is then added to a menu bar, and the menu bar is added to the JFrame/window. Finally a text panel is created which is then added to the content pane within the JFrame.


We would like to add features to this program which allows the user to change how the user interface appears. We will be adding another choice on the menu bar called Views, giving the user choices about scroll bars and the look and feel of the GUI.

Part 1 - Getting ready and exploring the existing code.

  1. Copy the file NoteTaker.java from this link.
  2. Compile and run the program. Observe the horizontal menu bar at the top which has only one menu choice, Notes. We will be adding an item to this menu bar called Views that has two submenus, one named Look and Feel and one named Scroll Bars. The submenu named Look and Feel lets the user change the look and feels: Metal, Motif, and Windows. The submenu named Scroll Bars offers the user three choices: Never, Always, and As Needed. When the user makes a choice, the scroll bars are displayed according to the choice.  
  3. We want to logically break down the problem and make our program easier to read and understand. We will write separate methods to create each of the vertical menus. The three methods that we will be writing are createViews(),createScrollBars(), and createLookAndFeel(). The method headings with empty bodies are provided.

Part 2 - Creating the menus

  1. Let’s start with the createLookAndFeel() method. This will create the first submenu shown in figure 1. There are three items on this menu, Metal, Motif, and Windows. We will create this menu by doing the following:
    1. Instantiate a new JMenu with the name Look and Feel and assign it to the attribute lafMenu. See example in createNotes().
    2. Create a new JMenuItem with the name Metal.
    3. Add an action listener to the menu item (see the createNotes()method to see how this is done). While you will add the MenuListener to the sub menu items, you are not editing the MenuListener actionPerformed method yet. That will come in a later step.
    4. Add the menu item to the menu.
    5. Repeat steps b through d for each of the other two menu items.
  2. Similarly, write the createScrollBars()method to instantiate a JMenu that has three menu items, Never, Always, and As Needed. Assign this to the sbMenu instance variable. See figure 2.
  3. Now that we have our submenus, these submenus will become menu items for the Views menu. The createViews()method will make the vertical menu shown cascading from the menu choice Views as shown in figure 3. We will do this as follows
    1. Create a new JMenu with the name Views.
    2. Call the createLookAndFeel() method to create the Look and Feel submenu.
    3. Add a MenuListener to the Look and Feel menu.
    4. Add the Look and Feel menu to the Views menu.
    5. Repeat steps b through d for the Scroll Bars menu item, this time calling the createScrollBars()method.
  4. Finish creating your menu system by adding the Views menu to the menu bar in the constructor.
  5. Test your window. You should see all of the new submenus, but of course none of them will work yet.
Figure1 Figure2 Figure 3
Figure 1 Figure 2 Figure 3

Part 3 - Adding Scroll Bars and Editing the Action Listener

  1. Add scroll bars to the text area by completing the following steps in the constructor
    1. Instantiate a JScrollPane object called scrolledText, passing in theText.
    2. Change the line that adds to the textPanel, by passing in scrolledText (which now has theText). So basically, what you are doing is wrapping a JScrollPane around a plain JTextPane so that the panel will have scroll bars.
  1. Edit the action listener by adding 6 more branches to the else-if logic. Each branch will compare the actionCommand to the 6 submenu items: Metal, Motif, Window, Never, Always, and As Needed. We'll start with the three Look and Feel options.

    Each look and and feel has its own class.  To cause our contentPanel to change its look and feel to Metal, the following have to be executed.

        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); Doc
        SwingUtilities.updateComponentTreeUI(getContentPane());

    The fact we are passing a a string containing a class name instead of creating an object of that class is a weird (and bad) design defect in how these classes were created. But we have to live with it.  The strings for the other look and feels are:

    "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"

    Compile and you will get an error.  These methods may throw a checked exception. Enclose that code in a try / catch block displaying an error message if the Look and Feel could not be changed (do not end the program in case of error).
  2. Now for the scroll bars. A scroll bar "policy" describes how the scroll bars should be handled. For each Scroll Bars submenu item, set the horizontal and vertical scroll bar "policy" to the appropriate values.
    1. Be sure to review the Javadocs for the JScrollPane. Doc
    2. What method can be used to set how the scroll bars are handled? This method takes an integer value as a parameter. If you look at the ScrollPanelConstants, which JScrollPane implements, you will find constants that represent the integer values. ScollPaneConstants is an example of an interface with only public constants...these fields are the values provided by the interface to be used by implementers of the interface but there are no required methods.
  3. Any components that have already been created need to be updated. This can be accomplished by calling the SwingUtilities.updateComponentTreeUI method as with the Look and Feel branches, passing a reference to the component that you want to update as an argument. Specifically you will need to add the line

SwingUtilities.updateComponentTreeUI(getContentPane());

to each branch that you just added in the logic structure.

Turning in your work

  1. For Prof Nancy Harris, Sections 1, 4, 5: Demonstrate your working program in lab.
  2. For Prof Arch Harris, Sections 2, 3, Demonstrate your working program in lab.

 


Updated 04/22/2014 (nlh)