Final Project
The importance of a graphical user interface in programming is paramount in being successful in the business industry. This project incorporates GUI techniques with other tools that you have learned about in this class.
Here is your assignment: You work for a Landscape architect. They have asked you to be a part of their team as they need a computer programmer, analyst, and designer to aid them in calculating engineering specification. Specifically, you have been assigned to the Pools, Hot Tubs and Spas section of their landscaping team. Your skills will be needed in creating a GUI program that calculates engineering specifications.
This assignment is due in Week 7, but it is suggested that you begin working on the final project assignment in Week 5, which should give you ample time to complete the project. You will find that all the lectures, Quick Test Programs, and Lab assignments will prepare you for the final project.
Analysis and Design
In week 4, you will complete the analysis and design for the project. You will use the information described in Chapter Two (pages 48-49) to create the Analysis and Design documentation. You will create the following items:
1. Request for New Application
2. Problem Analysis
3. List and description of the requirements
4. Interface Storyboard/drawing
5. Design Flowchart or pseudo code
The Analysis and Design document will be a single MS Word document, which contains all descriptions and drawings.
User's Manual
Include a User's Manual: a Word document, with screenshots, that explains how to run your application. Your mark will depend both on the program quality, and the quality of the User's Manual.
Here are some more detailed guidelines about the User's Manual:
? It does not need to be long, probably not more than 10 pages, including screenshots.
? Write it at the expected user's level - not too technical.
? Detail all the functionality that the application provides. Best, structure you presentation top-down way.
? For each function, show what it its purpose and sample execution, with a screenshot.
Minimum Requirements
This example would be the bare minimum to obtain a passing grade (no more than a 75%):
This Swimming Pool Calculator is one that is very simple to use. As you can see, it is a user interface that will allow the user to enter the desired length, width, and average depth of a pool and the program will calculate the volume of that pool based on the entered information. This information is then stored in a file that can be viewed. The interface contains four text boxes for the user to enter information along with a calculate and an exit button.
Entering Length of Pool
The user will be able to enter the length of the pool(in feet) in the text box shown above. To enter information in the box, the user must click in the desired box. The curser will appear allowing the user to enter the appropriate information. To get to the next box, all the user needs to do is either click with the mouse the next box, or the user can hit the tab button on their keyboard. Either method will move the cursor to the next input.
Entering the Width of the Pool
As with the length box, the width box is used in the same fashion. The user will enter the width of the Pool(in feet) using the keyboard. The user will then have the option to move to the next box by either using the mouse and clicking in the average box, or they can use the tab key on their keyboard. Either way will move the cursor to the average depth box.
Entering the Average Depth
The user will now enter the average depth of the Pool(in feet). This needs to be as exact a measurement as possible. If the user only wants to know an about figure then the measurements do not need to be as accurate. The more accurate the user needs the volume to be, the more accurate the measurements need to be. If a mistake is made, all the user needs to do is click the backspace button on their keyboard and the entered information will be deleted.
Calculating the Volume
The final step is for the user to calculate the volume of the Pool. This can be done in two ways. First, the user can click the calculate button to calculate the volume of the pool. Second, the user can use their keyboard and press Ctrl+C and the program will calculate the volume.
Retrieving Data.Txt file
The Data.txt file can be retrieved by going into the workspace file using Windows Explorer. Once there the user can click on the Data file to retrieve the contents. The contents will be in Word pad form, and will look like the following page.
From this point, the user can either review the document or can change any information. If the user desired, they could make this document a read only document by going into the security settings in Windows and setting the access. This should only be done by the administrator.
Exiting the program
To exit the program, the user can do one of two things. First, the user can click on the Exit button. This will close the program. Two, the user can use the keyboard and click Ctrl+X. Third, the user can click on the red X at the top of the interface. Any of these methods will allow the program to close.
Below are some hints:
??
volume = length * width * depth; //Will calculate the volume of the pool
volumeField.setText(num.format(volume)); //Will display the volume of the pool for the user
{
//Will write to the Data.txt file
try{
FileWriter fileW = new FileWriter("Data.txt", true);
System.out.println("Writing data to Data.txt file");
fileW.write("length:");
fileW.write(lengthField.getText());
fileW.write(",");
fileW.write(" ");
fileW.write("width:");
fileW.write(widthField.getText());
fileW.write(",");
fileW.write(" ");
fileW.write("avg depth:");
fileW.write(depthField.getText());
fileW.write(",");
fileW.write(" ");
fileW.write("volume:");
fileW.write(volumeField.getText());
fileW.write(",");
fileW.close();
FileReader fileR = new FileReader("Data.txt");
BufferedReader buffIn = new BufferedReader(fileR);
String textData = buffIn.readLine();
System.out.println(textData);
buffIn.close();
System.out.println("\n");
}
catch(IOException e1)
{
JOptionPane.showMessageDialog(null,e1.getMessage(), "ERROR",2); //Will display error message if unable to write to file
}
}
Sample Extended Requirements
The following screenshots provide a sample of what is possible, and could lead to a project worth the maximum grade of 100%. Something to think about!!! Do not think this is what you have to do to get the maximum number of points. It is just a sample! You are only bound by your programming ability. Please do not try and do more than you are capable of. Think, plan, design and code YOUR final project. This is your opportunity to show off your skills!
General Interface
The first panel simply displays today?s date ? I opted to not give the user the option to change this, as it uses the system date. Since this is to be a generic tool, we give the user the option to set the company name in the Options tab; doing so will change ?Enter a company name in the Options tab? to the company name. An Exit button is provided in all tabs. Mnemonic values are set for all buttons throughout the application. I opted for a few different error-catching options rather than a consistent one throughout the application, just to show some extra functionality. All tabs can easily be set to behave identically.
Options tab
This tab allows the user to enter a company name. Hitting Set New Name will change the window?s title to whatever was entered.
The result:
Customers tab
General
This tab allows the user to add new customers. It will display the existing customers in the main area.
Functionality
General
The program checks for the existence of the customer.txt file in the directory where the program is located. If the file does not exist, the program tells the customer in the Message Area at the bottom. The Customer Display will give a brief explanation of options.
Add Customer
In this window, the user can enter customers. Again, the Message Area will display program message. The State selection contains a list of all US state abbreviations. No error checking is performed here, as we are dealing with strings ? the information will be stored literally as entered.
Selecting Add Customer will attempt to write the information to customer.txt. If the file is write-protected, the Message Area will inform the user. If the file does not exist, it is created. If the file already exists and can be written to, the information will be appended to the file. Fields are not emptied when focused on (this can easily be changed).
Below is an example of the situation where the file already exists and can be written to; notice the message in the Message Area:
The Delete File option gives the user the option to delete the customer.txt file. If the file does not exist, the Message Area will inform the user after Yes has been selected. If No is selected, the Customer File Deletion window simply closes.
 
