Swing from A to Z Using Focus in Swing, Part 2. Preface

Similar documents
Swing from A to Z Some Simple Components. Preface

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Processing Image Pixels, Creating Visible Watermarks in Java. Preface

Graphical User Interfaces (GUIs)

Multimedia Programming with Java Getting Started

Introduction to the JAVA UI classes Advanced HCI IAT351

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

Window Interfaces Using Swing Objects

More Swing. CS180 Recitation 12/(04,05)/08

Window Interfaces Using Swing Objects

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

The AWT Package, Graphics- Introduction to Images

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Systems Programming Graphical User Interfaces

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Lecture 5: Java Graphics

Fun with Java: Sprite Animation, Part 3. Preface

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

The AWT Package, Placing Components in Containers, CardLayout. Preface. Introduction

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Swing: Building GUIs in Java

Swing: Building GUIs in Java

Java. GUI building with the AWT

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

What is an Affine Transform?

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

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

CS 251 Intermediate Programming GUIs: Components and Layout

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Chapter 1 GUI Applications

Java Help Files. by Peter Lavin. May 22, 2004

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

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

Preface. This is one of the lessons in a miniseries that will concentrate on having fun while programming in Java.

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

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

SD Module-1 Advanced JAVA

CS 11 java track: lecture 3

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

1.00 Lecture 14. Lecture Preview

SD Module-1 Advanced JAVA. Assignment No. 4

Lecture 3: Java Graphics & Events

H212 Introduction to Software Systems Honors

Class 14: Introduction to the Swing Toolkit

Graphical interfaces & event-driven programming

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

(Incomplete) History of GUIs

Java Programming Lecture 6

Programming Languages and Techniques (CIS120e)

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CS Exam 1 Review Suggestions

Swing. By Iqtidar Ali

Programming Language Concepts: Lecture 8

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

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

COMPSCI 230. Software Design and Construction. Swing

1005ICT Object Oriented Programming Lecture Notes

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Graphic User Interfaces. - GUI concepts - Swing - AWT

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Together, the appearance and how user interacts with the program are known as the program look and feel.

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

Fun with Java: Sprite Animation, Part 4. Preface

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

Chapter 12 Advanced GUIs and Graphics

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

Computer Science 210: Data Structures. Intro to Java Graphics

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

JAVA NOTES GRAPHICAL USER INTERFACES

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

Chapter 13 Lab Advanced GUI Applications

2110: GUIS: Graphical User Interfaces

Packages: Putting Classes Together

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!

Lecture 9. Lecture

Class 16: The Swing Event Model

Java OOP: Java Documentation

CS 2113 Software Engineering

GUI Event Handlers (Part I)

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

SELF-STUDY. Glossary

CHAPTER 2. Java Overview

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

BASICS OF GRAPHICAL APPS

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

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

User interfaces and Swing

PIC 20A GUI with swing

CS11 Java. Fall Lecture 4

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

RAIK 183H Examination 2 Solution. November 11, 2013

Graphics User Defined Forms, Part I

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

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

MVC: Model View Controller

encompass a group of features for building Graphical User Interfaces (GUI).

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

Transcription:

Swing from A to Z Using Focus in Swing, Part 2 By Richard G. Baldwin Java Programming, Lecture Notes # 1042 November 27, 2000 Preface Introduction Sample Program Interesting Code Fragments Summary What's Next Complete Program Listing Preface This series of lessons entitled Swing from A to Z, discusses the capabilities and features of Swing in quite a lot of detail. This series is intended for those persons who need to understand Swing at a detailed level. Viewing tip You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the figures and listings while you are reading about them, without losing your place. Recommended supplementary reading In an earlier lesson entitled Alignment Properties and BoxLayout, Part 1, I recommended a list of my earlier Swing tutorials for you to study prior to embarking on a study of this set of lessons. Where are they located? You will find those lessons published at Gamelan.com. I also maintain a consolidated Table of Contents at Baldwin's Java Programming Tutorials that may make it easier for you to locate specific lessons. The Table of Contents on my site provides links back to each of the lessons at Gamelan.com.

Introduction The previous lesson In a previous lesson entitled Using Focus in Swing, Part 1, I introduced you to: The five focus properties of the JComponent class that are inherited by most Swing components. The focus traversal cycle. The use of the requestfocus() method. The focus properties The five focus properties of the JComponent class that are inherited by most Swing components are repeated below for convenient viewing. focuscycleroot focustraversable managingfocus requestfocusenabled nextfocusablecomponent Promises, promises... In the previous lesson, I also provided the AWT baseline for focus traversal. I promised that this lesson would extend the concept to include focus traversal in Swing, and would contrast the two different versions of the focus traversal cycle. I also promised that subsequent lessons will discuss the use of all five of the focus properties of Swing, and will show you how to create and use your own focus manager as a replacement for the default focus manager. Swing components are beans Swing components are JavaBean components, and most extend JComponent either directly or indirectly. We are usually interested in the methods, events, and properties of the bean. Most Swing components inherit the methods, events, and properties from JComponent and its superclasses. Understanding Swing components as a group If you understand the characteristics that Swing components inherit from JComponent, you already know a lot about a Swing component even before you begin examining it individually. In other words, we can learn a lot about Swing components by examining the JComponent class.

