Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.

Slides:



Advertisements
Similar presentations
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Advertisements

Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Graphical User Interfaces
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.
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 
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.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
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,
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
For (int i = 1; i
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
A simple swing example GETTING STARTED WITH WIND CHILL.
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Event Handling and GUI Components.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
1 Lecture 8: User Interface Components with Swing.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
CSC 222: Computer Programming II
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
Processing Timer Events
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Chapter 12 Event Handling
A Quick Java Swing Tutorial
Advanced Programming in Java
Presentation transcript:

Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications in that they have a component (the JComponent object) which is being placed in a container (the JFrame) for display. A GUI component object is a screen element which is used to display information, or allow the user to interact with the program in a special way (Examples of components are JComponent objects, buttons, scroll bars, text fields and radio buttons.) A container is a special type of component that is used to hold and organize other components. Frames and panels are two examples of Java containers.

Swing GUI Components You can create GUI components to place in a container using classes available in the Swing package ( javax.swing ) Class names start with J : JTextField, JLabel, JButton Objects are created by calling a class constructor: JLabel xLabel = new JLabel("x = "); JTextField xField = new JTextField(width); JButton moveButton = new JButton("Move",buttonIcon); JButton anotherButton = new JButton( “ go ” );

import javax.swing.JButton; import javax.swing.JFrame; // Demonstrates how to create and display // a button public class ButtonTester{ private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Click me!"); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } Creates a frame with a button on it. BUT, nothing happens when this button is pressed ………. We need to provide code which can handle this event!!

Java GUI applications are event driven. They: *generate event objects when an ‘event’ happens *execute code is response to the event Typical events are moving the mouse, ‘pressing’ a button, typing in a text field, a mouse click …. you get the idea When an event occurs, an event object is created which stores information about that event. The program may then respond to the event by executing a predetermined set of statements.

The Event Class The class from which the event object is created depends upon the type of event which has occurred. An ActionEvent object is created if a button is pressed, or textfield data has been entered. A MouseEvent object is created if the event involved the mouse. *these event classes are provided by java.awt.event package.

The Event Source The event source is the visual component with which the user has interacted. For example: a button object is the event source of a button press a JComponent object is the event source of a mouse click The event source generates the event.

The Event Listener An event listener is an object that contains code which is to be executed when a particular event occurs. When an event occurs, if there is a event listener object waiting for that event, it will be used to react to that event. For example, if a button is pressed, an event is created, and if there is an event listener object waiting for that button to be pressed, it will provide the code that is to be executed.

How does it work? An event source (eg. a particular button) keeps an internal list of listener objects which are available for responding to any events that it may generate. If an event occurs (eg. the button is pressed) then listener objects on that list are ‘notified’. Any listener that is ‘waiting’ for this particular event may then respond. What does the programmer do? The programmer must: * Create the event source * provide a class from which a listener object can be created (a listener class) * create and ‘install’ the listener object with the event source

Back to the issue … we need to create a class, from which an event listening object can be created. Then we need to install (add) an object of this type to our button’s list of listeners. public class ButtonTester{ private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Click me!"); // button needs listener!! frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

A JButton object will generate an ActionEvent object if an event occurs. So the event listening object must be of type ActionListener. // ActionListener is an interface. public interface ActionListener { void actionPerformed(ActionEvent event); } Need to supply a class which implements ActionListener. So it must contain the method actionPerformed. This method will contain the instructions to be executed when the button is clicked.

* we need to create a class, from which an ActionListener object can be created. public class ButtonListener implements ActionListener { void actionPerformed(ActionEvent event){ System.out.println(“I was clicked”); } import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

* we need to install (add) this object to our button’s list of listeners. public class ButtonTester{ private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Click me!"); ButtonListener blisten = new ButtonListener(); button.addActionListener(blisten); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Figure 1: Implementing an Action Listener Output:

* Which objects are the event source and the event listener in the ButtonTester program? * Why is it legal to assign a ButtonListener object to a variable of type ActionListener ? button is the event source. bListen is the event listener object. Because ButtonListener implements the ActionListener interface, so a ButtonListener object is also an object of type ActionListener.

Suppose we were creating an application with 15 buttons, and a number of other components. We do not want to put all of our components directly on the frame, as they may overwrite each other, and we may like to organize these components in some way. We will use another container, a JPanel to hold our components, and then put the JPanel object on our frame. JPanel panel = new JPanel(); panel.add(button1); panel.add(button2); frame.add(panel);

Let’s code an application with 2 buttons, titled “RED” and “BLUE” -- and one label. When a button is pushed, the text on the label will be updated to the color indicated on the pushed button.

import javax.swing.*; // First create the components, put them on a panel, and // put the panel on the frame public class Button4{ public static void main(String[] args) { JFrame frame = new JFrame("Four buttons"); frame.setSize(200,150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel outlbl = new JLabel("Look at me!"); // create JButton redbtn = new JButton("RED"); JButton bluebtn = new JButton("BLUE"); JPanel panel = new JPanel(); //for organization panel.add(redbtn); panel.add(bluebtn); panel.add(outlbl); frame.add(panel); //frame will display panel frame.setVisible(true); }

so far we have created a frame which displays 2 buttons and a label -- but nothing happens when the buttons are pushed!! we need to create ActionListener objects, and install one on each button. This is a bit different than the last button program… because these listener objects will need access to the LABEL!! SO… they cannot be in separate files.. They must be inner classes…..

public class ButtonFour{ public static void main(String[] args) { // statements which create and set up frame final JLabel outlbl = new JLabel("Look at me!"); // this variable will be used inside the inner listener classes, // so it MUST be declared as Final JButton redbtn = new JButton("RED"); // create listener class for red button class RedListener implements ActionListener { public void actionPerformed (ActionEvent e) { outlbl.setForeground(Color.red); outlbl.setText("RED"); } // create listener object and install RedListener rlisten = new RedListener(); redbtn.addActionListener(rlisten);

JButton bluebtn = new JButton("BLUE"); // create listener class for blue button class BlueListener implements ActionListener { public void actionPerformed (ActionEvent e) { outlbl.setForeground(Color.blue); outlbl.setText("Blue"); } // create listener object and install BlueListener blisten = new BlueListener(); bluebtn.addActionListener(blisten); JPanel panel = new JPanel(); // all components go on panel panel.add(redbtn); panel.add(bluebtn); panel.add(greenbtn); frame.add(panel); frame.setVisible(true); } ys so far we have created a frame which displays