CS 051 Laboratory # 6
GUI Practice
Objective: To gain experience using GUI components and listeners.
In this lab, you will play with GUI components, layout management and listeners. You will create a program which includes a JSlider, a couple of JLabels, a pop-up menu (JComboBox), a JTextField, and a JButton. A running version of the program we hope you produce is provided below (if your browser handles applets correctly):
When the user clicks anywhere on the canvas, your program should display the text showing in the JTextField in the selected font and font size at the place where the user clicked. When the user adjusts the JSlider or selects a new font with the JComboBox menu, the last text placed on the canvas should change its size or font accordingly. Changing the text in the JTextField has no impact on items displayed on the canvas.
If the JButton is pressed, the last text placed on the canvas should be removed. (You may think of this as a one-level undo. Pressing the button a second or third time will not remove additional items.)
For this lab, you will create an Eclipse project from scratch. See the instructions at http://www.cs.pomona.edu/classes/cs051/handouts/eclipse-install.html. Please name your class TextPlay.
Before beginning be sure that you open up the GUI cheat sheet from the "Resources" section on the class web site. As a quick reminder, remember the basic steps in displaying components:
Finally, when you've added all your components, don't forget to call validate on the content pane obtained from calling getContentPane.
To react to events generated by the user interacting with a GUI component, remember these steps:
Note that the statements import java.awt.*;, import java.awt.event.*;, import javax.swing.*;, and import javax.swing.event.*; need to appear at the top of your class. These lines inform Java that your program will need access to the standard Java libraries that support GUI components and events (including event listeners).
When you run your program from Eclipse, set the width to 340 and the height to 420.
Here is a suggestion on how to decompose the development of this program into simpler steps.
You will want a variable associated with the Text item most recently displayed so that you can change its font or font size later. You will associate this name with the current text item in your onMouseClick method.
Since your program does not react to the user typing in the JTextField, it is not necessary to associate a listener with this component.
Start with the JButton.
Add a JButton to the panel so that it contains the words "Remove last text". In order to allow your WindowController extension to respond when the button is pressed, add the phrase "implements ActionListener" to the header of the class. Call the button's addActionListener method in your begin method to inform the button that your WindowController wants to be notified when the button is pressed and add an actionPerformed method to your class that performs the appropriate action (removing the text from the canvas) when the button is pressed.
Make sure your program behaves reasonably if the user clicks the button before actually putting any text on the canvas.
There are two ways to delete an object. One is to use removeFromCanvas and the other is to just hide it. Either one is fine, but make sure that if you press the button multiple times, you don't get any exceptions. If you use removeFromCanvas this will require you to check for null.
See the GUI cheat sheet for the constructor and the method used to add choices to the JComboBox menu. Then add the JComboBox menu to the main panel and tell it that your program will be the listener. The JComboBox requires an ActionListener just like JButton does, so you do not need to add an "implements" clause. You do need to modify your actionPerformed method to react to menu selections made by the user. Use the getSource() method on the event parameter to find out which GUI component the user interacted with. getSource() will return either the JButton object or the JComboBox object. Depending on which value getSource() returns, you should either remove the last text from the canvas or change its font.
To change the font, you can call the method
setFont(String fontName)of the Text class to change the font used by an existing Text object.
A bit of a hint here. We use getSelectedItem to figure out
which item of the JComboBox is selected. In the examples we've
seen so far in class, the type of variable we used to store the value returned
by this method call was Object. It turns out, we can typecast this
Object to a String, for example:
String choice = (String)menu.getSelectedItem();
If you name your menu entries in an appropriate way, this will greatly simplify your code since you can directly pass your choice string to the setFont method call.
Start by adding a JSlider to your main panel just as you added the JTextField and subpanel. Place it after the subpanel. Look at the GUI cheat sheet to determine what type of listener a JSlider needs. (When you have a class that implements several interfaces, you simply separate the interfaces with commas. Do NOT include the keyword implements more than once in the class header!) Define the appropriate listener method so that when the bubble in the scrollbar is moved, the size of the text changes. The possible font sizes should range from 10 to 48.
Be careful that your program does not crash if you manipulate the controls after removing the last text item from the canvas or before adding the first text item to the canvas.
If you are interested in earning up to 1 point of extra credit: add the capability of changing the color of your text. There are two ways of doing this, one fairly simple worth half a point, while the other is more complicated and worth the full point:
Before submitting your work, make sure that each of the .java files includes a comment containing your name. Also, before turning in your work, be sure to double check both its logical organization and your style of presentation. Make your code as clear as possible and include appropriate comments describing major sections of code and declarations. Use the Format command in the Source menu to make sure your indentation is consistent. Refer to the lab style sheet for more information about style.
Turn in your project the same as in past weeks; though make sure that the folder name begins with your last name (and includes the lab number).
This lab is due Monday at 11 p.m., though I wouldn't be surprised if most of you completed it during the lab period.
Grading Point AllocationsValue | Feature |
Style (5 pts total) | |
1 pts. | Descriptive comments |
1 pts. | Good names and formatting |
1 pts. | Good use of constants |
1 pts. | Good use of private and local variables |
1 pts. | Good use of private methods |
Correctness (5 pts total) | |
1 pt. | Displaying appropriate text at click point |
1 pt. | Changing the font |
1 pt. | Removing the last text |
1 pt. | Changing the font size (and showing its size in the label) |
1 pt. | Avoiding null pointer errors |