Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Graphic User Interfaces Layout Managers Event Handling.
Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
© 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;
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Unit 101 Java GUI Components and Events  GUI Components and Containers  Adding Components to Containers  GUI Events  GUI Events Classes  Learning.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
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.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
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
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.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Java Swing Reference: 1.Swing: A Beginner’s Guide by Herbt Schildt, TMH Edition.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
UID – Event Handling and Listeners Boriana Koleva
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Java Swing One of the most important features of Java is its ability to draw graphics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
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:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
GUI 1: JFC and Swing Basics OOP tirgul No
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Advanced User Interfaces
A Quick Java Swing Tutorial
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004

Serial and Parallel Port Access Java Communications API –javax.comm Extension Package –

Graphical User Interfaces (GUI) in Java Two sets of classes –AWT (Abstract Windowing Toolkit) –Swing

AWT vs Swing Mixing AWT & Swing is bad! Advantages of Swing –Faster –More complete –Being improved Advantages of AWT –Supported on older browsers –Java Micro-Edition (phones, PDAs, etc.) uses AWT, not Swing

The Good News AWT and Swing are very similar So changing code from one to the other is not too difficult.

A Note on Inheritance A Java class can be derived from another class. Suppose class B is derived from class A –We say B extends A –B is a subclass of A –B inherits features from A –An object of type B can be treated as if it is an object of type A

Swing Each GUI feature in Swing (windows, buttons, dialog boxes, menus, scroll bars, etc.) is a component. It is helpful to note that components in Swing are derived from components in AWT.

Component Components that can contain other components are Containers. JFrame, JPanel and JWindow are examples of Containers JButton, JMenu are not containers

Top-Level Containers A Java GUI has at least one top-level container. The most common are: –JFrame –JDialog –JApplet

Our first Swing GUI (JFrame) // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);

setSize // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 100); frame.setVisible(true);

Labels // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 100); // create a text label JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setVisible(true);

Buttons // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 100); // create a text label JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); JButton button = new JButton("I'm a Swing button!"); frame.getContentPane().add(button); frame.setVisible(true);

Panels and Layouts JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello World"); JButton button = new JButton("I'm a Swing button!"); // create a panel to hold the button and the label JPanel panel = new JPanel(new GridLayout(0,1)); // add the button and label to the panel panel.add(button); panel.add(label); // then put the panel in the frame frame.getContentPane().add(panel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true);

Borders // create a panel to hold the button and the label JPanel panel = new JPanel(new GridLayout(0,1)); panel.setBorder(BorderFactory.createEmptyBorder( 30, //top 30, //left 10, //bottom 30) //right ); // add the button and label to the panel panel.add(button); panel.add(label); // then put the panel in the frame frame.getContentPane().add(panel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true);

Events public class swingExample7 implements ActionListener { … public static void main(String[] args) { swingExample7 mainObj = new swingExample7(); … // create a button JButton button = new JButton("I'm a Swing button!"); button.addActionListener(mainObj); …

Events public class swingExample7 implements ActionListener { static JLabel label; static String labelPrefix = "Number of clicks: "; static int numClicks = 0; public static void main(String[] args) { … } public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); }

Event Handlers in General Require 3 pieces of code: 1.public class MyClass implements ActionListener { 2.Registering the event handler someComponent.addActionListener(instanceOfMyClass); 3.An Event handler method public void actionPerformed(ActionEvent e) {...//code that reacts to the action... }

Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener