12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.

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

Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
More on GUI We wrap up our examination of Java GUI programming by looking at several other components and their listeners – The components all come from.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
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.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Components. Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent –TextArea –TextField.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
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.
GUI Programming in Java: Event Handling & More Components Corresponds with Chapter 14, Chapter 15.
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 --
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
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,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
EVENTS Wiring together objects, code, and actions.
1 Lecture 6 Using AWT controls, Layout Managers, and Menus.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Getting Input. Text Fields A text field is a box that the user can type in Use the JTextField class JTextField tf1 = new JTextField(15); 15 is the field.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
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 User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
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.
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,
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Aum Amriteshwaryai Namah
TOPICS Labels Using Buttons Applying Check Boxes CheckBox Group
Graphical User Interface (pronounced "gooey")
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
JAVA AWT.
AWT Components and Containers
Ch. No Name Marks 01 AWT & SWING EVENT HANDLING NETWORKING
GUI building with the AWT
Programming Graphical User Interface (GUI)
GUI building with the AWT
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
Presentation transcript:

12-Jul-15 Components

2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField

3 Button new Button() constructs a Button with no text new Button( String ) constructs a Button with the specified text void addActionListener( ActionListener ) void removeActionListener( ActionListener ) String getLabel() void setLabel( String ) String actionEvent.getActionCommand() Returns the “action command” associated with this Button By default, the action command = the text on the Button

4 A Button listener button.addActionListener (new MyButtonListener ()); class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { showStatus ("Button clicked."); } }

5 Label constructors new Label() constructs a Label with no text new Label( String ) constructs a Label with the specified text new Label( String, alignment ) constructs a label with the specified text and alignment: Label.LEFT Label.RIGHT Label.CENTER (These are int constants defined in the Label class)

6 Label methods String getText() void setText( String ) int getAlignment() void setAlignment( int )

7 Scrollbar constructors new Scrollbar() creates a vertical Scrollbar new Scrollbar(int orientation ) Scrollbar.HORIZONTAL or Scrollbar.VERTICAL new Scrollbar(int orientation, int initialValue, int bubbleSize, int minimum, int maximum ) bubbleSize: size of the bubble (sometimes called elevator) min value of Scrollbar = minimum max value of Scrollbar = maximum - bubbleSize ( value of scrollbar is given by left or top edge of bubble) initialValue: the initial position setting of the bubble

8 Scrollbars bubbleSize maximum maximum — bubbleSize minimum The units for bubbleSize are the same as those for minimum and maximum The scrollbar reading is taken from the left edge of the bubble

9 Scrollbar methods void addAdjustmentListener( AdjustmentListener ) void removeAdjustmentListener( AdjustmentListener ) int getMinimum() void setMinimum( int ) int getMaximum() void setMaximum( int ) int getValue() void setValue( int )

10 A Scrollbar listener scrollbar.addAdjustmentListener(new MyScrollbarListener ()); class MyScrollbarListener implements AdjustmentListener { public void adjustmentValueChanged (AdjustmentEvent event) { showStatus ("Scrollbar: " + scrollbar.getValue ()); } };

11 Checkbox constructors new Checkbox() new Checkbox( String ) new Checkbox( String, boolean ) new Checkbox( String, boolean, CheckboxGroup ) new Checkbox( String, CheckboxGroup, boolean )

12 Checkbox methods void addItemListener( ItemListener ) void removeItemListener( ItemListener ) CheckboxGroup getCheckboxGroup () void setCheckboxGroup( CheckboxGroup ) String getLabel() void setLabel( String )

13 A Checkbox listener checkbox.addItemListener (new MyCheckboxListener ()); class MyCheckboxListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("Checkbox status is now " + (event.getStateChange () == ItemEvent.SELECTED)); } };

14 CheckboxGroup methods Checkbox getSelectedCheckbox() Suggested use: myCG.getSelectedCheckbox().getLabel() void setSelectedCheckbox( Checkbox )

15 A CheckboxGroup listener No such thing—listen to individual Checkboxes checkbox1.addItemListener (myCheckboxGroupListener); checkbox2.addItemListener (myCheckboxGroupListener); class MyCheckboxGroupListener implements ItemListener { public void itemStateChanged (ItemEvent event) { Checkbox d = checkboxGroup.getSelectedCheckbox (); showStatus ("CheckboxGroup: " + d.getLabel ()); } }

16 Choice constructor new Choice() After constructing a Choice, use add( String ) to add items to it

17 Choice methods addItemListener( ItemListener ) removeItemListener( ItemListener ) void add ( String ) void remove( String ) void removeAll() String getSelectedItem() void select( String )

18 A Choice listener choice.addItemListener (new MyChoiceListener ()); class MyChoiceListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("Choice: " + choice.getSelectedItem ()); } };

19 List constructors new List() constructs an empty list new List( int ) int : the number of items visible (a scrollbar is automatically added if needed) new List(int, boolean ) boolean: true if multiple selection is allowed

20 List methods I void addItemListener( ItemListener ) void removeItemListener( ItemListener ) void add( String ) void remove( String ) void removeAll()

21 List methods II String getSelectedItem() String[] getSelectedItems() int getSelectedIndex() int[] getSelectedIndexes() void select(int) // can only select by index There are other methods for adding, removing, etc. by index

22 A List listener list.addItemListener (new MyListListener ()); class MyListListener implements ItemListener { public void itemStateChanged (ItemEvent event) { showStatus ("List: " + list.getSelectedItem ()); } };

23 TextComponent No constructors Instead, construct a TextField or TextArea void addTextListener( TextListener ) void removeTextListener( TextListener ) String getText() void setText( String ) boolean isEditable() void setEditable( boolean )

24 TextField constructors new TextField() new TextField( int ) int : the approximate desired number of columns new TextField( String ) String : the initial text in the TextField new TextField( String, int )

25 TextField methods void addActionListener( ActionListener ) void removeActionListener( ActionListener ) The ActionListener for a TextField responds only when the Enter key is pressed

26 TextArea constructors new TextArea() new TextArea(int rows, int columns ) new TextArea(int rows, int columns, int scroll ) TextArea.SCROLLBARS_VERTICAL_ONLY TextArea.SCROLLBARS_HORIZONTAL_ONLY TextArea.SCROLLBARS_BOTH TextArea.SCROLLBARS_NONE new TextArea( String ) new TextArea( String, int rows, int columns )

27 TextComponent listeners textField.addActionListener (new MyTextFieldListeners ()); textField.addTextListener (new MyTextFieldListeners ()); class MyTextFieldListeners implements ActionListener,TextListener { public void actionPerformed (ActionEvent event) { showStatus ("TextField (ActionListener): " + textField.getText ()); } public void textValueChanged (TextEvent event) { showStatus ("TextField (TextListener): " + textField.getText ()); } };

28 Suggestion: Don’t use text listeners! Text fields are intended to be passive; that is, nothing should happen immediately as a result of typing into them Consequently, I’ve seen a lot of programs in which text listeners do their work at the wrong time It usually works much better to ignore text fields until an active control (such as a Button ) is used, and then do getText() to find the current value in the text field

29 Canvas This is a rectangular blank area that you can paint on Constructor: new Canvas() Don't use Canvas directly; instead, subclass it and override its void paint( Graphics ) method Or even simpler: Paint directly on a Panel

30 The End