Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.

Slides:



Advertisements
Similar presentations
Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package To understand the inheritance hierarchy of the AWT.
Advertisements

Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.
1 Chapter 13 Creating User Interfaces. 2 Objectives F To create graphical user interfaces with various user-interface components: JButton, JCheckBox,
1 Simple Phone Applet Lab Mobile Phone Display Area Send, Menu and End Numbers 0-9 * and #
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Components. Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent –TextArea –TextField.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
TCU CoSc Introduction to Programming (with Java) Java Components.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
1 Lesson: Applets with User Input and Output with GUI ICS4M.
1 Chapter Ten Using Controls. 2 Objectives Learn about Controls How to create a Form containing Labels How to set a Label’s Font How to add Color to a.
1 Lecture 6 Using AWT controls, Layout Managers, and Menus.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Using AWT Controls, Layout Managers, and Menus Controls are components that allow a user to interact with your application in various ways A layout manager.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Java Applet Basics (2). The Body Mass Index Calculator.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Graphical User Interfaces. Graphical input and output with JOptionPane.
Csc Basic Graphical User Interface (GUI) Components.
AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Canvas Button Event Check Box Choice Label Object Component List
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
Event and GUI programming 1. Event Handling in Java An object resides in a particular state until it is made to transit to other state. This transition.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Chapter 4: Laying out the GUI Useful containers –Panels –Frames –Dialogs Layout mechanisms –FlowLayout –BorderLayout –GridLayout –CardLayout Menus.
CSC 205 Programming II Lecture 5 AWT - I.
Fundamental of Java Programming Abstract Window Toolkit
TOPICS Labels Using Buttons Applying Check Boxes CheckBox Group
Java Swing.
GUI Components Part II.
Abstract Window ToolKit (AWT)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Chapter 13 Creating User Interfaces
Components.
AWT Components and Containers
Ch. No Name Marks 01 AWT & SWING EVENT HANDLING NETWORKING
Graphics Programming - Components
Programming Graphical User Interface (GUI)
AWT Components Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e.
A picture's worth a thousand words
Advanced GUIs and Graphics
Presentation transcript:

Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice List

Building a GUI Graphical User Interfaces composed of widgets (buttons, text fields, etc.) Various useful classes defined in AWT Part of Component class (abstract class)

Component methods (inherited by all): Point location() - x,y position Dimension size() - width,height of object Rectangle bounds() - both location, size info void move (int x, int y) - set x,y position void resize (int w, int h) - set w,h of object void resize (Dimension d) - similar void reshape (int x, int y, int w, int h) - set rectangle Dimension minimumSize() - determined by system, smallest size needed Dimension preferredSize() - similar to minimum

Component methods (cont): Color getBackground() - return background color Color getForeground() - return foreground color void setBackground(Color c) - set background color void setForeground(Color c) - set foreground color Font getFont() - return current font void setFont(Font f) - set current font boolean isShowing() - true if Component on screen boolean isVisible() - true if object should be visible void hide() - turns component invisible void show() - makes component visible

Component methods (cont): Graphics getGraphics() - returns the Graphics object associated with the current component, useful for doing graphical operations on this component void paint(Graphics g) - method that “shows” component, should not be called by user (overridden) void repaint() - informs the computer that this component needs to be repainted void repaint(int x, int y, int width, int height) - similar to repaint() but indicates only the portion falling in the rectangle needs to be redone void update(Graphics g) - how component redone (generally box erased, repainted), useful to avoid flicker, should not be called by user (overridden)

Text Classes Label - simple text statements, generally one line with no editing expected TextComponent - abstract class with methods used by –TextField - one line text, with some editing –TextArea - generally for more than one line of text with some editing

Label class constructors: Label() - no text, center alignment Label(String label) - center-aligned label with text Label(String label, int alignment) - label with text aligned based on arg, vals: Label.LEFT, CENTER, RIGHT methods: String getText() - return string associated with Label void setText(String label) - set string int getAlignment() - return Label’s current alignment void setAlignment(int alignment) - set alignment

TextComponent class abstract class (parent of TextField, TextArea) methods: String getText() - return the text associated with object void setText(String text) - set text of object boolean isEditable() - flag indicating text can be edited void setEditable(boolean canEdit) - set editable flag String getSelectedText() - return highlighted text int getSelectionStart() - location where selection starts int getSelectionEnd() - location where selection ends void select (int start, int end) - select text start-end void selectAll() - select all of text

TextField class - one line, editing constructors: TextField() - empty (no text), default (system) width TextField(int columns) - no text, columns wide TextField(String text) - set text, use it for width TextField(String text, int columns) - set text, width methods: int getColumns() - return column width of object void setColumns(int c) - set column width char getEchoChar() - return echo char (if any) void setEchoChar(char c) - set echo char boolean echoCharIsSet() - check if echo char set Dimension minimumSize() - minimum needed size Dimension preferredSize() - preferred size

