Chapter 17 Creating User Interfaces

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

CS111: PROGRAMMING LANGUAGE II

Graphical User Interfaces. Comp 152

user-friendly and easy to use.

CS 112 Programming 2. Lecture 16. JavaFX UI Controls & Multimedia (1) Chapter 16 JavaFX UI Controls and Multimedia

Swing. By Iqtidar Ali

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

EVENTS, EVENT SOURCES AND LISTENERS

Chapter 4. Swing 18 marks

Chapter 12 Advanced GUIs and Graphics

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!

Graphical User Interface (GUI)

Programming graphics

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

2110: GUIS: Graphical User Interfaces

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

JList. Displays a series of items The user can select one or more items Class JList extends directly class Jcomponent Class Jlist supports

Chapter 6: Graphical User Interfaces

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

More Swing. Chapter 14. Chapter 14 1

Window Interfaces Using Swing Objects

Java IDE Programming-I

CSC 1214: Object-Oriented Programming

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

JavaFX UI Controls and Multimedia

Window Interfaces Using Swing Objects

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

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

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

Contents Chapter 1 Introduction to Programming and the Java Language

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Class 16: The Swing Event Model

Chapter 18. Advanced graphics programming

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

To gain experience using GUI components and listeners.

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Chapter 12 GUI Basics

Unit 6: Graphical User Interface

Graphical User Interface (GUI)

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

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Part I: Learn Common Graphics Components

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

Index SELF-STUDY. Symbols

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Graphical Applications

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

Jonathan Aldrich Charlie Garrod

Graphical User Interfaces

Systems Programming Graphical User Interfaces

Window Interfaces Using Swing. Chapter 12

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

GUI Software Architecture

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

JAVA NOTES GRAPHICAL USER INTERFACES

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Introduction to the JAVA UI classes Advanced HCI IAT351

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

Introduction. Introduction

CoSc Lab # 5 (The Controller)

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

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

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Java Programming Lecture 6

Agenda. Container and Component

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

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Chapter 7: A First Look at GUI Applications

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

We are on the GUI fast track path

Contents Introduction 1

Summary Chapter 25 GUI Components: Part 2

Part 3: Graphical User Interface (GUI) & Java Applets

Human-Computer Interaction IS4300

GUI Event Handlers (Part I)

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

UNIT-V 1 KNREDDY JAVA PROGRAMMING

Sri Vidya College of Engineering & Technology

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

Dr. Hikmat A. M. AbdelJaber

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

BASICS OF GRAPHICAL APPS

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

CS 209 Programming in Java #13 Multimedia

Introduction to concurrency and GUIs

Graphic User Interfaces. - GUI concepts - Swing - AWT

Swing from A to Z Some Simple Components. Preface

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

PIC 20A GUI with swing

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

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

AP CS Unit 11: Graphics and Events

COMPSCI 230. Software Design and Construction. Swing

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

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Transcription:

Chapter 17 Creating User Interfaces 1

Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity and knowledge of how GUI components work. Since the GUI components in Java are very flexible and versatile, you can create a wide assortment of useful user interfaces. Previous chapters briefly introduced several GUI components. This chapter introduces the frequently used GUI components in detail. 2

Objectives To create graphical user interfaces with various user-interface components: JButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, JComboBox, JList, JScrollBar, and JSlider ( 17.2 17.11). To create listeners for various types of events ( 17.2 17.11). To explore JButton ( 17.2) To explore JCheckBox ( 17.3) To explore JRadioButton ( 17.4) To explore JLabel ( 17.5) To explore JTextField ( 17.6) To explore JTextArea ( 17.7) To explore JComboBox ( 17.8) To explore JList ( 17.9) To explore JScrollBar ( 17.10) To explore JSlider ( 17.11) To display multiple windows in an application ( 17.12). 3

Components Covered in the Chapter Introduces the frequently used GUI components Uses borders and icons 4

Buttons A button is a component that triggers an action event when clicked. Swing provides regular buttons, toggle buttons, check box buttons, and radio buttons. The common features of these buttons are generalized in javax.swing.abstractbutton. 5

AbstractButton 6

JButton JButton inherits AbstractButton and provides several constructors to create buttons. 7

JButton Constructors The following are JButton constructors: JButton() JButton(String text) JButton(String text, Icon icon) JButton(Icon icon) 8

JButton Properties text icon mnemonic horizontalalignment verticalalignment horizontaltextposition verticaltextposition icontextgap 9

Default Icons, Pressed Icon, and Rollover Icon A regular button has a default icon, pressed icon, and rollover icon. Normally, you use the default icon. All other icons are for special effects. A pressed icon is displayed when a button is pressed and a rollover icon is displayed when the mouse is over the button but not pressed. (A) Default icon (B) Pressed icon (C) Rollover icon 10

Demo TestButtonIcons Run 11

Horizontal Alignments Horizontal alignment specifies how the icon and text are placed horizontally on a button. You can set the horizontal alignment using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. At present, LEADING and LEFT are the same and TRAILING and RIGHT are the same. Future implementation may distinguish them. The default horizontal alignment is SwingConstants.TRAILING. 12

Vertical Alignments Vertical alignment specifies how the icon and text are placed vertically on a button. You can set the vertical alignment using one of the three constants: TOP, CENTER, BOTTOM. The default vertical alignment is SwingConstants.CENTER. 13

Horizontal Text Positions Horizontal text position specifies the horizontal position of the text relative to the icon. You can set the horizontal text position using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. The default horizontal text position is SwingConstants.RIGHT. 14

Vertical Text Positions Vertical text position specifies the vertical position of the text relative to the icon. You can set the vertical text position using one of the three constants: TOP, CENTER. The default vertical text position is SwingConstants.CENTER. 15

