Chapter 121 Window Interfaces Using Swing Chapter 12.

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo Program l Layout Managers l Buttons and Action Listeners l Container.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 51 More About Objects and Methods Chapter 5.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
CS102--Object Oriented Programming Lecture 18: – The Swing Package Copyright © 2008 Xiaoyan Li.
Java Swing 4 th April 2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
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.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
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.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter 12- GUI’s, Java, and Swing.. Overview n What are GUI’s n How Java does GUI’s- Swing n Buttons n Containers n Text I/O and Swing n Review.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
PANELS. Color Class Colour constants are as follows: –Color.black –Color.blue import java.awt.* to access Color Class.
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.
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,
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 230 S Programming Techniques
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Programming III
Java Programming: From Problem Analysis to Program Design,
Chapter 17 Swing I Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc. All.
Ellen Walker Hiram College
Comp 249 Programming Methodology
Window Interfaces Using Swing Objects Chapter 12
Window Interfaces Using Swing Objects Chapter 12
Graphical User Interface
Presentation transcript:

Chapter 121 Window Interfaces Using Swing Chapter 12

2 Reminders Project 7 due Nov 10:30 pm Project 6 grades released: regrades due by next Friday ( ) at midnight

Chapter 123 GUIs - Graphical User Interfaces Windowing systems that interact with users often are called GUIs. A GUI accepts information from a user and makes it available to the program for processing. Most of the interaction is graphical in nature.

Chapter 124 Event-Driven Programming Most GUI programs involve events and event handlers. A GUI event is an object that represents some action such as clicking the mouse, dragging the mouse, pressing a keyboard key, clicking the close-window button on a window, etc. When an object generates an event, it is said to fire the event.

Chapter 125 Event-Driven Programming, cont. Objects that can fire events have one or more listener objects. The programmer chooses which event-firing objects have listeners. Event handlers are programmer-defined methods associated with the listener objects that determine what happens when events are detected by their associated listener(s).

Chapter 126 Layout Managers The objects that you add to a container class are arranged by an object known as a layout manager. A layout manager is added using method setLayout, which is a method of every container class. syntax Container_Object.setLayout(new Layout_Manager_Class(Any_Parameters));

Chapter 127 Border Layout, cont. A BorderLayout manager can place a component into any of five regions. Regions which are unused give up their space to BorderLayout.CENTER.

Chapter 128 Flow Layout The simplest layout manager is the FlowLayout manager. Components are added and arranged one after another, left to right, until a row is filled. Then components are added to the next row in the same manner. Each row is centered in its container.

Chapter 129 Grid Layout A GridLayout manager arranges components in a grid of rows and columns. example aContainer.setLayout(new GridLayout(2,3));

Chapter 1210 Grid Layout, cont. Each entry has the same size. Rows are filled one at a time, top to bottom, and from left to right within each row.

Chapter 1211 Default Layout Managers When a layout manager is not added explicitly, a default layout manager is provided. The default manager for the content pane of a JFrame is BorderLayout. The default manager for a JPanel is FlowLayout.

Chapter 1212 Buttons A button is a GUI component that looks like a button and does something when it is clicked using a mouse. Like a label, a button is created and added to a container. Unlike a label, a button can fire an event and the event can cause a GUI to perform some action.

Chapter 1213 Adding Buttons A button is created using JButton Button_Name = new JButton(“Button_Label”); A button is added to a container using Container_Name.add(Button_Name);

Chapter 1214

Chapter 1215 Adding Buttons, cont.

Chapter 1216 Action Listeners and Action Events For each button, the GUI needs to –register (specify) the listener object(s). –define the methods to be invoked when an event is fired. For a statement such as stopButton.addActionListener(this); the class ButtonDemo is itself the listener class.

Chapter 1217 Action Listeners and Action Events, cont. Different kinds of components requite different kinds of listener classes. Buttons fire action events which are handled by action listeners. An action listener is an object of type ActionListener, and ActionListener is an interface (discussed later).

Chapter 1218 Action Listeners and Action Events, cont. To make a class into an ActionListener –add implements ActionListener to the heading of the class definition –define a method named ActionPerformed. –register the ActionListener object with the component that will fire the event using the method addActionListener –(A component may register with more than one listener.)

Chapter 1219 Action Listeners and Action Events, cont. Any class can be an action listener class.

Chapter 1220 Buttons and an Action Listener

Chapter 1221 The actionPerformed Method An actionListener class must have a method named actionPerformed that has one parameter of type ActionEvent. syntax public void actionPerformed(ActionEvent e) { Code_for_Actions_Performed }

Chapter 1222 Interfaces An interface is a property of a class that states what methods the class must define. ActionListener is an interface. A class which satisfies the requirements of an interface implements the interface. A class can define methods in addition to the methods required by the interface.

Chapter 1223 Interfaces, cont. To implement an interface, a class must –include the phrase implements Interface_Name at the start of the class definition –implement all the method headings listed in the definition of the interface.