TextArea class - > 1 line, editing look and feel depends on system (may include scrollbars) constructors: TextArea() - empty (no text), default (system) size TextArea(int rows, int cols) - no text, rows, cols size TextArea(String text) - set text, default sized TextField(String text, int rows, int cols) - set text, size methods: int getColumns() - column width of object int getRows() - row height of object

TextArea class (cont) methods: void appendText(String str) - add string to the end of the current text void insertText(String str, int pos) - insert string at position pos void replaceText(String str, int start, int end) - insert string replacing chars from start to end Dimension minimumSize() - minimum required size Dimension preferredSize() - preferred size

import java.applet.*; import java.awt.*; public class TestClass extends Applet { Label myLabel1 = new Label("Test label 1",Label.LEFT); Label myLabel2 = new Label(); TextField myTextField1 = new TextField("Init text",20); TextField myTextField2 = new TextField("Start text"); TextArea myTextArea = new TextArea("Initial text",3,10); public void init () { resize(500,400); myLabel1.setText("TLab1"); myLabel2.setText("TLab2"); myTextField1.setForeground(Color.green); myTextField2.setEchoCharacter('*'); myTextArea.insertText("silly ",8); add(myLabel1); add(myTextField1); add(myTextArea); add(myLabel2); add(myTextField2); }

Active Widgets Various classes provide mechanisms for users to indicate info: –Button - labeled object that can be clicked –Checkbox - labeled object, can be checked/un –Choice - object providing multiple choices with one showing –List - object providing multiple choices with all showing

Button class constructors: Button() - unlabeled button Button(String label) - button with label methods: String getLabel() - return label of object void setLabel(String label) - set label of Button We will learn later how to check if the mouse has been used to access button

Checkbox class constructors: Checkbox() - no label, initially unchecked Checkbox(String label) - labeled, initially unchecked Checkbox(String label, CheckboxGroup group, boolean state) * methods: String getLabel() - return label of object void setLabel(String label) - set label of Checkbox boolean getState() - determine if checked/unchecked void setState(boolean state) - set the state of the Checkbox CheckboxGroup getCheckboxGroup() - returns group *

CheckboxGroup class Checkbox objects added using Checkbox methods constructors: CheckBoxGroup() - empty group methods: Checkbox getCurrent() - Checkbox of group that is checked void setCurrent(Checkbox c) - set c as checked member

import java.applet.*; import java.awt.*; public class TestButton extends Applet { Button myButton = new Button("AButton"); Checkbox myCheckBox1 = new Checkbox("CB1"); Checkbox myCheckBox2 = new Checkbox("CB2"); CheckboxGroup myCheckBoxGroup = new CheckboxGroup(); Checkbox myCBG1 = new Checkbox("Big"); Checkbox myCBG2 = new Checkbox("Bigger"); Checkbox myCBG3 = new Checkbox("Biggest"); public void init () { resize(500,400); myCheckBox2.setState(true); myCBG1.setCheckboxGroup(myCheckBoxGroup); myCBG2.setCheckboxGroup(myCheckBoxGroup); myCBG3.setCheckboxGroup(myCheckBoxGroup); myCheckBoxGroup.setCurrent(myCBG1); add(myCheckBox1); add(myButton); add(myCBG1); add(myCBG2); add(myCBG3); add(myCheckBox2); }

Choice class constructor: Choice() - initially empty set of choices methods: void addItem(String item) - add the named item to the choices int getSelectedIndex() - return num of selected item String getSelectedItem() - return string associated with item String getItem(int index) - returns string of index item void select(int index) - select the index item (starting at 0) void select(String item) - select item with string name int countItems() - return number of items

List class constructors: List() - default-size list in single-selection mode List(int rows, boolean multSel) - list with rows, if multSel true can make multiple selections methods: void addItem(String item) - add the named item to the choices void addItem(String item, int index) - add item at loc index void delItem(int index) - remove the index item void delItems(int start, int end) - remove items from start to end void clear() - remove all items void replaceItem(String item, int index) - replace item at index int countItems() - count of the number of items in List

methods: void getSelectedIndex() - num of selected index -1 if multiple or none boolean isSelected(int index) - true if index selected void select(int index) - set index as selected void deselect(int index) - unselect index void makeVisible(int index) - scroll so index visible int getVisibleIndex() - index of last makeVisible call boolean allowsMultipleSelections() - true if list allows multiple selections void setMultipleSelections(boolean m) - set multiple selection flag to m

methods: int getRows() - number of rows in object Dimension minimumSize() - min size needed Dimension minimumSize(int rows) - min size for rows Dimension preferredSize() - preferred size Dimension preferredSize(int rows) - preferred by rows

import java.applet.*; import java.awt.*; public class TestLists extends Applet { Choice myChoice = new Choice(); List myList = new List(4,true); public void init () { resize(500,400); myChoice.addItem("Small"); myChoice.addItem("Medium"); myChoice.addItem("Large"); myChoice.select("Medium"); myList.addItem("Left"); myList.addItem("Right"); myList.addItem("Straight"); myList.select(0); myList.select(2); add(myChoice); add(myList); }