Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Information Technology Center Hany Abdelwahab Computer Specialist.
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.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Programming Task: Task 1 Controlled Assessment Practice.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
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.
CS413: Java Programming language Applications Applets
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Software Construction Lecture 10 Frameworks
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
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.
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.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
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.
Graphical User Interface (GUI)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
GUI building with the AWT
Welcome To java
Lecture 09 Applets.
Lecture 27 Creating Custom GUIs
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Event loops.
GUI building with the AWT
Event loops 17-Jan-19.
Constructors, GUI’s(Using Swing) and ActionListner
GUI building with the AWT
Event loops.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010

What Is an Applet? a special Java class that doesn't run in a DOS window runs in a web browser on client, in a window, on a HTML page Java has security features to keep the applet from acting like a virus on the client machine

Creating an Applet need import statements to bring in classes, etc. that Applet will use import java.applet.*; import java.awt.*; put extends Applet after class name has Applet functionality, plus what you add has a public void init() method has no main() method // code below

GUI Components components are objects (or like objects) GUI means graphic user interface graphic is the opposite of text you see something other than just plain text interface is what the user works with when using a computer program

GUI Components for Applets some "heavyweight" (platform dependent) components in Java Label: writes text on the Applet Button: push button to trigger action TextField: one line input box TextArea: multi-line input box others: scrollbar, etc. (similar to many components in Windows programs)

"Heavyweight" vs. Swing earlier Java versions had "heavyweight" platform dependent AWT (abstract windowing toolkit) components different on different operating systems "lightweight" Swing components are platform independent in many ways e.g., look the same on PC or Apple

JOptionPane Java Swing class (lightweight) "bean" that includes components including: Label, TextField Buttons Java beans: classes with extra features makes them easier to use with other classes, languages, and on different platforms

Additional Components the Java Swing class has additional GUI components (that are NOT platform dependent) JCheckBox: yes or no choices JRadioButton: can only select 1 in a group JComboBox: drop down list that user can type one or more characters into

Using GUI Components need to declare the object Label aLabel = new Label("Hello"); Button aButton = new Button("Click"); need to add objects to the Applet, usually in its init() method public void init() { add(aLabel); }

GUI Component Methods can use methods to set or change properties on components font on labels (type, style, size) text on a button e.g., OK input box size(s) default content inside an input box make content of input box editable or not

GUI Component Methods - 2 can use methods to manipulate components set focus (cursor position) onto a TextField get contents (user input) from a TextField e.g., getting String from JOptionPane

Event-Driven Programming event = something that happens mouse click, mouse over, mouse out, button push, key stroke, time of day, etc. event-driven program flow is (partially) controlled by external events rather than just by internal logic

"Listener" object that waits for an event and then triggers a method that responds to it associated with another object e.g., button or text component

Using a Listener need another import statement at top import java.awt.event.*; the class also needs to "implement" an ActionListener public class Greet extends Applet implements ActionListener

Using a Listener - 2 attach a listener method to an object public void init() { add(aButton); // declared above init aButton.addActionListener(this); }

Using a Listener - 3 add an actionPerformed method public void actionPerformed(ActionEvent thisEvent) { aLabel.setText("Hi"); } /*will get fatal error if have an ActionListener without actionPerformed method in class*/

Adding Interactive Output can add statements to actionPerformed method e.g., labels with text from user inputs may need to force Applet to redraw itself by adding following methods: invalidate(); // marks Applet as out-of-date validate(); // redraws out-of-date Applet

Other Interactivity can also add methods to actionPerformed method to remove GUI components remove(aLabel);

Controlling Position when add components without a layout manager, Java chooses where to put each item left to right until row is filled, and then starts a new row below previous as space permits, centered in row can subsequently move components to locations identified by coordinates

Window Coordinate System 1 st number is x (horizontal) position 2 nd number is y (vertical) position upper left corner is 0, 0 100, 0 is top of window, 100 pixels to right of upper left corner 0, 100 is left side of window, 100 pixels down from upper left corner 100, 100 is 100 pixels over, 100 down

Placing Components on Applet use setLocation method with a component myLabel.setLocation(50, 150); // where?

Disable and Enable disable makes it impossible to click a button, checkbox, etc. e.g. disable Print button until inputs are entered and calculations are completed enable makes the item functional again enable Print button when data is OK

Enabling Components clickButton.setEnabled(false); button previously declared by user will not respond to clicks clickButton.setEnabled(true); default condition, don't have to set to true if didn't previously set it to false