Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.

Slides:



Advertisements
Similar presentations
F27SB2 Programming Languages
Advertisements

TCU CoSc Programming with Java Handling Events.
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.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
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.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
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.
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.
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.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
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.
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.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
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 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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Creating User Interfaces Event-Driven Programming.
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.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
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.
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.
© 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
GUIs and Events Rick Mercer.
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")
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java Programming Fifth Edition Chapter 13 Introduction to Swing Components

Understanding Swing Components UI components –Buttons, text fields, and other components with which the user can interact Swing component –Descendant of JComponent –Inherits from java.awt.Container class Insert import statement: import javax.swing.*; Java Programming, Fifth Edition2

Understanding Swing Components Container –Type of component that holds other components –Can treat group as single entity –Defined in Container class –Often takes form of window Drag Resize Minimize Restore Close Java Programming, Fifth Edition3

4 Understanding Swing Components (continued) Window class –Child of Container –Does not have title bars or borders –Rarely used –Instead use subclasses Frame JFrame

Using the JFrame Class Java Programming, Fifth Edition5

6

7 Using the JFrame Class (continued) Create JFrame JFrame firstFrame = new JFrame("Hello"); Set size and title firstFrame.setSize(200, 100); firstFrame.setTitle("My frame");

Using the JFrame Class (continued) Java Programming, Fifth Edition8

9 Using the JFrame Class (continued) Close JFrame –Click Close button –JFrame becomes hidden and application keeps running Default behavior –To change this behavior Use setDefaultCloseOperation() method

Java Programming, Fifth Edition10 Customizing a JFrame 's Appearance Window decorations –Icon and buttons Look and feel –Default appearance and behavior of user interface –setDefaultLookAndFeelDecorated() method Set JFrame ’s look and feel

Using a JLabel JLabel –Holds text you can display –Available constructors Methods –add() –remove() –setText() –getText() Java Programming, Fifth Edition11

Changing a JLabel ’s Font Font class –Creates an object that holds typeface and size information –To construct a Font object Arguments: typeface, style, and point size setFont() method –Requires a Font object argument Java Programming, Fifth Edition12

Changing a JLabel ’s Font (continued) Java Programming, Fifth Edition13

Java Programming, Fifth Edition14 Using the JCheckBox Class JCheckBox –Consists of a label positioned beside a square –Click square to display or remove check mark –Use to allow user to turn option on or off Constructors JCheckBox() JCheckBox("Check here") JCheckBox("Check here", false)

Using the JCheckBox Class (continued) Java Programming, Fifth Edition15

Using the JCheckBox Class (continued) Java Programming, Fifth Edition16

Java Programming, Fifth Edition17 Using the JCheckBox Class (continued) Methods –setSelected –isSelected Status of JCheckBox changes from unchecked to checked –ItemEvent generated –itemStateChanged() method executes

Adding JTextField s, JButton s, and Tool Tips to a JFrame JTextField –Component into which a user can type a single line of text data –Several constructors –Methods setText() getText() setEditable() Java Programming, Fifth Edition18

Adding JButton s JButton –Click with a mouse to make a selection –Five constructors –Methods setText() and getText() add() method –Adds a JButton to a Jframe When clicked –No resulting actions occur –Code has not yet been written to handle user- initiated events Java Programming, Fifth Edition19

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 tool tip for a Component Java Programming, Fifth Edition20

Using a Layout Manager Layout manager –Class that controls component positioning BorderLayout –Normal (default) behavior of a JFrame –Divides a container into regions FlowLayout –Places components in a row Java Programming, Fifth Edition21

Java Programming, Fifth Edition22 Extending the JFrame Class Create class that descends from JFrame class Advantage –Set JFrame ’s properties within object’s constructor –When JFrame child object created Automatically endowed with specified features Create child class using keyword extends Call parent class’s constructor method –Using keyword super

Extending the JFrame Class (continued) Java Programming, Fifth Edition23

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 –User might initiate any number of events in any order Source - Component on which an event occurs Listener -Object that is interested in an event Respond to user events within any class you create –Tell your class to expect events to happen –Tell your class how to respond to events Java Programming, Fifth Edition24

Preparing Your Class to Accept Event Messages Import the java.awt.event package Add the phrase implements ActionListener to the class header ActionListener –Standard event method specifications that allow your listener to work with ActionEvents Java Programming, Fifth Edition25

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” Java Programming, Fifth Edition26

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

Using the setEnabled() Method setEnabled() method –Make a component unavailable –Then make it available again in turn Use after a specific series of actions has taken place Java Programming, Fifth Edition28

Java Programming, Fifth Edition29 Understanding Swing Event Listeners Classes that respond to user-initiated events –Must implement interface that deals with events –Called event listeners Many types of listeners exist in Java –Each can handle specific event type Class can implement as many event listeners as it needs Event occurs every time user types character or clicks mouse button

Understanding Swing Event Listeners (continued) Java Programming, Fifth Edition30

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

Understanding Swing Event Listeners (continued) Java Programming, Fifth Edition32

Java Programming, Fifth Edition33 Using the ButtonGroup Class ButtonGroup –Group several components so that user can select only one at a time Group JCheckBox objects –All of other JCheckBox es automatically turned off when user selects any one check box

Java Programming, Fifth Edition34 Using the ButtonGroup Class (continued) Create ButtonGroup and then add JCheckBox –Create ButtonGroup ButtonGroup aGroup = new ButtonGroup(); –Create JCheckBox JCheckBox aBox = new JCheckBox(); –Add aBox to aGroup aGroup.add(aBox);

Java Programming, Fifth Edition35 Using the JComboBox Class JComboBox –Component that combines two features Display area showing one option List box containing additional options –When user clicks JComboBox, list of alternative items drops down User selects one to replace box’s displayed item

Using the JComboBox Class (continued) Java Programming, Fifth Edition36

Java Programming, Fifth Edition37 Using the JComboBox Class (continued) Build JComboBox –Use constructor with no arguments –Add items with addItem() method –Alternatively, construct by using array of Objects as constructor argument String[] majorArray = {"English", "Math", "Sociology"}; JComboBox majorChoice = new JComboBox(majorArray);

Java Programming, Fifth Edition38

Java Programming, Fifth Edition39 Using the JComboBox Class (continued) setSelectedItem() or setSelectedIndex() method Choose one item in JComboBox to be selected item getSelectedItem() or getSelectedIndex() method –Discover which item currently selected Treat list of items in JComboBox object as array –First item at position 0 –Second at position 1

Java Programming, Fifth Edition40 Summary (continued) JCheckBox –Consists of a label positioned beside a square ButtonGroup –Group several components so user can select only one at a time JComboBox –Display area showing an option combined with list box containing additional options