1 Lecture 8: User Interface Components with Swing.

Slides:



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

Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
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.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Graphical User Interface Components: Part 1
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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 
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Welcome to CIS 083 ! Events CIS 068.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– 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 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
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.
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.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Graphical User Interface Components: Part 1 Chapter 11.
Creating User Interfaces Event-Driven Programming.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
 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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
Welcome To java
Aum Amriteshwaryai Namah
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
Ellen Walker Hiram College
IFS410: Advanced Analysis and Design
Presentation transcript:

1 Lecture 8: User Interface Components with Swing

2 Swing Overview (1) Classes from package javax.swing defines various GUI components — objects with which the user interacts via the mouse, the keyboard or another form of input. Some basic GUI components JLabel JTextField JCheckBox JComboBox JList JPanel Most of the swing components are written completely in java, so they provide a greater portability and flexibility than the original GUI components from package java.awt Awt components are platform dependent Some swing components are still platform dependent. E.g, JFrame

3 Swing Overview (2) Common superclasses of many of the Swing components Component class Operations common to most GUI components are found in Component class. Container class Two important methods originates in this class add — adds components to a container. setLayout — enables a program to specify the layout manager that helps a Container position and size its components. java.lang.Object java.awt.Container Javax.swing,JComponent java.awt.Component

4 JLabel A JLabel object provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and image Example code One thing to be emphasized: if you do not explicitly add a GUI component to a container, the GUI component will not be displayed when the container appears on the screen

5 To Make an Interactive GUI Program To make an interactive GUI program, you need: Components buttons, windows, menus, etc. Events mouse clicked, window closed, button clicked, etc. Event listeners (interfaces) and event handlers (methods) listen for events to be trigged, and then perform actions to handle them

6 Event-Handling Model (1) Some GUIs are event driven — they generate events when the user interacts with the GUI E.g, moving the mouse, clicking a button, typing in a text field, selecting an item from a menu, etc. When a user interaction occurs, an event is sent to the program. Many event types are defined in packages java.awt.event and javax.swing.event ActionEvent AdjustmentEvent TextEvent ComponentEvent ItemEvent ContainerEvent FocusEvent PaintEvent WindowEvent InputEvent KeyEvent MouseEvent CLASS key some event classes in package java.awt.event ABSTRACT CLASS

7 Event-Handling Model (2) Three parts of the event-handling mechanism event source: the GUI component with which the user interacts event object: encapsulated information about the occurred event event listener: an object which is notified by the event source when an event occurs, and provides responds to the event The programmer must perform two tasks to process a GUI event 1. register an event listener An object of a class that implements one or more of the event-listener interfaces from packages java.awt.event and javax.swing.event 2. implement an event handling method Event-listener interface of package java.awt.event

8 JTextField and JPasswordField They are single-line areas in which text can be entered by the user from the keyboard or text can simply be displayed When the user types data into them and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the text field at the time of the event in the program. Example code

9 Buttons A button is a component the user clicks to trigger a specific action There are several types of buttons in Java, all are subclasses of AbstractButton command buttons: is created with class JButton. It generates ActionEvent toggle buttons: have on/off or true/false values check boxes: a group of buttons. It generates ItemEvent radio buttons: a group of buttons in which only one can be selected. It generates ItemEvent javax.swing.JComponent javax.swing.AbstractButton javax.swing.JButton javax.swing.JCheckBox javax.swing.ToggleButton javax.swing.JRadioButton Example Code of JButton

10 More Examples … JComboBox : a drop-down list provides a list of items from which the user can make a selection. It generates ItemEvent JList : a list supports both single selection and multiple-selection. It generates ListSelectionEvent

11 Layout Management Let’s see an example first All components in a container are positioned by a layout manager. Buttons in the above example are managed by the flow layout manager, which is the default layout manager for a panel. The default manager lines the components horizontally until there is no more room and then start a new row of components After resizing the container, the layout manager reflows the components automatically The default is to center the components in each row. You can choose aligning them to the left or to the right of the container. panel.setLayout(new FlowLayout(FlowLayout.LEFT)); Other managers: see

12 Layout Management — Using Panel Potential problem with BorderLayout: The button is stretched to fill the entire southern region of the frame If you add another button to the southern region, it would just displace the first button Solution – use additional panels. It acts as containers for interface elements and can themselves be arranged inside a larger panel Use flow layout by default To fix the problem of BorderLayout 1. Create a panel 2. Add components to the panel 3. Add the panel to the larger container JPanel p = new JPanel(); p.add(button1); p.add(button2); P.add(button3); frame.add(panel, BorderLayout.SOUTH);

13 Supplemental Reading A visual Index to the Swing Components Creating a GUI with JFC/Swing Building a User Interface