More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.
Graphical User Interfaces
TCU CoSc Programming with Java Visual Design (Chapter 5)
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
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.
Corresponds with Chapter 12
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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 Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Graphical User Interfaces Tonga Institute of Higher Education.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Chapter 7 A First Look at GUI Applications Layout Managers.
A Quick Java Swing Tutorial
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.
Advanced GUIs II CS Lecture
Java GUI.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
Creating Graphical User Interfaces
AWT Components and Containers
Graphical User Interface
Zhen Jiang West Chester University
Programming Graphical User Interface (GUI)
GUI building with the AWT
Advanced GUIs II CS Lecture
Graphical User Interface
Presentation transcript:

More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check boxes –Radio buttons –Text boxes –Frames –Layout Managers

Note None of the code on these slides is meant to be copied & pasted, it’s for example only. (Showing the full code would take too much space). See the Microwave.java. All of these things are in there.

Microwave Example A silly implementation of a Microwave

Layout You’ve used FlowLayout: –FlowLayout is the default –Just lays the controls out in a line, and wraps when necessary, based on window shape

Grid Layout GridLayout creates a grid and puts each component in the grid, left to right, top to bottom

Grid Layout Cont. Layout managers manage the layout for canvases, like Frame or Applet: –setLayout(new GridLayout(rows, cols)) Note the widgets grow to fit the cells by default… there are many options to control formatting Entering a blank space in the grid: –Give it an empty Label: component.add(new Label()); Add components like usual: –this.add(Button)

Border Layout Border Layout creates five areas on the screen, “North”, “South”, “East”, “West”, and “Center”, and puts components in those areas. This screen shot put things in “North”, “Center”, and “South”; you can experiment with “East” and “West” Checkbox: North Choice: Center Radio buttons: South

Border Layout Cont. setLayout(new BorderLayout()) add() like usual Widgets don’t grow to fill available space

Frames Frames are separate dialog boxes, complete with closing button, maximize, minimize, etc.

Creating/Using a Frame frame = new Frame(); frame.add(some_widget); // adds a widget frame.setVisible(true); //shows the frame frame.setLayout(new BorderLayout()); // uses the BorderLayout manager frame.setSize(400,125); //width, height

Frame Events The widgets so far do not need event listeners (though they all can have listeners attached to them), but a Frame needs a WindowListener attached to it. Without a listener, the window will never close! frame.addWindowListener(new minimalFrameListener()); // see next slide for minimalFrameListener // change “name” at the bottom to your Frame’s name

Minimal Frame Listener public class minimalFrameListener implements WindowListener{ public void windowDeactivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowClosing(WindowEvent e) { name.setVisible(false); }

Panels These layouts are only rarely enough Panels can hold components and organized by the layout managers Panels can even hold other panels! Nest panels in panels to create relatively complicated UI’s.

Panel Code panel = new Panel() Example: This is actually a panel that contains a label and a choice, and was added to the “Center” of the border layout panel.add(new Label(“Set Heat Level”)); panel.add(powerSetting); this.add(“Center”, panel)

Nesting Panels Layouts too complicated for the simple managers can usually be created by creating several panels and composing them together Each panel can have its own Layout manager

More Widgets The toolkit you are using, “Abstract Window Toolkit” (AWT), has several more “widgets” “Widgets” are the things you interact with, like buttons, choice boxes, or radio buttons

Checkboxes Checkboxes are appropriate for Boolean questions: Yes or no. checkbox = new Checkbox(“Output result to stdout?”); Inside parameter is the label on the Checkbox Getting/Setting the state: Checkbox.setState(true); // checks the checkbox Checkbox.getState(); // returns true or false if checkbox.getState() { System.out.println(“It was checked.”); }

Radio Buttons Radio Buttons are checkboxes bundled into a group, Appropriate when only one choice of many is valid at a time Create a “CheckboxGroup” and pass it to each Checkbox in the constructor wavelengthGroup = new CheckboxGroup(); // no arguments microwave = new Checkbox(“Microwave”, wavelengthGroup, true); // last argument controls initial status infrared = new Checkbox(“Infrared”, wavelengthGroup, false); Getting the selected Checkbox: Checkbox selected = wavelengthGroup.getSelectedCheckbox() // note that returns a Checkbox reference

Choice Boxes Choice boxes are appropriate for when there are more then a few choices and only one is appropriate Do not use radio buttons for more than three or four choices. Use Choice boxes. Choice boxes can multi-select, but it is significantly more complicated to program. powerSetting = new Choice(); powerSetting.add(“Carbonize”); powerSetting.add(“Sear”); powerSetting.select(“Sear”); //selects a choice programmatically System.out.println(powerSetting.getSelectedItem()); // prints the item text

TextField Freeform text entry text = new TextField(“initial contents”, width); System.out.println(text.getText()); Text.setText(“Something.”);

MultiThreading Microwave sample has a countdown timer in a separate thread To create a thread: –create a class that extends Thread, and override “public void run()” –Instantiate that class –Call “start()” method See Microwave.java for example (too long to add here) Also see Timer in Counter thread