Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Similar documents
Chapter 17 Creating User Interfaces

Chapter 4. Swing 18 marks

Graphical User Interfaces. Comp 152

CS111: PROGRAMMING LANGUAGE II

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CSC 1214: Object-Oriented Programming

Graphical Applications

UNIT-V 1 KNREDDY JAVA PROGRAMMING

Chapter 7: A First Look at GUI Applications

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

EVENTS, EVENT SOURCES AND LISTENERS

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Outline CSE 3461 F10. What is a Widget? Properties of Widgets. A self-contained screen object Also called a control Examples of widgets:

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphical User Interface (GUI)

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

To gain experience using GUI components and listeners.

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Chapter 12 Advanced GUIs and Graphics

Chapter 18. Advanced graphics programming

Adding Buttons to StyleOptions.java

Programming graphics

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Chapter 6: Graphical User Interfaces

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Java Event Handling -- 1

Introduction. Introduction

BASICS OF GRAPHICAL APPS

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Unit 6: Graphical User Interface

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

GUI Event Handlers (Part I)

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

MIT AITI Swing Event Model Lecture 17

Swing Programming Example Number 2

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

More About Objects and Methods

Swing. Component overview. Java UI, summer semester 2017/2018 1

Contents Chapter 1 Introduction to Programming and the Java Language

Agenda. Container and Component

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Graphical User Interface (GUI)

SINGLE EVENT HANDLING

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

UNIT - 6 AWT SWINGS [ABSTARCT WINDOW TOOLKIT]

Swing/GUI Cheat Sheet

ICS3C0 Day 2 Multiple Choice Exam Review Please answer on the scantron card.

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 209 Programming in Java #13 Multimedia

CS180 Recitation. More about Objects and Methods

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

Java Applets This is not a Java course! (You re supposed to know about Java.)

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

We are on the GUI fast track path

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

GUI Forms and Events, Part II

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Java Programming Lecture 6

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Packages: Putting Classes Together

More Swing. Chapter 14. Chapter 14 1

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Systems Programming Graphical User Interfaces

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Example: Building a Java GUI

Window Interfaces Using Swing. Chapter 12

Unit - 2 Abstract Window Toolkit

Example: Building a Java GUI

Graphical User Interfaces

Dr. Hikmat A. M. AbdelJaber

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Window Interfaces Using Swing Objects

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

GUI Software Architecture

Java - Applets. public class Buttons extends Applet implements ActionListener

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Day 2: Review, ICS3U0, June 2017

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

Java IDE Programming-I

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

More About Objects and Methods

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Transcription:

Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani

The layout manager helps lay out the components held by this container. When you set a layout to null, you tell the container that it is using no layout at all, and so you the programmer are thus completely responsible for setting all the sizes and positions of components that are added to this container.

JLabel and ImageIcon JLabel is Swing s easiest-to-use component. It creates a label which can be used to display text and/or an icon. It is a passive component, which does not respond to user input. JLabel defines several constructors, here are 3 of them: JLabel(Icon icon) JLabel(String str) JLabel(String str, Icon icon, int align) The align argument specifies the horizontal alignment of the text and/or icon within the dimensions of the label. It must be one of the following values: LEFT, RIGHT, CENTRE, LEADING or TRAILING

ImageIcon ImageIcon(String filename) It obtains the image in the file named filename. The icon and text associated with the label can be obtained by the following methods: Icon geticon() String gettext() The icon and text associated with the label can be set by the following methods: void seticon(icon icon) void settext(string str) Therefore, using settext(string str) it is possible to change the text inside a label during program execution.

JTextField JTextField is the simplest Swing text component. It is also probably its most widely used text component. JTextField allows you to edit one line of text. Three of JTextField s constructors are shown here: JTextField(int cols) JTextField(String str, int cols) JTextField(String str) Here, str is the string to be initially presented, and cols is the number of columns in the text field. If no string is specified, the text field is initially empty. If the number of columns is not specified, the text field is sized to fit the specified string.

JTextField generates events in response to user interaction. For example, an ActionEvent is fired when the user presses ENTER. A CaretEvent is fired each time the caret (i.e., the cursor) changes position. To obtain the text currently in the text field, call gettext( ).

The Swing Buttons Swing defines four types of buttons: JButton JToggleButton JCheckBox JRadioButton The text associated with a button can be read and written via the following methods: String gettext( ) void settext(string str) A button generates an action event when it is pressed. Other events are possible.

JButton The JButton class provides the functionality of a push button. JButton allows an icon, a string, or both to be associated with the push button. Three of its constructors are shown here: JButton(Icon icon) JButton(String str) JButton(String str, Icon icon) Here, str and icon are the string and icon used for the button. When the button is pressed, an ActionEvent is generated. Using the ActionEvent object passed to the actionperformed( ) method of the registered ActionListener, you can obtain the action command string associated with the button. The action command identifies the button.

Thus, when using two or more buttons within the same application, the action command gives you an easy way to determine which button was pressed. You can obtain the action command by calling getactioncommand( ) on the event object. It is declared like this: String getactioncommand( )

b.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { System.out.println("Button Clicked..."); //t1.settext("button Clicked"); //no2.settext("button Clicked"); t1.settext("clicked = " + e.getactioncommand()); } });

JToggleButton A toggle button looks just like a push button, but it acts differently because it has two states: pushed and released. Toggle buttons are objects of the JToggleButton class. JToggleButton defines several constructors. JToggleButton(String str) This creates a toggle button that contains the text passed in str. By default, the button is in the off position. When a JToggleButton is pressed in, it is selected. When it is popped out, it is deselected.

JToggleButton generates an action event each time it is pressed. It also generates an item event. To handle item events, you must implement the ItemListener interface. Each time an item event is generated, it is passed to the itemstatechanged( ) method defined by ItemListener. Inside itemstatechanged( ), the getitem( ) method can be called on the ItemEvent object to obtain a reference to the JToggleButton instance that generated the event. Object getitem( ) A reference to the button is returned.

The easiest way to determine a toggle button s state is by calling the isselected( ) method on the button that generated the event. boolean isselected( ) It returns true if the button is selected and false otherwise.

Check Boxes The JCheckBox class provides the functionality of a check box. Selecting multiple options from multiple choices. JCheckBox defines several constructors. JCheckBox(String str) It creates a check box that has the text specified by str as a label. Other constructors let you specify the initial selection state of the button and specify an icon.

When the user selects or deselects a check box, an ItemEvent is generated. You can obtain a reference to the JCheckBox that generated the event by calling getitem() on the ItemEvent passed to the itemstatechanged( ) method defined by ItemListener. The easiest way to determine the selected state of a check box is to call isselected( ) on the JCheckBox instance.

Radio Buttons Radio buttons are a group of mutually exclusive buttons, in which only one button can be selected at any one time. Selecting one option from multiple choices. JRadioButton provides several constructors. JRadioButton(String str) Here, str is the label for the button. Other constructors let you specify the initial selection state of the button and specify an icon. A JRadioButton generates action events, item events, and change events each time the button selection changes.

In order for their mutually exclusive nature to be activated, radio buttons must be configured into a group. Only one of the buttons in the group can be selected at any time. For example, if a user presses a radio button that is in a group, any previously selected button in that group is automatically deselected. A button group is created by the ButtonGroup class. Its default constructor is invoked for this purpose. Elements are then added to the button group via the following method: void add(abstractbutton ab) Here, ab is a reference to the button to be added to the group.

References Herbert Schildt, The Complete Reference, JAVA, 7 th ed