Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.

Slides:



Advertisements
Similar presentations
F27SB2 Software Development 2 Lecture 2: Java GUIs 1.
Advertisements

Event Handling Feb 14, Event Model Revisited zRecall that a component can fire an event. zEach type of event is defined as a distinct class. zAn.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
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.
For IST410 Students only Events-1 Event Handling.
Unit 101 Java GUI Components and Events  GUI Components and Containers  Adding Components to Containers  GUI Events  GUI Events Classes  Learning.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
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.
Lecture 19 Graphics User Interfaces (GUIs)
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt * Object-Oriented Software Development Part 18 Building.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
1 1. About Swing 2. The Applet Framework 3. A first Applet 4. Swing Applications 5. The Swing Event model 6. Containment hierarchies 7. Swing Taxonomy.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
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.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java Swing One of the most important features of Java is its ability to draw graphics.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event Handling and Listeners in SWING The practice of event handling.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
IB103 Week 9 Free Standing Programs Chapter 18. Applets vs. Applications Applications are free standing See Program “Greeting” which prints out Hello.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 6: Event-Driven Programming.
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 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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:
GUI Programming in Java Hao Jiang Boston College April, 2009.
Component-Based Software Engineering GUI Basics Paul J Krause.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
Object-Orientated Analysis, Design and Programming
CHAPTER Reacting to the user.
Event Handling Chapter 2 Objectives
A Quick Java Swing Tutorial
Event-driven programming for GUI
Graphics Programming - Frames
Chapter 10 Getting Started with Graphics Programming
A Quick Java Swing Tutorial
Programming Graphical User Interface (GUI)
Presentation transcript:

Ch13 Creating windows and applets

Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used graphic classes SWING: Is a part of The Java Foundation Classes (JFC) Extensive package for the creation of GUI’s AWT vs. Swing Many AWT components have improved Swing counterpartsMany AWT components have improved Swing counterparts For example, the AWT Button class corresponds to a more versatile Swing class called JButtonFor example, the AWT Button class corresponds to a more versatile Swing class called JButton

Swing Hierarchy

Swing Components Frame Combo box Button List Menu Dialog Text Fields

The Swing’s Event model In the new event model a component can initiate an event. Each type of event is represented by a distinct class. When an event is fired, it is received by one or more “listeners,” which act on that event.

Selected Event Handlers Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) ItemEvent ItemListener itemStateChanged(ItemEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent)

JFrame 1. import javax.swing.*; public class MyFrame 4. { 5. public static void main(String[ ] args) 6. { 7. JFrame f = new JFrame(“My First Frame"); 8. f.setSize(400,300); 9. f.setVisible(true); 10. } 11. }

JFrame 1. import javax.swing.*; 2. import javax.awt.*; 3. import javax.awt.event.*; 4. public class FrameDemo 5. { 6. public static void main(String s[ ]) { 7. JFrame frame = new JFrame("FrameDemo"); 8. Container content = frame.getContentPane( ); 9. frame.addWindowListener(new WindowAdapter() { 10. public void windowClosing(WindowEvent e) { 11. System.exit(0);} 12. }); 13. frame.pack(); 14. frame.setVisible(true); 15. }

Closing Windows Steps to complete before a window will be able to close itself: Import the package java.awt.event.* so we get access to a windowlistener Add a windowlistener to the frame-object Implement the correct event for the listener, in this case the windowClosing event

Example: Closing-capability 1. import javax.swing.*; 2. import javax.awt.*; 3. import javax.awt.event.*; 4.public static void main(String[] args) { 5. MyFrame myWindow = new MyFrame(); 6. myWindow.addWindowListener(new WindowAdapter(){ 7. public void windowClosing(WindowEvent e) 8. { 9. System.exit(0); 10. }//windowClosing 11. }); 12. }

Example: Buttons //: Buttons.java // Various Swing buttons import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.*; import javax.swing.border.*; Import com.bruceeckel.swing.*; public class Buttons extends JApplet { JButton jb = new JButton("JButton"); BasicArrowButton up = new BasicArrowButton( BasicArrowButton.NORTH), down = new BasicArrowButton( BasicArrowButton.SOUTH), right = new BasicArrowButton( BasicArrowButton.EAST), left = new BasicArrowButton( BasicArrowButton.WEST);

public void init( ) { Container cp = get ContentPane( ); Cp.setLayout(new FlowLayout( ) ); add(jb); add(new JToggleButton("JToggleButton")); add(new JCheckBox("JCheckBox")); add(new JRadioButton("JRadioButton")); JPanel jp = new JPanel(); jp.setBorder(new TitledBorder("Directions")); jp.add(up); jp.add(down); jp.add(left); jp.add(right); add(jp); } public static void main(String args[]) { Console.run(new Buttons(), 300, 200); } } ///:~