Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Slides:



Advertisements
Similar presentations
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Advertisements

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.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Event-Driven Programming
Lecture 19 Graphics User Interfaces (GUIs)
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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
GUI programming Graphical user interface-based programming.
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
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 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.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Class Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.
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.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Event Handling and Listeners in SWING The practice of event handling.
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.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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 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:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
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.
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Java Applet What is a Java Applet? How is applet compiled?
Object-Orientated Analysis, Design and Programming
A First Look at GUI Applications
A Quick Java Swing Tutorial
Event-driven programming for GUI
Graphical user interface-based programming
Introduction to Event Handling
A Quick Java Swing Tutorial
Presentation transcript:

Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14 Texts, Choices and Windows Focus listenerspage 2 Text componentspage 3-6 Giving the user the coice between alternativespage 7-9 Choices using check boxespage 10 Choices using radio buttons page 11 Choices using listspage Java Windowspage 14 The class tree with the windows classespage 15 Creating a windowpage The differences between applets and applicationspage Other ways programming listenerspage 21-22

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 2 Focus Listeners A focus event occurs when a component gains or loses focus. Useful if, for example, we want to control input immediately after the user leaves an input field. If input is not ok, the focus may be set back to the field again (requestFocus()). A class describing focus listeners must implement the java.event.FocusListener interface: –public void focusGained() –public void focusLost() Often one of the methods is empty.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 3 Text Components The javax.swing.text.JTextComponent class –public String getText() –public void setText(String text) –public void setEditable(boolean open) The javax.swing.JTextField class –public JTextField() –public JTextField(String text) –public JTextField(int noOfColumns) –public JTextField(String text, int noOfColumns) –public void setHorizontalAlignment( int horizontalAdjusting) –public int getHorizontalAlignment() –public void setFont(Font newFont) –public void addActionListener( ActionListener aListener) The javax.swing.JPasswordField class –public JPasswordField() –public JPasswordField(int noOfColumns) –public char[] getText() –public String getText() (deprecated! ) Object Component JTextComponent JTextAreaJTextField Container JComponent JPasswordField

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 4 An Example

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 5 Message Exchange

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 6 Show program listing 14.1, pp Solve the problem, page 423.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 7 Giving the User a Choice Between Alternatives check boxes radio buttons list

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 8 GUI Componenter for Choices GUI componentClassListener will implement the interface Check box javax.swing.JCheckBoxjava.awt.ActionListener Radio button javax.swing.JRadioButton, usually placed in a javax.swing.ButtonGroup java.awt.ActionListener List javax.swing.JList, usually placed in a javax.swing.JScrollPane javax.swing.event. ListSelectionListener

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 9 GUI Components for Choices Object Component Container JComponent JToggleButton AbstractButton JRadioButton JList ButtonGroup JCheckBox

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 10 Choices Using Check Boxes Show program listing 14.2 pp Solve the problems, page 428. Will have dinner. Will have lunch. Output after click on the dinner box Output after click on the lunch box The user first clicks on the dinner check box, then on the lunch check box. An ActionEvent is generated for every click. The console:

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 11 Choices Using Radio Buttons Solve all problems, page 431. Show program listing 14.3 page

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 12 Choices Using Lists The list can be set up such that the user can select only one item, an interval of items, or several intervals of items. List with fixed contents. Show program listing 14.4 page Solve problem 1, page 439.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 13 Choices Using Lists The changes have to be done in the list’s ”model”. ”The model” represents the data that will be displayed. ”The model” is described in the DefaultListModel class. The contents of the list displayed are described in the JList class. List with dynamic contents. Show program listing 14.5, p Solve problem 2, page 439. Clicking on the line with “Evy Wilde” gives:

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 14 Java Windows

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 15 The Class Tree for the Windows Classes Object Component JFrameJApplet JInternalFrameDialog JDialog Container WindowJComponentPanel FrameJWindow Applet

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 16 Creating a Windows import javax.swing.JFrame; class TestJFrame { public static void main(String[] args) { JFrame myFirstWindow = new JFrame("My First Window"); myFirstWindow.setVisible(true); myFirstWindow.setSize(300, 200); // the size in pixels } } // this program does not work correctly The window is closed, but the Java interpreter keeps running… The Java interpreter may be stopped by using System.exit(0);, but where should this statement be inserted? We have to create an object, listening to the window closing event.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 17 The WindowListener Interface and the WindowAdapter Class The java.awt.event.WindowListener interface: void windowActivated(WindowEvent event); void windowClosed(WindowEvent event); void windowClosing(WindowEvent event); void windowDeactivated(WindowEvent event); void windowDeiconified(WindowEvent event); void windowIconified(WindowEvent event); void windowOpened(WindowEvent event); Often all these methods, except for one, have empty bodies, therefore there exists an adapter class (this is true for all awt listener interfaces with more than one method): package java.awt.event; public abstract class WindowAdapter implements WindowListener { public void windowOpened(WindowEvent event) {} public void windowClosing(WindowEvent event) {} //... and so on, only empty bodies }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 18 An Example Using the Adapter Class Solve problems 2 and 3, page 446. import java.awt.*; import javax.swing.*; import java.awt.event.*; class JFrame2 extends JFrame { public JFrame2() { Container guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); guiContainer.add(new JLabel("This window handles closing. Close the window!")); addWindowListener(new MyWindowListener()); } private class MyWindowListener extends WindowAdapter { public void windowClosing(WindowEvent event) { System.out.println("Here we may write to data files, etc."); System.exit(0); // Remember this! } class TestJFrame2 { public static void main(String[] args) { JFrame mySecondWindow = new JFrame2(); mySecondWindow.setTitle("My Second Window"); mySecondWindow.setSize(400, 100); mySecondWindow.setVisible(true); }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 19 Differences Between Applets and Applications The applet –The applet is a subclass of JApplet. –The applet class must have public access. –The user interface is constructed in the init() method. –The browser instantiates the applet object and sends the init() message to it. The application –The application is a subclass of JFrame. –Most of our applications have package access. –The user interface is constructed in the constructor. –The instantiation of application objects is coded in our programs, for example in the main() method.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 20 Differences Between Applets and Applications, an Example public class ColorButtonApplet2 extends JApplet { private Container guiContainer; public void init() { guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); ButtonListener theButtonListener = new ButtonListener(); myButton.addActionListener(theButtonListener); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { guiContainer.setBackground(Color.red); } class ColorButtonApplication extends JFrame { private Container guiContainer; public ColorButtonApplication(String title) { super(title); guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); ButtonListener theButtonListener = new ButtonListener(); myButton.addActionListener(theButtonListener); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { guiContainer.setBackground(Color.red); } class TestColorButtonApplication { public static void main(String[] args) { ColorButtonApplication myWindow = new ColorButtonApplication("My Window"); myWindow.pack(); myWindow.setVisible(true); myWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 21 Other Ways to Program Listeners: 1. The Window as Listener public class ColorButtonAppletThis extends JApplet implements ActionListener { private Container guiContainer; public void init() { guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); myButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { guiContainer.setBackground(Color.red); } The class describing the applet, is the same as the class implementing the ActionListener interface this is registered as a listener object The inner class ButtonListener is gone.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 14, page 22 Other Ways to Program Listeners: 2. An Anonymous Class