© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline
3.1 Test-Driving the Inventory Application
3.2 Customizing JLabels on the Inventory Application
3.3 Customizing JTextFields and a JButton in the Inventory Application
3.4 Wrap-Up
Application
Introducing JTextField s and JButton s
Objectives
• In this tutorial, you will learn to:
– Use graphical user interface design guidelines to create a useful GUI.
– Customize JLabel s, JTextField s and a JButton in an application window.
– Align text horizontally in a JTextField .
– Specify that a JTextField is uneditable.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Application Requirements
A college bookstore receives cartons of textbooks. In each shipment, each carton contains the same number of textbooks. The inventory manager wants to use a computer to calculate the total number of textbooks arriving at the bookstore for each shipment. The inventory manager will enter the number of cartons received and the fixed number of textbooks in each carton of the shipment; the application should then calculate and display the total number of textbooks in the shipment.
3.1 Test-Driving the Inventory Application (Cont.)
Figure 3.1 Inventory application JFrame with default data displayed in the input JTextFields.
Input JTextFields Output JTextField
JButton
• JTextField s
– Enables a user to input data from the keyboard – Display data as output to the user
• JButton s
– Component the user can click to cause the application to perform
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
(Cont.)
Figure 3.2 Inventory application after you enter new quantities.
Descriptive JLabels
• Input data into application
– Enter
3in the
Cartons per shipment: JTextField– Enter
15in the
Items per carton: JTextField3.1 Test-Driving the Inventory Application (Cont.)
Figure 3.3 Calculating the total number of items received.
Result of calculation displayed in an uneditable JTextField
• Click the Calculate Total JButton
– Result of calculation displays in an uneditable
JTextField© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Application
Figure 3.4 JLabels in the Inventory application.
Descriptive JLabels
3.2 Customizing JLabel s in the Inventory Application (Cont.)
Figure 3.5 Setting the Cartons per shipment: JLabel’s text and bounds.
Set the text and bounds of the
cartonsJLabel
• Setting the text displayed in the JLabel
– Use the
setTextmethod to set the text property – Use sentence-style capitalization
• Setting the bounds of the JLabel
– Use the
setBoundsmethod to set the bounds property
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Application (Cont.)
Figure 3.6 Updated application with Cartons per shipment:
JLabel.
Upper-left corner of
JLabel appears at 16, 16
3.2 Customizing JLabel s in the Inventory Application (Cont.)
Figure 3.7 Setting the Items per carton: JLabel’s text and bounds.
Set the text and bounds of the itemsJLabel
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Application (Cont.)
Figure 3.8 Setting the Total: JLabel’s text and bounds.
Set the text and bounds of the totalJLabel
3.2 Customizing JLabel s in the Inventory Application (Cont.)
Figure 3.9 Running Inventory Application with three customized JLabels.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
JButton in the Inventory Application
Figure 3.10 Setting the Cartons per shipment: JTextField’s properties.
Set the text, bounds and alignment of the
cartonsJTextField
• Setting the text displayed in the JTextField
– Use the
setTextmethod to set the text property
• Setting the bounds of the JTextField
– Use the
setBoundsmethod to set the bounds property
3.3 Customizing JTextField s and a
JButton in the Inventory Application (Cont.)
Figure 3.11 Setting the Items per carton: JTextField’s properties.
Set the text, bounds and alignment of the
itemsJTextField
• Setting the horizontal alignment of the JLabel
– Use the
setHorizontalAlignmentmethod to set the horizontal alignment property
• JTextField.RIGHT
• JTextField.LEFT
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
JButton in the Inventory Application (Cont.)
Figure 3.12 Setting the Total: JTextField’s bounds and horizontal alignment.
Set the bounds and alignment of the
totalResultJTextField
3.3 Customizing JTextField s and a
JButton in the Inventory Application (Cont.)
Figure 3.13 Changing the editable property of the Total: JTextField.
Set the editable property of the totalResultJTextField
to false to prevent the user from modifying it
• For an output JTextField
– Use the
setEditablemethod to allow or prevent the user from
modifying the contents of the
JTextField© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
JButton in the Inventory Application (Cont.)
Figure 3.14 Running Inventory Application with customized JTextFields.
3.3 Customizing JTextField s and a
JButton in the Inventory Application (Cont.)
Figure 3.15 Customizing the CalculateTotal JButton.
Set the text and bounds of the calculateJButton
• Adding a JButton
– Set text property
– Use book-title capitalization
– Set bounds property
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
JButton in the Inventory Application (Cont.)
Figure 3.16 Running the application after customizing the Calculate Total JButton.
Outline
Inventory.java (1 of 5)
2 // Calculates the number of items in a shipment based on the number 3 // of cartons received and the number of items per carton.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class Inventory extends JFrame 9 {
10 // JLabel and JTextField for cartons per shipment 11 private JLabel cartonsJLabel;
12 private JTextField cartonsJTextField;
13
14 // JLabel and JTextField for items per carton 15 private JLabel itemsJLabel;
16 private JTextField itemsJTextField;
17
18 // JLabel and JTextField for total items per shipment 19 private JLabel totalJLabel;
20 private JTextField totalResultJTextField;
21
22 // JButton to initiate calculation of total items per shipment 23 private JButton calculateJButton;
2004 Prentice Hall, Inc.
All rights reserved.
Inventory.java (2 of 5)
28 createUserInterface();
29 } 30
31 // create and position GUI components; register event handlers 32 public void createUserInterface()
33 {
34 // get content pane and set layout to null 35 Container contentPane = getContentPane();
36 contentPane.setLayout( null );
37
38 // set up cartonsJLabel
39 cartonsJLabel = new JLabel();
40 cartonsJLabel.setText( "Cartons per shipment:" );
41 cartonsJLabel.setBounds( 16, 16, 130, 21 );
42 contentPane.add( cartonsJLabel );
43
44 // set up itemsJLabel
45 itemsJLabel = new JLabel();
46 itemsJLabel.setText( "Items per carton:"
47 itemsJLabel.setBounds( 16, 48, 104, 21 );
48 contentPane.add( itemsJLabel );
49
Set the text and bounds of the
cartonsJLabel
Set the text and bounds of the
itemsJLabel
Outline
Inventory.java (3 of 5)
51 totalJLabel = new JLabel();
52 totalJLabel.setText( "Total:" );
53 totalJLabel.setBounds( 204, 16, 40, 21 );
54 contentPane.add( totalJLabel );
55
56 // set up cartonsJTextField
57 cartonsJTextField = new JTextField();
58 cartonsJTextField.setText( "0" );
59 cartonsJTextField.setBounds( 148, 16, 40, 21 );
60 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT );
61 contentPane.add( cartonsJTextField );
62
63 // set up itemsJTextField
64 itemsJTextField = new JTextField();
65 itemsJTextField.setText( "0" );
66 itemsJTextField.setBounds( 148, 48, 40, 21 );
67 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT );
68 contentPane.add( itemsJTextField );
69
Set the text and bounds of the
totalJLabel
Set the text, bounds and alignment of the
cartonsJTextField
Set the text, bounds and alignment of the
itemsJTextField
2004 Prentice Hall, Inc.
All rights reserved.
Inventory.java (4 of 5)
73 totalResultJTextField.setHorizontalAlignment(
74 JTextField.RIGHT );
75 totalResultJTextField.setEditable( false );
76 contentPane.add( totalResultJTextField );
77
78 // set up calculateJButton
79 calculateJButton = new JButton();
80 calculateJButton.setText( "Calculate Total" );
81 calculateJButton.setBounds( 204, 48, 126, 24 );
82 contentPane.add( calculateJButton );
83
84 // set properties of window
85 setSize( 354, 112 ); // set window size
86 setTitle( "Inventory" ); // set title bar string 87 setVisible( true ); // display window
88
89 } // end method createUserInterface 90
Set the bounds, alignment and editability of the
totalResultJText- Field
Set the text and bounds of the
calculateJButton
Outline
Inventory.java (5 of 5)
92 public static void main( String[] args ) 93 {
94 Inventory application = new Inventory();
95 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
96
97 } // end method main 98
99 } // end class Inventory