Sample Program This sample program, named Swing24, is the second in a series of programs designed to illustrate the use of focus in Swing. The screen shot in Figure 1 shows what the screen looks like when this sample program starts running. A side trip Figure 1 A Screen Shot of the Running Program Strictly as an aside, Figure 2 shows a screen shot from the previous lesson. What's the difference? Figure 2 A Screen Shot from the Previous Lesson Do you notice any difference between the two screen shots? The main difference is that Figure 1 shows the Swing GUI rendered in the Sun Metal look and feel, while Figure 2 shows the AWT GUI rendered under the Windows operating system, giving it a Windows look and feel. Swing provides pluggable look and feel With the AWT, the rendering of components is determined by the operating system. However, with Swing, you have the ability to change the look and feel at runtime largely independent of the operating system. (See my earlier lessons to learn more about pluggable look and feel.) Now back to the main thread Now lets return from our side trip and get back to the business at hand. The previous lesson illustrated two important aspects of AWT and Swing focus handling: Order of focus traversal

First, the order of focus traversal for the AWT is the order in which the components are placed in the container. This lesson will explain the order of focus traversal for Swing, which is different from the AWT. Requesting the focus Second, it is possible, with either the AWT or Swing, to request that the focus be shifted to a specific component by invoking the requestfocus() method on that component. The default starting point With both Swing and the AWT, the default is for the first component placed in the container to have the focus when the program starts running. In this case, that would be the East Button (later you will see that the East button is the first component placed in the container). Overriding the default However, a ComponentEvent handler in this program requests that the focus be shifted to the North Button immediately after the GUI is made visible. As shown in Figure 1, this causes the focus to reside on the North button by the time the GUI is available to the user. Focus traversal cycle Focus traversal can be observed by repeatedly pressing the Tab key. Pressing Shift-Tab will reverse the order of focus traversal. What is the order of focus traversal? If, like the AWT, the order of traversal were based on the order that the components are placed in the container, then the order of focus traversal for the GUI in Figure 1 would be North, Center, East, West, South, and back to North. Not like the AWT However, with the default Swing focus manager, the order of traversal is not based on the order that the components are placed in the container. Rather, the order of traversal proceeds from the upper left to the lower right regardless of the order that the components are placed in the container. Therefore, for the Swing GUI shown in Figure 1, the order of traversal is North, West, Center, East, South, and back to North. Modifying the order of focus traversal There are at least two different ways to modify the order of focus traversal in Swing.

