Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
TCU CoSc Programming with Java Handling Events.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
Graphical User Interface Components: Part 1
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Advanced Java and Android Programming Day 1: Introduction to GUI Programming Intro to GUI Programming1.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
GUI Programming in Java: Event Handling & More Components Corresponds with Chapter 14, Chapter 15.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Graphical User Interface Components: Part 1 Chapter 11.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
GUIs & Event-Driven Programming Chapter 11 Review.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
A First Look at GUI Applications
Java GUI.
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Chapter 14: Introduction to Swing Components

Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend the JFrame class 2Java Programming, Seventh Edition

Objectives (cont’d.) Add JTextField s, JButton s, and tool tips to a JFrame Learn about event-driven programming Understand Swing event listeners Use the JCheckBox, ButtonGroup, and JComboBox classes 3Java Programming, Seventh Edition

Understanding Swing Components UI components – Buttons, text fields, and other components with which the user can interact Swing components – Descendants of JComponent – Inherit from the java.awt.Container class To take advantage of the Swing UI components and their methods, insert import javax.swing.*; at the beginning of Java program files 4Java Programming, Seventh Edition

Understanding Swing Components (cont’d.) Container – A type of component that holds other components – Can treat a group as a single entity – Defined in the Container class – Often takes the form of a window that you can: Drag Resize Minimize Restore Close 5Java Programming, Seventh Edition

Window class – A child of Container – Does not have title bars or borders – Is rarely used – Instead, use the following subclasses: Frame JFrame 6Java Programming, Seventh Edition Understanding Swing Components (cont’d.)

Using the JFrame Class 7Java Programming, Seventh Edition

Using the JFrame Class (cont’d.) Create a JFrame so you can place other objects within it for display The JFrame class has four constructors: – JFrame() – JFrame(String title) – JFrame(GraphicsConfiguration gc) – JFrame(String title, GraphicsConfiguration gc) 8Java Programming, Seventh Edition

9

Using the JFrame Class (cont’d.) Create JFrame JFrame firstFrame = new JFrame("Hello"); Set size and title firstFrame.setSize(200, 100); firstFrame.setTitle("My frame"); 10Java Programming, Seventh Edition

Using the JFrame Class (cont’d.) 11Java Programming, Seventh Edition

Using the JFrame Class (cont’d.) To close JFrame, click the Close button – JFrame becomes hidden and the application keeps running Default behavior – To change this behavior, use the setDefaultCloseOperation() method 12Java Programming, Seventh Edition

Customizing a JFrame ’s Appearance Window decorations – Icon and buttons Look and feel – The default appearance and behavior of a user interface – The setDefaultLookAndFeelDecorated() method sets JFrame ’s look and feel 13Java Programming, Seventh Edition

Customizing a JFrame ’s Appearance (cont’d.) 14Java Programming, Seventh Edition

Using the JLabel Class JLabel – Holds text you can display – Available constructors – Methods: add() method remove() method setText() method getText() method 15Java Programming, Seventh Edition

Changing a JLabel ’s Font Font class – Creates an object that holds typeface and size information – To construct a Font object, you need three arguments: Typeface Style Point size setFont() method – Requires a Font object argument 16Java Programming, Seventh Edition

Changing a JLabel ’s Font (cont’d.) 17 Figure The JFrame4 program Java Programming, Seventh Edition

Using a Layout Manager Layout manager – A class that controls component positioning BorderLayout – The normal (default) behavior of a JFrame – Divides a container into regions Flow layout manager – Places components in a row 18Java Programming, Seventh Edition

Extending the JFrame Class When you create a class that descends from the JFrame class: – You can set the JFrame ’s properties within your object’s constructor – Then, when the JFrame child object is created, it is automatically endowed with the features you specified Create a child class using the keyword extends Call the parent class’s constructor method using the keyword super 19Java Programming, Seventh Edition

Extending the JFrame Class (cont’d.) 20 Figure The JMyFrame Class Java Programming, Seventh Edition

Adding JTextField s, JButton s, and Tool Tips to a JFrame In addition to including JLabel objects, JFrame s often contain other window features, such as JTextField s, JButton s, and tool tips 21Java Programming, Seventh Edition

Adding JTextField s JTextField – A component into which a user can type a single line of text data – Has several constructors – Methods: setText() method getText() method setEditable() method 22Java Programming, Seventh Edition

Adding JButton s JButton – Click with a mouse to make a selection – Has five constructors – Methods: setText() getText() add() method – Adds a JButton to a JFrame 23Java Programming, Seventh Edition

Adding JButton s (cont’d.) When clicked, no resulting actions occur – The code has not yet been written to handle user-initiated events 24Java Programming, Seventh Edition

Using Tool Tips Tool tips – Popup windows – Help a user understand the purpose of components in an application – Appear when a user hovers the mouse pointer over the component setToolTipText() method – Set a tool tip for a Component 25Java Programming, Seventh Edition

Learning About Event-Driven Programming Event – Occurs when a user takes action on a component, such as clicking the mouse on a JButton object Event-driven program – A program in which the user might initiate any number of events in any order Source – The component on which an event is generated Listener – The object that is interested in an event 26Java Programming, Seventh Edition

Learning About Event-Driven Programming (cont’d.) To respond to user events within any class you create, you must: – Prepare your class to accept event messages – Tell your class to expect events to happen – Tell your class how to respond to events 27Java Programming, Seventh Edition

Preparing Your Class to Accept Event Messages Import the java.awt.event package Add the phrase implements ActionListener to the class header Implementing ActionListener provides you with standard event method specifications that allow your listener to work with ActionEvent s 28Java Programming, Seventh Edition

Telling Your Class to Expect Events to Happen addActionListener() method aButton.addActionListener(this); – Causes any ActionEvent messages (button clicks) that come from aButton to be sent to “this current object” 29Java Programming, Seventh Edition