Chapter 1224 Multiple Interfaces A class which implements multiple interfaces lists the names of all the interfaces, separated by commas. implements First_Interface_Name, Second_Interface_Name, …, Last_Interface_Name The class must implement all the methods of all the listed interfaces.

Chapter 1225 Method setActionCommand We can think of the method invocation e.getActionCommand() as returning the string written on the button. In fact, this method invocation returns a string known as the action command for the button. A different action command can be specified for the button.

Chapter 1226 Method setActionCommand, cont. example JButton stopButton = new JButton(“Red”); stopButton.setActionCommand(“Stop”); This permits the same string to be written on two different buttons, but with the two buttons distinguished from one another by the program.

Chapter 1227 Method setActionCommand, cont. Every object that fires an action event has an associated string known as the action command for that component. e.getActionCommand() returns the action command for the component that fired e. The default action command for a button is the string written on it. Method setActionCommand can be used to change the action command for the object.

Chapter 1228 The JPanel Class A GUI can be organized hierarchically, with window-like containers inside other window- like containers. Class JPanel is a simple container that does little more than hold components. Components can be placed in a JPanel which can be placed in another JPanel, … which can be placed in a JFrame.

Chapter 1229 The JPanel Class, cont. To place two components in BorderLayout.SOUTH for example, simply place the two components in a panel and place the panel in the BorderLayout.SOUTH position. The panel has its own layout manager.

Chapter 1230

Chapter 1231 The JPanel Class, cont.

Chapter 1232 The Container Class Class Container is a predefined class. An object of a class which descends from class Container is called a container class and can have components added to it. Class JFrame is a descendent of class Container, permitting any JFrame object to hold labels, buttons, panels, and other components.

Chapter 1233 Adding Components To add a component to a JFrame, use method getContentPane to obtain the content pane, and the use method add with the content pane as the calling object. example Container contentPane = getContentPane(); Jlabel label = new Jlabel(“Click Here”); contentPane.add(label);

Chapter 1234 Adding Components, cont. For other container classes, add components by using method add directly with an object of the container class. example JPanel buttonPanel = new JPanel(); JButton stopButton = new JButton(“Stop”); buttonPanel.add(stopButton);

Chapter 1235 Text Areas and Text Fields class MemoSaver

Chapter 1236 Text Areas and Text Fields, class MemoSaver, cont.

Chapter 1237 Text Areas and Text Fields, cont.

Chapter 1238 Text Areas and Text Fields, cont. An object of class JTextArea can have a size consisting of a specified number of lines and a specified number of characters per line. example JTextArea someText = new JTextArea(10,30);

Chapter 1239 Text Areas and Text Fields, cont. An object of class JTextField can have a size consisting of a specified number of characters. example JTextField name = new JTextField(10);

Chapter 1240 Text Areas and Text Fields, cont. The number of characters (per line) is not absolute, but represents the space needed for one ‘m’ character.

Chapter 1241 Line Wrapping in Text Areas Method setLineWrap sets the line wrapping policy for a JTextArea object. example theText.setLineWrap(true); If the argument is set to false, extra characters will be on the same line, but will not be visible.

Chapter 1242 Read-Only Text Components To specify that a user cannot write in a JTextArea or a JTextField, use method setEditable. example theText.setEditable(false); A JTextArea or a JTextField can be made editable subsequently using, for example theText.setEditable(true);

Chapter 1243 Inputting and Outputting Numbers Input provided using a JTextArea object or JTextField object is received as a string. When numeric input is needed, the string must be converted to a number. To output a number using a GUI constructed with Swing, the number must be converted to a string. All input typed by the user is string output, and all displayed output is string output.

Chapter 1244 Inputting and Outputting Numbers, cont. To convert a string to an integer, use, for example Integer.parseInt(“42”); or Integer.parseInt(ioField.getText()); or, to eliminate whitespace before or after the input, use Integer.parseInt (ioField.getText().trim());

Chapter 1245 Inputting and Outputting Numbers, cont. To input numbers of type double, use Double.parseDouble (ioField.getText().trim()); Analogous conversions can be done with classes Long and Float.

Chapter 1246 Inputting and Outputting Numbers, cont. To write numeric output to a JTextArea or a JTextField, use method toString. examples Integer.toString(sum); Double.toString(average); ioField.setText(Integer.toString(sum));

Chapter 1247 Catching a NumberFormatException A GUI, such as class Adder, has no control over what the user enters in the text field. The user might enter commas, or even alphabetic characters, resulting an a NumberFormatException, which leaves the GUI in an unpredictable state.

Chapter 1248 Catching a NumberFormatException, A NumberFormatException can be caught, and the user can be asked to reenter the number.

Chapter 1249 Summary You have learned the basics of event-driven programming. You have designed and coded a simple GUI with buttons and text. You have learned about several Swing- related classes.