One way is to manipulate the nextfocusablecomponent property. The second way is to replace the default focus manager with a new focus manager of your own design. Both approaches will be illustrated in subsequent lessons. Event handlers There are no event handlers registered on the five buttons placed in the JFrame in Figure 1. However, as mentioned earlier, a component listener is registered on the JFrame to invoke requestfocus() on the North button when the JFrame becomes visible. (You can only request focus on a component after it becomes visible. If you request focus before the component becomes visible, the request has no effect.) Interesting Code Fragments I will break this program down and discuss it in fragments. A listing of the entire program is provided in Listing 5. The controlling class Listing 1 shows the beginning of the controlling class named Swing24 along with the main() method. The fragment also shows the instantiation of a new JButton object with a label of "North". A subsequent code fragment will invoke the requestfocus() method to purposely shift the focus to this JButton component. /*File Swing24.java Rev 8/17/00 **********************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; class Swing24 extends JFrame{ JButton northbtn = new JButton("North"); public static void main(string args[]) { new Swing24(); }//end main() Listing 1 The controlling class extends JFrame

Note that the controlling class extends the JFrame class. JFrame is one of the top-level classes in Swing. Therefore the instance of the controlling class created in the main() method appears on the screen as a GUI. The constructor Listing 2 shows the beginning of the constructor. Swing24(){//Constructor add(new JButton("East"),"East"); add(new JButton("West"),"West"); add(new JButton("South"),"South"); add(northbtn,"north"); add(new JButton("Center"),"Center"); settitle("copyright 2000, R.G.Baldwin"); setsize(300,100); Listing 2 The layout manager Hopefully by now, you are familiar with the use of getcontentpane() in Swing. According to Sun, "The default contentpane() will have a BorderLayout manager set on it." Place components in the JFrame The first five statements in the constructor (highlighted in boldface) instantiate four anonymous JButton objects and place them, along with the JButton object instantiated earlier, in the East, West, South, North, and Center locations of the JFrame component. First component is placed in the East Note in particular that the first component is placed in the east position. By default, this is the button that would have the focus when the GUI first appears on the screen. However, the requestfocus() method will be used to prevent this button from having the focus when the GUI becomes available to the user.

Traversal doesn't match order of placement As explained earlier, unlike the AWT, the focus traversal order of components in a Swing container does not match the order in which the components are placed in the container. If it did, this would cause the focus traversal order among the buttons to be: East, West, South, North, Center, and back to East. The actual focus traversal cycle However, the focus traversal order provided by the default Swing focus manager is from left to right, top to bottom. Therefore, the actual focus traversal order of this GUI is North, West, Center, East, South, and back to North. Set title and size properties The remaining code in Listing 2 simply sets the title property and the size property of the JFrame. There's nothing exciting or unusual about that. An anonymous ComponentEvent listener Listing 3 is an anonymous ComponentEvent listener class that is used to cause the focus to reside on the North button when the GUI becomes available to the user. //Register component listener to request // focus on the north JButton after the // JFrame becomes visible. this.addcomponentlistener( new ComponentAdapter(){ public void componentshown( ComponentEvent e){ northbtn.requestfocus(); }//end componentshown }//end ComponentAdapter );//end addcomponentlistener //Make the JFrame visible setvisible(true); Listing 3 What does this code do? This code invokes the requestfocus() method on the reference to the North button. This causes the focus to shift to that button (after the GUI becomes visible on the screen).

This is essentially the same code that was explained in the previous lesson. Therefore, I won't discuss it further here. Make the JFrame visible The remaining code in Listing 3 sets the visible property to true, causing the JFrame to become visible on the screen. This is what actually triggers the componentshown() event that the earlier code in Listing 3 is listening for. The terminator... The code in Listing 4 registers an anonymous object that causes the program to terminate when the user clicks the close button on the JFrame. //Anonymous inner terminator class this.addwindowlistener( new WindowAdapter(){ public void windowclosing( WindowEvent e){ System.exit(0); }//end windowclosing() }//end WindowAdapter );//end addwindowlistener }//end constructor }//end class Swing24 Listing 4 In the previous lesson, I introduced you to: Summary The five focus properties of the JComponent class that are inherited by most Swing components. The focus traversal cycle. The use of the requestfocus() method. In that lesson, I also provided the AWT baseline for focus traversal. In this lesson, I have extended the concept to include focus traversal in Swing, and I have contrasted the two different versions of the focus traversal cycle. What's Next?

In the next lesson, I will show you how to control the focus root cycle in Swing. What I mean by focus root cycle is, when you create a composite GUI consisting of nested containers, you have the choice of allowing the focus to traverse smoothly into and out of a nested container, or of causing the focus to continually cycle within a nested container once it enters that container. This will be accomplished using the isfocuscycleroot() method and the focuscycleroot property. The lessons in this miniseries will discuss the use of all five of the focus properties of Swing, and will also show you how to create and use your own focus manager as a replacement for the default focus manager. Complete Program Listing A complete listing of the program is provided in Listing 5. /*File Swing24.java Rev 8/17/00 Copyright 2000, R.G.Baldwin This is one in a series of programs designed to illustrate the use of focus in Swing. This lesson illustrates how the focus traverses its cycle among Swing components. This will be contrasted with the manner in which the focus traverses its cycle in the AWT. Illustrates two important aspects of Swing focus handling: First, unlike the AWT, the order of focus traversal in Swing is left to right, top to bottom, regardless of the order in which the components are placed in the container. Second, it is possible to request that focus be moved to a specific component by invoking the requestfocus() method on that component. Like the AWT, the default is for the first component placed in the container to have the focus when the program starts running. In this case, that would be the East JButton. A ComponentEvent handler requests focus on the North JButton immediately after the GUI is made visible which causes the focus to reside on the North JButton by the time the GUI is available to the user. Thus, the order of focus traversal is

North, West, Center, East, South, and back to North. Focus traversal can be observed by repeatedly pressing the Tab key. Repeatedly pressing Shift-Tab will reverse the order of focus traversal among the components. There are no event handlers registered on the five JButtons placed in the JFrame. However, a component listener is registered on the JFrame to invoke requestfocus() on the North JButton when the JFrame becomes visible. (You can only request focus on a component after it becomes visible.) Tested using JDK 1.2.2 under WinNT 4.0 WkStn **********************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; class Swing24 extends JFrame{ JButton northbtn = new JButton("North"); public static void main(string args[]) { new Swing24(); }//end main() //---------------------------------------// Swing24(){//Constructor add(new JButton("East"),"East"); add(new JButton("West"),"West"); add(new JButton("South"),"South"); add(northbtn,"north"); add(new JButton("Center"),"Center"); settitle("copyright 2000, R.G.Baldwin"); setsize(300,100); //Register component listener to request // focus on the north JButton after the // JFrame becomes visible. this.addcomponentlistener( new ComponentAdapter(){ public void componentshown( ComponentEvent e){ northbtn.requestfocus(); }//end componentshown }//end ComponentAdapter );//end addcomponentlistener

//Make the JFrame visible setvisible(true); //Anonymous inner terminator class this.addwindowlistener( new WindowAdapter(){ public void windowclosing( WindowEvent e){ System.exit(0); }//end windowclosing() }//end WindowAdapter );//end addwindowlistener }//end constructor }//end class Swing24 Listing 5 Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited. About the author Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web. Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the hightech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine. Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems. baldwin.richard@iname.com -end-