Lecture 15 Basic GUI programming

Slides:



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

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
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.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
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.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
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:
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
1 Lecture 8: User Interface Components with Swing.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
GUI Programming using Java - Event Handling
Lecture 15.1 Event Delegation.
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
JButton Chapter 11 Lecture 3.
Lecture 16 More GUI programming
Web Design & Development Lecture 11
A First Look at GUI Applications
Processing Timer Events
Lecture 27 Creating Custom GUIs
A Quick Java Swing Tutorial
Ellen Walker Hiram College
GUI Programming III: Events
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Timer class and inner classes
Introduction to Event Handling
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Graphical User Interface
Presentation transcript:

Lecture 15 Basic GUI programming D&D 12 Date

Goals By the end of this lesson, you should: Be able to write Java applications using input and message dialogs Be able to write Java applications using JFrame windows, JLabels and JButtons Be able to handle button click events and understand the mechanism behind ActionEvent and ActionListeners

Input and message dialogs Java Swing components A simple GUI example Events Notes Summary import javax.swing.JOptionPane; public class PopUpDialogs { public static void main(String[] args) { String name = JOptionPane.showInputDialog("Please enter your name"); JOptionPane.showMessageDialog(null, "Hello " + name + "!\nNow I know who you are!", "Greeting", JOptionPane.INFORMATION_MESSAGE); } When it comes to GUI (graphical user interface) programming, Java’s standard library are the Swing components, with a bit of support from awt, which we’ve already seen. Pop-up dialogs like the input dialog and message dialog above are launched via a class method of the javax.swing.JOptionPane class. Note that the input dialog blocks execution of the method until the user has entered a name.

More complex GUIs Input and message dialogs Java Swing components A simple GUI example Events Notes Summary We’ve already dealt with a GUI – remember the stick figures from Lecture 10? Remember how this was based on a JFrame that contained a JPanel with the stick figures? Most Swing applications also use a JFrame to provide the window in which the application runs. Typically, we will extend the JFrame class for this purpose.

Swing Components Things such as buttons, checkboxes, text fields, lists, images, menus, etc. that we find in GUIs are known as components. The Java Swing classes know two types of components: Heavyweight components: Like AWT components, they use operating system specific code in the background and are only partially written in Java. Lightweight components: Written entirely in Java. Most Swing components are lightweight. The only heavyweight Swing component we have met so far has been the JFrame. Some basic Swing components have counterparts in AWT, e.g., a JButton is the Swing version of a Button. Swing has more components than AWT. Input and message dialogs Java Swing components A simple GUI example Events Notes Summary

Lightweight Swing Components and containers Input and message dialogs Java Swing components A simple GUI example Events Notes Summary In Swing, each lightweight component is an instance of a subclass of JComponent. This class adds tooltips, accessibility, keyboard shortcuts etc. Each JComponent is a Container, and each container is a Component, which in turn is a subclass of the Object class, the common ancestor of all Java objects. A Container is a Component that we can attach other Components to (e.g., attaching a JButton to a JFrame). We do so with its add()method. A Component in turn implements all the common features of awt and Swing components. Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.AbstractButton javax.swing.JButton

The HelloWorld application Input and message dialogs Java Swing components A simple GUI example Events Notes Summary This “HelloWorld” application is a Swing application that simply consists of a window with a label and a button. When you click on the button, the label changes.

Starting a Swing application import javax.swing.JFrame; import javax.swing.SwingUtilities; public class RunHelloWorld implements Runnable { public void run() { HelloWorld h = new HelloWorld(); h.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); h.setSize(300,150); h.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new RunHelloWorld()); Input and message dialogs Java Swing components A simple GUI example Events Notes Summary Need this import only because of the JFrame.EXIT_ON_CLOSE Runnable method, will start on the event-dispatching thread Swing is not thread-safe (we’ll find out later what that means exactly). As many components in Swing work with events, we should run them in the thread that dispatches the events. We’ll do this by getting main() to instantiate that class that contains it and schedule it to invoke the actual startup method, run(), in the event-dispatching thread. run() is part of the Runnable interface.

The HelloWorld class Input and message dialogs Java Swing components A simple GUI example Events Notes Summary import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; Before the actual class declaration, we import several component classes: From awt: FlowLayout (to enable the JFrame to arrange components automatically) The ActionListener interface and the ActionEvent class From Swing: The classes for a JFrame, JLabel, and JButton

The HelloWorld class Input and message dialogs Java Swing components A simple GUI example Events Notes Summary public class HelloWorld extends JFrame implements ActionListener { private final JLabel label; private final JButton button; public HelloWorld() { super("Hello World with Swing GUI components"); setLayout(new FlowLayout()); label = new JLabel("Hey you, push my button!"); add(label); button = new JButton("Push me!"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent event) { label.setText("Thanks!"); A call to super() sets the window title, and a call to setLayout() passes a new FlowLayout object, which manages the arrangement of components in the order in which we will add them.

The HelloWorld class Then: Create components (instantiate them) Input and message dialogs Java Swing components A simple GUI example Events Notes Summary public class HelloWorld extends JFrame implements ActionListener { private final JLabel label; private final JButton button; public HelloWorld() { super("Hello World with Swing GUI components"); setLayout(new FlowLayout()); label = new JLabel("Hey you, push my button!"); add(label); button = new JButton("Push me!"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent event) { label.setText("Thanks!"); Then: Create components (instantiate them) Configure the components (e.g., by adding an ActionListener) Add the components to the extended JFrame.

Remarks A JFrame isn’t actually a JComponent, but it is a subclass of Container, so we can add() other components to it. We have already met the ActionListener interface and the actionPerformed() method, as well as the addActionListener()method in Lecture 10 in our stick figure application. Back then, the component that triggered the event was our timer. Now, it is our button, operated by the user. Many components can trigger events. These include anything from a mouse hovering over a component to a key being pressed, a component getting focus and so on. Events are objects, and there are many event classes defined in java.awt.event. Their common ancestor is AWTEvent, which is a subclass of EventObject, whose superclass is Object. See Fig. 12.11 in D&D. ActionEvent is one of these classes. Input and message dialogs Java Swing components A simple GUI example Events Notes Summary

Events Input and message dialogs Java Swing components A simple GUI example Events Notes Summary A JComponent can in principle have several events associated with it. For example, a JButton can receive focus (user hitting the tab key until the button becomes active), or the user could click on it, etc. Events are instances of subclasses of AWTEvent. This includes the ActionEvent in the previous example. Not every event necessarily requires handling (e.g., we mightn’t want anything to happen just because a button gets focus). Every event class is associated with a particular type of event listener, e.g., ActionEvent is associated with an ActionListener.

Event listeners Input and message dialogs Java Swing components A simple GUI example Events Notes Summary Event listeners are interfaces. A class that is meant to handle events of the respective type must implement that interface, i.e., the methods that the interface declares. All event listener interfaces inherit from a common ancestor interface named EventListener. For example, the class that handled our ActionEvent had to implement the ActionListener interface, which involved implementing the actionPerformed() method. Each JComponent maintains a list (listenerList) of references to objects that implement the respective event listener. We can register event handling objects in that list via addActionListener() and similar methods that the subclasses of JComponent provide. Some of these add…Listener() methods cannot be implemented in JComponent itself. Can you figure out why?

Deitel & Deitel … do a couple of things differently in Chapter 12: They start Swing applications directly from main() because the book hasn’t touched on concurrency at this point. That’s generally fine for simple stuff but isn’t the “proper” way of going about. See: https://docs.oracle.com/javase/8/docs/api/javax/swing/package-summary.html#threading We have implemented the ActionListener interface right on our extended JFrame class: public class HelloWorld extends JFrame implements ActionListener { So when we add the ActionListener for the button, we pass this as the reference of the object that implements the actionPerformed() method. Deitel & Deitel also use a variety of other techniques, some of which we will still meet. They use our technique in Figs. 12.42 and 12.43. Input and message dialogs Java Swing components A simple GUI example Events Notes Summary

What do we know Input and message dialogs Java Swing components A simple GUI example Events Notes Summary The java.awt package lets us build simple graphical applications and provides a set of basic window controls. The javax.swing package partially replaces and extends this set of controls by mostly lightweight controls written entirely in Java. Lightweight Swing components are subclasses of JComponent JComponents can dispatch events to event listeners. Events are objects, event listeners are instances of classes that implement the appropriate listener interface. Pop-up dialogs based on JOptionPane offer a very simple GUI, more complex GUIs can be built on top of a JFrame. We also know how to use a JLabel and a JButton.

Resources Input and message dialogs Java Swing components A simple GUI example Events Notes Summary D&D Chapter 12 http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html https://docs.oracle.com/javase/tutorial/uiswing/components/index.html http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Next Lecture More GUI programming (Chapter 12)