After a customer has been added and the Customers window is closed, the user selects Refresh to refresh the Customer Display area, which will display the contents of the customer.txt file. It can be changed to automatically refresh the contents, but I preferred to give the user some more control.
Contractor tab
The Contractor tab functions in exactly the same way as the Customers tab. The file name for contractors is contractor.txt.
Pools tab
General
This tab allows for pool volume calculation. It assumes pools are rectangular (round- and oval-shaped pools/tubs can be calculated in the Hot Tubs tab.
Functionality
After a length, width, and depth have been entered, the user clicks Calculate Volume and the program will display the calculated volume (length * width * depth in cubic feet).
Anomalies caught are no input and invalid input. The user is limited to entering only numbers and periods.
No input:
Invalid input:
Combination invalid input/no input:
Hot Tubs tab
General
This tab allows for round and oval tubs? volumes to be calculated.
Functionality
When Round Tub is selected, the user cannot fill out the width field, as it should be the same as the length (since we?re dealing with a circle). After filling out the length and tub fields, the Calculate Volume button will display the volume (Pi * ((length/2)^2) * depth). The width is automatically set to the same value as the length, and the user is informed.
Again, invalid input is caught (only numbers and periods are allowed), and the program will display the same error messages as the Pools tab.
When Oval Tub is selected, the width field is opened up:
Again, the same error messages are displayed in case of no input/invalid values.
Formula used is (Pi * (length * width)^2) * depth.
Temp Calc tab
General
This tab offers a temperature converter (Celsius <-> Fahrenheit).
Functionality
The user enters a temperature, and selects either C or F. The field after the Result display will display the opposite (if C is selected, field will display F; if F is selected, field will display C). I took a different approach for the System Messages here, but the principle is the same. In this tab, the temperature value will be automatically set to zero in case of invalid input or no input.
No input:
Length Calc tab
General
This tab offers the user a length converter (millimeters, meters, yards, feet, and inches).
Functionality
The user fills out one of the fields (only numerical values and the period are allowed), and hits Convert. Only one of the fields can be filled out at any time (changing the focus will delete the other fields).
The end?
In order to get full credit which is 100 points, you need to provide a lot of extra functionalities. I prepared the following 4 examples to assist you.
1. The JTabbedPane class
2. Checkboxes and radio buttons
3. Lists and combo boxes
4. Scrollbars and sliders
The JTabbedPane Class
If you've ever dealt with the Control Panel in Windows, you already know what a JTabbedPane is. It's a container with labeled tabs. When you click on a tab, a new set of controls is shown in the body of the JTabbedPane. In Swing, JTabbedPane is simply a specialized container.
Each tab has a name. To add a tab to the JTabbedPane, simply call addTab(). You'll need to specify the name of the tab as well as a component that supplies the tab's contents. Typically, it's a container holding other components.
Even though the JTabbedPane only shows one set of components at a time, be aware that all the components on all the pages are in memory at one time. If you have components that hog processor time or memory, try to put them into some "sleep" state when they are not showing.
The following example create five tabs, each tab have their own components.
import java.awt.*;
import javax.swing.*;
public class TabDemo extends JFrame
{
public TabDemo()
{
super( "JTabbedPane Demo " );
JTabbedPane tab = new JTabbedPane();
// constructing the first panel
JLabel l1 = new JLabel( "Welcome to CIS355A", SwingConstants.CENTER );
JPanel p1 = new JPanel();
p1.add( l1 );
tab.addTab( "Tab#1", null, p1, " Panel #1" );
// constructing the second panel
JLabel l2 = new JLabel("Welcome to JTabbedPaneDemo", SwingConstants.CENTER);
JPanel p2 = new JPanel();
p2.setBackground( Color.blue );
p2.add( l2 );
tab.addTab( "Tab#2", null, p2, " Panel #2" );
// constructing the third panel
JLabel l3 = new JLabel( " Java is sooooooooooo fun!" );
JPanel p3 = new JPanel();
p3.setLayout( new BorderLayout() );
p3.add( new JButton( "J" ), BorderLayout.NORTH );
p3.add( new JButton( "A" ), BorderLayout.WEST );
p3.add( new JButton( "V" ), BorderLayout.EAST );
p3.add( new JButton( "A" ), BorderLayout.SOUTH );
p3.add( l3, BorderLayout.CENTER );
tab.addTab( "Tab#3", null, p3, " Panel #3" );
// constructing the fourth panel
JLabel l4 = new JLabel( "panel four" );
JPanel p4 = new JPanel();
p4.setBackground( Color.black );
JLabel digits = new JLabel( "Java" );
digits.setFont( new Font( "Serif", Font.ITALIC, 50 ) );
digits.setForeground( Color.blue );
p4.add( digits );
tab.addTab( "Tab#4", null, p4, " Panel #4" );
// constructing the last panel
JLabel l5 = new JLabel( "panel five" );
JLabel l6 = new JLabel( "The end!" );
JPanel p5 = new JPanel();
p5.add( l6 );
tab.addTab( "Tab#5", null, p5, " Panel #5" );
// add JTabbedPane to container
getContentPane().add( tab );
setSize( 250, 200 );
setVisible( true );
}
public static void main( String args[] )
{
TabDemo demo = new TabDemo();
demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
Checkboxes and Radio Buttons
A checkbox is a labeled toggle switch. Each time the user clicks it, its state toggles between checked and unchecked. Swing implements the checkbox as a special kind of button. Radio buttons are similar to checkboxes, but they are normally used in groups. Clicking on one radio button in the group automatically turns the others off. They are named for the mechanical preset buttons on old car radios (like some of us had in high school).
Checkboxes and radio buttons are represented by instances of JCheckBox and JRadioButton, respectively. Radio buttons can be tethered together using an instance of another class called ButtonGroup . By now you're probably well into the swing of things (no pun intended) and could easily master these classes on your own. We'll use an example to illustrate a different way of dealing with the state of components and to show off a few more things about containers.
A JCheckBox sends ItemEvent s when it's pushed. Because a checkbox is a kind of button, it also fires ActionEvents when checked. For something like a checkbox, we might want to be lazy and check on the state of the buttons only at some later time, such as when the user commits an action. For example, when filling out a form you may only care about the user's choices when the submit button is pressed.
DriveThrough prints the results when we press the Place Order button. Therefore, we can ignore all the events generated by our checkboxes and radio buttons and listen only for the action events generated by the regular button.
//file: DriveThrough.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DriveThrough
{
public static void main(String[] args) {
JFrame frame = new JFrame("Lister v1.0");
JPanel entreePanel = new JPanel( );
final ButtonGroup entreeGroup = new ButtonGroup( );
JRadioButton radioButton;
entreePanel.add(radioButton = new JRadioButton("Beef"));
radioButton.setActionCommand("Beef");
entreeGroup.add(radioButton);
entreePanel.add(radioButton = new JRadioButton("Chicken"));
radioButton.setActionCommand("Chicken");
entreeGroup.add(radioButton);
entreePanel.add(radioButton = new JRadioButton("Veggie", true));
radioButton.setActionCommand("Veggie");
entreeGroup.add(radioButton);
final JPanel condimentsPanel = new JPanel( );
condimentsPanel.add(new JCheckBox("Ketchup"));
condimentsPanel.add(new JCheckBox("Mustard"));
condimentsPanel.add(new JCheckBox("Pickles"));
JPanel orderPanel = new JPanel( );
JButton orderButton = new JButton("Place Order");
orderPanel.add(orderButton);
Container content = frame.getContentPane( );
content.setLayout(new GridLayout(3, 1));
content.add(entreePanel);
content.add(condimentsPanel);
content.add(orderPanel);
orderButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
String entree =
entreeGroup.getSelection( ).getActionCommand( );
System.out.println(entree + " sandwich");
Component[] components = condimentsPanel.getComponents( );
for (int i = 0; i < components.length; i++) {
JCheckBox cb = (JCheckBox)components[i];
if (cb.isSelected( ))
System.out.println("With " + cb.getText( ));
}
}
});
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300, 150);
frame.setVisible(true);
}
}
DriveThrough lays out three panels. The radio buttons in the entreePanel are tied together through a ButtonGroup object. We add() the buttons to a ButtonGroup to make them mutually exclusive. The ButtonGroup object is an odd animal. One might expect it to be a container or a component, but it isn't; it's simply a helper object that allows only one RadioButton to be selected at a time.
In this example, the button group forces you to choose a beef, chicken, or veggie entree, but not more than one. The condiment choices, which are JCheckBoxes, aren't in a button group, so you can request any combination of ketchup, mustard, and pickles on your sandwich.
When the Place Order button is pushed, we receive an ActionEvent in the actionPerformed() method of our inner ActionListener. At this point, we gather the information in the radio buttons and checkboxes and print it. actionPerformed() simply reads the state of the various buttons. We could have saved references to the buttons in a number of ways; this example demonstrates two. First, we find out which entree was selected. To do so, we call the ButtonGroup's getSelection() method. This returns a ButtonModel, upon which we immediately call getActionCommand(). This returns the action command as we set it when we created the radio buttons. The action commands for the buttons are the entrée names, which is exactly what we need.
To find which condiments were selected, we use a more complicated procedure. The problem is that condiments aren't mutually exclusive, so we don't have the convenience of a ButtonGroup. Instead, we ask the condiments JPanel for a list of its components. The getComponents() method returns an array of references to the container's child components. We'll use this to loop over the components and print the results. We cast each element of the array back to JCheckBox and call its isSelected() method to see if the checkbox is on or off. If we were dealing with different types of components in the array, we could determine each component's type with the instanceof operator. Or, more generally, we could maintain references to the elements of our form in some explicit way (a map by name,perhaps).
Lists and Combo Boxes
JLists and JComboBoxes are a step up on the evolutionary chain from JButtons and JLabels. Lists let the user choose from a group of alternatives. They can be configured to force a single selection or allow multiple choices. Usually, only a small group of choices is displayed at a time; a scrollbar lets the user move to the choices that aren't visible. The user can select an item by clicking on it. She can expand the selection to a range of items by holding down Shift and clicking on another item. To make discontinuous selections, the user can hold down the Control key instead of the Shift key (on a Mac, this is the Command key).
A combo box is a cross-breed between a text field and a list. It displays a single line of text (possibly with an image) and a downward pointing arrow on one side. If you click on the arrow, the combo box opens up and displays a list of choices. You can select a single choice by clicking on it. After a selection is made, the combo box closes up; the list disappears, and the new selection is shown in the text field.
Like other components in Swing, lists and combo boxes have data models that are distinct from visual components. The list also has a selection model that controls how selections may be made on the list data.
Lists and combo boxes are similar because they have similar data models. Each is simply an array of acceptable choices. This similarity is reflected in Swing, of course: the type of a JComboBox's data model is a subclass of the type used for a JList's data model. The next example demonstrates this relationship.
The following example creates a window with a combo box, a list, and a button. The combo box and the list use the same data model. When you press the button, the program writes out the current set of selected items in the list.
Here's the code for the example:
//file: Lister.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lister {
public static void main(String[] args) {
JFrame frame = new JFrame("Lister v1.0");
// create a combo box
String [] items = { "uno", "due", "tre", "quattro", "cinque",
"sei", "sette", "otto", "nove", "deici",
"undici", "dodici" };
JComboBox comboBox = new JComboBox(items);
comboBox.setEditable(true);
// create a list with the same data model
final JList list = new JList(comboBox.getModel( ));
// create a button; when it's pressed, print out
// the selection in the list
JButton button = new JButton("Per favore");
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
Object[] selection = list.getSelectedValues( );
System.out.println("-----");
for (int i = 0; i < selection.length; i++)
System.out.println(selection[i]);
}
});
// put the controls the content pane
Container c = frame.getContentPane( );
JPanel comboPanel = new JPanel( );
comboPanel.add(comboBox);
c.add(comboPanel, BorderLayout.NORTH);
c.add(new JScrollPane(list), BorderLayout.CENTER);
c.add(button, BorderLayout.SOUTH);
frame.setSize(200, 200);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
}
The combo box is created from an array of strings. This is a convenience?behind the scenes, the JComboBox constructor creates a data model from the strings you supply and sets the JComboBox to use that data model. The list is created using the data model of the combo box. This works because JList expects to use a ListModel for its data model, and the ComboBoxModel used by the JComboBox is a subclass of ListModel.
The button's action event handler simply prints out the selected items in the list, which are retrieved with a call to getSelectedValues(). This method actually returns an object array, not a string array. List and combo box items, like many other things in Swing, are not limited to text. You can use images, drawings, or some combination of text and images.
You might expect that selecting one item in the combo box would select the same item in the list. In Swing components, selection is controlled by a selection model. The combo box and the list have distinct selection models; after all, you can select only one item from the combo box while it's possible to select multiple items from the list. Thus, while the two components share a data model, they have separate selection models.
We've made the combo box editable. By default, it would not be editable: the user could choose only one item in the drop-down list. With an editable combo box, the user can type in a selection, as if it were a text field. Noneditable combo boxes are useful if you just want to offer a limited set of choices; editable combo boxes are handy when you want to accept any input but offer some common choices.
There's a great class tucked away in the last example that deserves some recognition. It's JScrollPane. In Lister, you'll notice we created one when we added the List to the main window. JScrollPane simply wraps itself around another Component and provides scrollbars as necessary. The scrollbars show up if the contained Component's preferred size (as returned by getPreferredSize() ) is greater than the size of the JScrollPane itself.
The following example combins all the above choice boxes and layout managers in one application ? Language.
import java.awt.*;
import javax.swing.*;
public class Language extends JFrame {
private JButton button1, button2, button3, button4;
private JCheckBox check1, check2, check3, check4;
private JRadioButton radio1, radio2, radio3;
private ButtonGroup radioGroup;
private JComboBox comboBox;
private JLabel label1, label2;
private JPanel panel1, panel2, panel3, panel4, panel5,panel6, panel7, panel8;
public Language()
{
super( "Language Choice" );
// build left north panel
label1 = new JLabel( "My favorite language" );
panel1 = new JPanel();
panel1.setLayout( new FlowLayout( FlowLayout.LEFT ) );
panel1.add( label1 );
// build right east panel
button1 = new JButton( "Reset" );
button2 = new JButton( "Clear" );
button3 = new JButton( "Confirm" );
button4 = new JButton( "Help" );
panel2 = new JPanel();
panel2.setLayout( new GridLayout( 4, 1, 5, 5 ) );
panel2.add( button1 );
panel2.add( button2 );
panel2.add( button3 );
panel2.add( button4 );
// build left south panel
label2 = new JLabel( "Class " );
comboBox = new JComboBox();
comboBox.addItem( "CIS170" );
comboBox.addItem( "CIS247" );
comboBox.addItem( "CIS355" );
check1 = new JCheckBox( "Currently taking" );
panel3 = new JPanel();
panel3.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 0 ) );
panel3.add( label2 );
panel3.add( comboBox );
panel3.add( check1 );
// build left east panel
check2 = new JCheckBox( "C++" );
check3 = new JCheckBox( "C#" );
check4 = new JCheckBox( "Java", true );
panel4 = new JPanel();
panel4.setLayout( new BorderLayout( ) );
panel4.add( check2, BorderLayout.NORTH );
panel4.add( check3, BorderLayout.CENTER );
panel4.add( check4, BorderLayout.SOUTH );
// build left west panel
panel5 = new JPanel();
panel5.setLayout( new BorderLayout() );
panel5.add( radio1 = new JRadioButton( "Strongly Recommend",true ), BorderLayout.NORTH );
panel5.add( radio2 = new JRadioButton( "Recommend", false ), BorderLayout.CENTER );
panel5.add( radio3 = new JRadioButton( "Not Recommend", false ), BorderLayout.SOUTH );
// group the radio buttons
radioGroup = new ButtonGroup();
radioGroup.add( radio1 );
radioGroup.add( radio2 );
radioGroup.add( radio3 );
// build left center
panel8 = new JPanel();
panel8.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 0 ) );
panel8.setBackground( Color.white );
panel8.add( panel4 );
panel8.add( panel5 );
// setup left panel
panel6 = new JPanel();
panel6.setLayout( new BorderLayout() );
panel6.add( panel1, BorderLayout.NORTH );
panel6.add( panel8, BorderLayout.CENTER );
panel6.add( panel3, BorderLayout.SOUTH );
// setup layout
panel7 = new JPanel();
panel7.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 0 ) );
panel7.add( panel6 );
panel7.add( panel2 );
getContentPane().add( panel7 );
setSize( 500, 160 );
setVisible( true );
} // end constructor
public static void main( String args[] )
{
Language myChoice = new Language();
myChoice.setDefaultCloseOperation( EXIT_ON_CLOSE );
}
}
Scrollbars and Sliders
JScrollPane is such a handy component that you may not ever need to use scrollbars by themselves. In fact, if you ever do find yourself using a scrollbar by itself, chances are you really want to use another component called a slider.
There's not much point in describing the appearance and functionality of scrollbars and sliders. Instead, let's jump right in with an example that includes both components. The following case shows a simple example with both a scrollbar and a slider.
//file: Slippery.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Slippery {
public static void main(String[] args)
{
JFrame frame = new JFrame("Slippery v1.0");
Container content = frame.getContentPane( );
JPanel main = new JPanel(new GridLayout(2, 1));
JPanel scrollBarPanel = new JPanel( );
final JScrollBar scrollBar =
new JScrollBar(JScrollBar.HORIZONTAL, 0, 48, 0, 255);
int height = scrollBar.getPreferredSize( ).height;
scrollBar.setPreferredSize(new Dimension(175, height));
scrollBarPanel.add(scrollBar);
main.add(scrollBarPanel);
JPanel sliderPanel = new JPanel( );
final JSlider slider =
new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
slider.setMajorTickSpacing(48);
slider.setMinorTickSpacing(16);
slider.setPaintTicks(true);
sliderPanel.add(slider);
main.add(sliderPanel);
content.add(main, BorderLayout.CENTER);
final JLabel statusLabel =
new JLabel("Welcome to Slippery v1.0");
content.add(statusLabel, BorderLayout.SOUTH);
// wire up the event handlers
scrollBar.addAdjustmentListener(new AdjustmentListener( ) {
public void adjustmentValueChanged(AdjustmentEvent e) {
statusLabel.setText("JScrollBar's current value = "
+ scrollBar.getValue( ));
}
});
slider.addChangeListener(new ChangeListener( ) {
public void stateChanged(ChangeEvent e) {
statusLabel.setText("JSlider's current value = "
+ slider.getValue( ));
}
});
frame.pack( );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
}
All we've really done here is added a JScrollBar and a JSlider to our main window. If the user adjusts either of these components, the current value of the component is displayed in a JLabel at the bottom of the window.
The JScrollBar and JSlider are both created by specifying an orientation, either HORIZONTAL or VERTICAL. You can also specify the minimum and maximum values for the components, as well as the initial value. The JScrollBar supports one additional parameter, the extent. The extent simply refers to what range of values is represented by the slider within the scroll bar. For example, in a scrollbar that runs from 0 to 255, an extent of 128 means that the slider will be half the width of the scrollable area of the scrollbar.
JSlider supports the idea of tick marks, lines drawn at certain values along the slider's length. Major tick marks are slightly larger than minor tick marks. To draw tick marks, just specify an interval for major and minor tick marks, and then paint the tick marks:
slider.setMajorTickSpacing(48);
slider.setMinorTickSpacing(16);
slider.setPaintTicks(true);
JSlider also supports labeling the ticks with text strings, using the setLabelTable() method.
Responding to events from the two components is straightforward. The JScrollBar sends out AdjustmentEvents every time something happens; the JSlider fires off ChangeEvents when its value changes. In our simple example, we display the new value of the changed component in the JLabel at the bottom of the window.
Grading scales of final project
1. You will get 75% which is 75 points out of 100 points if you only finished the basic calculation with an appropriate user?s manual. The user?s manual worth 20 points, the program source file worth 55 points.
After each calculation, the collected information and the calculated volume will be saved into a file (or database). You will then add a function to retrieve the input and calculation history from the and display it on the screen.
2. You will get 90% which is 90 points out of 100 points if you designed the following GUI with functioning Pool and Spa tab with an appropriate user?s manual. The user?s manual worth 20 points, the program source file worth 70 points.
3. You will get 100% which is 100 point out of 100 points if you designed the following GUI with all five functioning tab which provides capabilities to calculate the volume and add Customer, Vendors and Contractors info with an appropriate user?s manual. The user?s manual worth 20 points, the program source file worth 80 points.
View Answers