Example: Using Buttons Write a program that displays a message on a panel and uses two buttons, <= and =>, to move the message on the panel to the left or right. ButtonDemo Run 16

JCheckBox JCheckBox inherits all the properties such as text, icon, mnemonic, verticalalignment, horizontalalignment, horizontaltextposition, verticaltextposition, and selected from AbstractButton, and provides several constructors to create check boxes. 17

Example: Using Check Boxes Add three check boxes named Centered, Bold, and Italic into the ButtonDemo example to let the user specify whether the message is centered, bold, or italic. ButtonDemo CheckBoxDemo CheckBoxDemo Run 18

JRadioButton Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time. 19

Grouping Radio Buttons ButtonGroup btg = new ButtonGroup(); btg.add(jrb1); btg.add(jrb2); 20

Example: Using Radio Buttons Add three radio buttons named Red, Green, and Blue into the preceding example to let the user choose the color of the message. ButtonDemo CheckBoxDemo RadioButtonDemo RadioButtonDemo Run 21

JLabel A label is a display area for a short text, an image, or both. 22

JLabel Constructors The constructors for labels are as follows: JLabel() JLabel(String text, int horizontalalignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalalignment) JLabel(String text, Icon icon, int horizontalalignment) 23

JLabel Properties JLabel inherits all the properties from JComponent and has many properties similar to the ones in JButton, such as text, icon, horizontalalignment, verticalalignment, horizontaltextposition, verticaltextposition, and icontextgap. 24

Using Labels // Create an image icon from image file ImageIcon icon = new ImageIcon("image/grapes.gif"); // Create a label with text, an icon, // with centered horizontal alignment JLabel jlbl = new JLabel("Grapes", icon, SwingConstants.CENTER); // Set label's text alignment and gap between text and icon jlbl.sethorizontaltextposition(swingconstants.center); jlbl.setverticaltextposition(swingconstants.bottom); jlbl.seticontextgap(5); 25

JTextField A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description). 26

JTextField Constructors JTextField(int columns) Creates an empty text field with the specified number of columns. JTextField(String text) Creates a text field initialized with the specified text. JTextField(String text, int columns) Creates a text field initialized with the specified text and the column size. 27

JTextField Properties text horizontalalignment editable columns 28

JTextField Methods gettext() Returns the string from the text field. settext(string text) Puts the given string in the text field. seteditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. setcolumns(int) Sets the number of columns in this text field. The length of the text field is changeable. 29

Example: Using Text Fields Add a text field to the preceding example to let the user set a new message. TextFieldDemo Run 30

JTextArea If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text. 31

JTextArea Constructors JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns. JTextArea(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified. 32

JTextArea Properties text editable columns linewrap wrapstyleword rows linecount tabsize 33

Example: Using Text Areas This example gives a program that displays an image in a label, a title in a label, and a text in a text area. 34

Example, cont. TextAreaDemo Run 35

JComboBox A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. 36

JComboBox Methods To add an item to a JComboBox jcbo, use jcbo.additem(object item) To get an item from JComboBox jcbo, use jcbo.getitem() 37

Using the itemstatechanged Handler When a choice is checked or unchecked, itemstatechanged() for ItemEvent is invoked as well as the actionperformed() handler for ActionEvent. public void itemstatechanged(itemevent e) { // Make sure the source is a combo box if (e.getsource() instanceof JComboBox) String s = (String)e.getItem(); } 38

Example: Using Combo Boxes This example lets users view an image and a description of a country's flag by selecting the country from a combo box. ComboBoxDemo Run 39

JList A list is a component that performs basically the same function as a combo box, but it enables the user to choose a single value or multiple values. 40

JList Constructors JList() Creates an empty list. JList(Object[] stringitems) Creates a new list initialized with items. 41

JList Properties selectedindexd selectedindices selectedvalue selectedvalues selectionmode visiblerowcount 42

Example: Using Lists This example gives a program that lets users select countries in a list and display the flags of the selected countries in the labels. ListDemo Run 43

JScrollBar A scroll bar is a control that enables the user to select from a range of values. The scrollbar appears in two styles: horizontal and vertical. 44

Scroll Bar Properties 45

Example: Using Scrollbars This example uses horizontal and vertical scrollbars to control a message displayed on a panel. The horizontal scrollbar is used to move the message to the left or the right, and the vertical scrollbar to move it up and down. ScrollBarDemo Run 46

JSlider JSlider is similar to JScrollBar, but JSlider has more properties and can appear in many forms. 47

Example: Using Sliders Rewrite the preceding program using the sliders to control a message displayed on a panel instead of using scroll bars. SliderDemo Run 48

Creating Multiple Windows The following slides show step-by-step how to create an additional window from an application or applet. 49

Creating Additional Windows, Step 1 Step 1: Create a subclass of JFrame (called a SubFrame) that tells the new window what to do. For example, all the GUI application programs extend JFrame and are subclasses of JFrame. 50

Creating Additional Windows, Step 2 Step 2: Create an instance of SubFrame in the application or applet. Example: SubFrame subframe = new SubFrame("SubFrame Title"); 51

Creating Additional Windows, Step 3 Step 3: Create a JButton for activating the subframe. add(new JButton("Activate SubFrame")); 52

Creating Additional Windows, Step 4 Step 4: Override the actionperformed() method as follows: public actionperformed(actionevent e) { String actioncommand = e.getactioncommand(); if (e.target instanceof Button) { if ("Activate SubFrame".equals(actionCommand)) { subframe.setvisible(true); } } } 53

Example: Creating Multiple Windows This example creates a main window with a text area in the scroll pane, and a button named "Show Histogram." When the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area. 54

Example, cont. MultipleWindowsDemo Run Histogram 55