Telling Your Class How to Respond to Events The ActionListener interface contains the actionPerformed(ActionEvent e) method specification – The body of the method contains any statements that you want to execute when the action occurs When more than one component is added and registered to a JFrame, it might be necessary to determine which component was used – Find the source of the event using getSource(); 30Java Programming, Seventh Edition

Using the setEnabled() Method setEnabled() method – Makes a component unavailable, and then makes it available again in turn – Use after a specific series of actions has taken place 31Java Programming, Seventh Edition

Understanding Swing Event Listeners Classes that respond to user-initiated events must implement an interface that deals with events called event listeners Many types of listeners exist in Java – Each can handle a specific event type A class can implement as many event listeners as it needs An event occurs every time the user types a character or clicks the mouse button 32Java Programming, Seventh Edition

Understanding Swing Event Listeners (cont’d.) 33Java Programming, Seventh Edition

Understanding Swing Event Listeners (cont’d.) Create relationships between Swing components and classes that react to users’ manipulations of them JCheckBox responds to the user’s clicks – addItemListener() method – Register JCheckBox as a type of object that can create an ItemEvent – Format: theSourceOfTheEvent.addListenerMethod (theClassThatShouldRespond); 34Java Programming, Seventh Edition

Understanding Swing Event Listeners (cont’d.) 35Java Programming, Seventh Edition

Understanding Swing Event Listeners (cont’d.) The class of the object that responds to an event contains a method that accepts the event object created by the user’s action – Specific methods react to specific event types If you declare a class that handles an event, create a class to do one of the following: – Implement a listener interface – Extend a class that implements a listener interface 36Java Programming, Seventh Edition

Understanding Swing Event Listeners (cont’d.) If you declare a class that extends MyFrame, you need not include implements ItemListener in its header You must register each instance of the event- handling class as a listener for one or more components 37Java Programming, Seventh Edition

Using the JCheckBox, ButtonGroup, and JComboBox Classes Besides JButton s and JTextField s, several other Java components allow a user to make selections in a UI environment 38Java Programming, Seventh Edition

The JCheckBox Class JCheckBox – Consists of a label positioned beside a square – Click the square to display or remove a check mark – Use to allow the user to turn an option on or off Constructors: JCheckBox() JCheckBox("Check here") JCheckBox("Check here", false) 39Java Programming, Seventh Edition

The JCheckBox Class (cont’d.) 40 Figure Output of the CheckBoxDemonstration class Java Programming, Seventh Edition

The JCheckBox Class (cont’d.) 41Java Programming, Seventh Edition

The JCheckBox Class (cont’d.) Methods: – setText() – setSelected() – isSelected() When the status of JCheckBox changes from unchecked to checked: – An ItemEvent is generated – The itemStateChanged() method executes 42Java Programming, Seventh Edition

The ButtonGroup Class ButtonGroup – Groups several components so that the user can select only one at a time When you group JCheckBox objects, all of the other JCheckBox es are automatically turned off when the user selects any one check box 43Java Programming, Seventh Edition

The ButtonGroup Class (cont’d.) To create a ButtonGroup in a JFrame and then add JCheckBox : – Create a ButtonGroup ButtonGroup aGroup = new ButtonGroup(); – Create a JCheckBox JCheckBox aBox = new JCheckBox(); – Add aBox to aGroup aGroup.add(aBox); 44Java Programming, Seventh Edition

The JComboBox Class JComboBox – A component that combines two features: A display area showing a default option A list box containing additional, alternate options – When the user clicks the JComboBox, a list of alternative items drops down If the user selects one, it replaces the box’s displayed item 45Java Programming, Seventh Edition

The JComboBox Class (cont’d.) 46 Figure A JComboBox before and after the user clicks it Java Programming, Seventh Edition

The JComboBox Class (cont’d.) To build a JComboBox : – Use a constructor with no arguments and then add items with the addItem() method – Alternatively, use an array of Object s as the constructor argument String[] majorArray = {"English", "Math", "Sociology"}; JComboBox majorChoice = new JComboBox(majorArray); 47Java Programming, Seventh Edition

48Java Programming, Seventh Edition

The JComboBox Class (cont’d.) setSelectedItem() or setSelectedIndex() method Choose one item in the JComboBox to be the selected item getSelectedItem() or getSelectedIndex() method – Discover which item is currently selected Treat the list of items in a JComboBox object as an array – The first item is at position 0 – The second item is at position 1 – And so on 49Java Programming, Seventh Edition

You Do It Creating a JFrame Ending an Application When a JFrame Closes Adding Components to a JFrame Adding Functionality to a JButton and a JTextField Distinguishing Event Sources Including JCheckBox es in an Application 50Java Programming, Seventh Edition

Don’t Do It Don’t forget the x in javax when you import Swing components into an application Don’t forget to use a JFrame ’s setVisible() method if you want the JFrame to be visible Don’t forget to use setLayout() when you add multiple components to a JFrame 51Java Programming, Seventh Edition

Don’t Do It (cont’d.) Don’t forget to call validate() and repaint() after you add or remove a component from a container that has been made visible Don’t forget that the ButtonGroup class does not begin with a J 52Java Programming, Seventh Edition

Summary JFrame – A Swing container that resembles a window – Has a title bar and borders, and the ability to be resized, minimized, restored, and closed Many types of listeners exist in Java – Each can handle a specific event type – Register a listener with the event source – Handle an event in the event-handling method 53Java Programming, Seventh Edition

Summary (cont’d.) JCheckBox – Consists of a label positioned beside a square ButtonGroup – Groups several components so the user can select only one at a time JComboBox – Displays an area showing an option combined with a list box containing additional options 54Java Programming, Seventh Edition