Chapter 141 More Swing Chapter 14. 2 Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the.

Slides:



Advertisements
Similar presentations
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 28 Containers, Layout.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 Chapter 4 l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Swing II.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Java Programming Chapter 10 Graphical User Interfaces.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Swing II. Swing II Topics WindowListener interface WindowListener interface Icons Icons Scrollbars Scrollbars The Graphics class The Graphics class Colors.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Menus l Making GUIs Pretty (and More Functional) l Box Containers.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
3461 Laying Out Components Interior Design for GUIs.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Chapter 14- More Swing, Better looking applications.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
GUIs & Event-Driven Programming Chapter 11 Review.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Swing.
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Programming III
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
Presentation transcript:

Chapter 141 More Swing Chapter 14

2 Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the Box class learn about inner classes learn about the WindowListener interface learn how to change GUI components to visible or invisible

Chapter 143 Outline Menus Making GUIs Pretty and More Functional More Layout Managers Inner Classes More on Events and Listeners The Swing Class Hierarchy Reconsidered

Chapter 144 Menus: Outline Programming Example: A GUI with a Menu Menu Bars, Menus, and Menu Items Nested Menus

Chapter 145 Programming Example: A GUI with a Menu class MemoGUI

Chapter 146 Programming Example: A GUI with a Menu, cont. class MemoGUI, cont.

Chapter 147 Programming Example: A GUI with a Menu, cont.

Chapter 148 Menu Bars, Menus, and Menu Items You add menus using three Swing classes: –JMenuBar –JMenu –JMenuItem JMenuItem s are placed in a JMenu, and a JMenu typically is placed in a JMenuBar. By default, an object of class JMenuItem is identified by the string that labels it.

Chapter 149 Menu Bars, Menus, and Menu Items, cont. Using method add, you can add as many JMenuItem s as you wish to a menu. –example JMenu_Name.add(JMenu_Item); The menu lists them in the order in which they are added. Listeners are added using JMenu_Item_Name.addActionListener (Action_Listener);

Chapter 1410 Menu Bars, Menus, and Menu Items, cont. Method actionPerformed is defined for menu items the same way it is defined for buttons. The menu in our example includes an additional entry labeled Exit.

Chapter 1411 Menu Bars A menu bar is a container for a menu. Typically it is placed near the top of a windowing interface. Menus are added to the menu bar using JMenu_Bar_Name.add(JMenu_Name); A menu bar can be added to a JFrame using setJMenuBar(JMenu_Bar_Name);

Chapter 1412 Menu Bars, cont. Alternatively, a menu bar can be added to the content pane of a JFrame or other container.

Chapter 1413 Setting the Action Command for a Menu Item If you do not wish to use the text for a JMenuItem as the default action command, you can set the action command using Menu_Item_Object.setActionCommand (Action_Command_String);

Chapter 1414 Nested Menus Class JMenu descends from class JMenuItem, so every JMenu object is also a JMenuItem object. Thus, a JMenu can be a menu item in another menu, permitting menus to be nested (cascading menus).

Chapter 1415 Making GUIs Pretty and More Functional: Outline Adding Icons The JScrollPane Class for Scroll Bars Adding Borders

Chapter 1416 Adding Icons Typically, an icon is simply a small picture. Labels, buttons, menu items, and other components can have icons. A label or button can have just a string, just an icon, both, or neither. A picture in almost any standard format can be used as the basis for an icon.

Chapter 1417 Converting a Picture to a Swing Icon You use class ImageIcon to convert a picture file to a Swing Icon. –example ImageIcon dukeWavingIcon = new ImageIcon(“duke_waving.gif”); You can use a relative or complete path name to specify the picture file.

Chapter 1418 Adding an Icon to a Label and a Button To produce a button with just an icon on it, you use JButton dukeButton = new JButton(dukeWavingIcon); –setActionCommand should be used explicitly to give the button an action command.

Chapter 1419 Placing an Icon and a String on a Label (or Button) example JButton helloButton = new JButton(“Hello”); ImageIcon dukeWavingIcon = new ImageIcon(“dukeWaving.gif”); helloButton.setIcon(dukeWavingIcon);

Chapter 1420 Placing an Icon and a String on a Label (or Button), cont. class IconDemo

Chapter 1421 Placing an Icon and a String on a Label (or Button), cont.

Chapter 1422 Some Methods in the Classes JButton and JLabel to create a button or label with no text and no icon public JButton() public JLabel() to create a button or label with text public JButton(String text) public JLabel(String text)

Chapter 1423 Some Methods in the Classes JButton and JLabel, cont. to create a button or label with an icon public JButton(ImageIcon Picture) public JLabel(ImageIcon Picture) to create a button or label with both text and an icon public JButton(String text, ImageIcon Picture) public JLabel(String text, ImageIcon Picture)

Chapter 1424 Some Methods in the Classes JButton and JLabel, cont. to make text the text on the already created button or label public void setText(String text) to make picture the icon on the already created button or label public void setIcon(ImageIcon picture)

Chapter 1425 Some Methods in the Classes JButton and JLabel, cont. to set the size of the margin (in pixels) around the text and icon in the button (but not the label) public void setMargin(Insets margin) or public void setMargin (new Insets(int top, int left, int bottom, int right))

Chapter 1426 Some Methods in the Classes JButton and JLabel, cont. to set the preferred size (in pixels) of the button or label public void setPreferredSize( Dimension(preferredSize)) or public void setPreferredSize( new Dimension(int width, int height))

Chapter 1427 Some Methods in the Classes JButton and JLabel, cont. to set the maximum size (in pixels) of the button or label public void setMaximumSize( Dimension(maximumSize)) or public void setMaximumSize( new Dimension(int width, int height))

Chapter 1428 Some Methods in the Classes JButton and JLabel, cont. to set the minimum size (in pixels) of the button or label public void setMinimumSize( Dimension(minimumSize)) or public void setMinimumSize( new Dimension(int width, int height))

Chapter 1429 Some Methods in the Classes JButton and JLabel, cont. to set the vertical position of the text relative to the icon public void setVerticalTextPosition (int textPosition) where textPosition is one of the constants SwingConstants.TOP SwingConstants.CENTER (default) SwingContants.BOTTOM

Chapter 1430 Some Methods in the Classes JButton and JLabel, cont. to set the horizontal position of the text relative to the icon public void setHorizontalTextPosition (int textPosition) where textPosition is one of the constants SwingConstants.RIGHT (default) SwingConstants.LEFT SwingConstants.CENTER SwingContants.LEADING SwingConstants.TRAILING

Chapter 1431 Resizing Buttons The methods for setting the preferred, maximum, and minimum sizes are only recommendations to the layout manager. An image may be clipped (NOT resized) if the icon is too big.

Chapter 1432 Classes Dimension and Inset Objects of classes Dimension and Inset are used with buttons, labels, and other objects. The numbers are in pixels. constructors Insets (int top, int left, int bottom, int right) Dimension(int width, int height)

Chapter 1433 Classes Dimension and Inset, cont. examples aButton.setMargin(new Insets (10, 20, 10, 20)); aLabel.setPreferredSize (new Dimension (20, 50));

Chapter 1434 The JScrollPane Class for Scroll Bars When you create a text area, you specify the number of lines that are visible and the number of characters per line. –example JTextArea the Text = new JTextArea(10,40;) It might be better not to limit the number of lines and the number of characters per line.

Chapter 1435 The JScrollPane Class for Scroll Bars, cont. This can be accommodated using scroll bars along the sides of the “window” or view port that shows only a selected portion of the text. The view port functions as a “cut out” over an unbounded document.

Chapter 1436 The JScrollPane Class for Scroll Bars, cont.

Chapter 1437 The JScrollPane Class for Scroll Bars, cont. Scroll bars can be provided using class JScrollPane. An object of class JScrollPane is essentially a view port with scroll bars.

Chapter 1438 The JScrollPane Class for Scroll Bars, cont. The text area is provided as an argument to the JScrollPane constructor. –example JScrollPane scrolledText = new JScrollPane(theText); A JScrollPane can be added to a container such as a JPanel or a JFrame. –example textPanel.add(scrolledText);

Chapter 1439 The JScrollPane Class for Scroll Bars, cont. class ScrollBarDemo

Chapter 1440 The JScrollPane Class for Scroll Bars, cont.

Chapter 1441 Scroll Bar Policy If you omit the invocation of the methods setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy, the scroll bars will be visible only when you need them.

Chapter 1442 Some Methods and Constants in Class JScrollBar to create a new JScrollPane for the objectToBeScrolled public JScrollPane(Component objectToBeScrolled)

Chapter 1443 Some Methods and Constants in Class JScrollBar, cont. To set the policy for showing the horizontal scroll bar public void setHorizontalScrollBarPolicy(int policy) where policy is one of JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS JScrollPane.HORIZONTAL_SCROLLBAR_NEVER JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED (default)

Chapter 1444 Some Methods and Constants in Class JScrollBar, cont. To set the policy for showing the vertical scroll bar public void setVerticalScrollBarPolicy(int policy) where policy is one of JScrollPane.VERTICAL_SCROLLBAR_ALWAYS JScrollPane.VERTICAL_SCROLLBAR_NEVER JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED (default)

Chapter 1445 Adding Borders A border is an area around a component that frames the component. You can add a border to any JComponent. A border can serve two purposes: –to make a component more attractive –to separate the component from other components

Chapter 1446 Adding Borders, cont. to use the border classes import javax.swing.border.* to provide a border JComponent.setBorder(Border_Object);

Chapter 1447 Adding Borders, cont. class BorderDemo

Chapter 1448 Adding Borders, cont.

Chapter 1449 Adding Borders, cont. You can place a border around any JComponent such as a JButton, a JLabel, a JPanel, or a JTextField. It is common to use an anonymous border object. –example testButton.setBorder(new BevelBorder(BevelBorder.LOWERED));

Chapter 1450 Some Border Classes to create a BevelBorder object public BevelBorder(int bevelType) where bevelType is one of BevelBorder.RAISED BevelBorder.LOWERED

Chapter 1451 Some Border Classes, cont. to create an EtchedBorder object public EtchedBorder(int etchType, Color highlight, Color shadow) where etchType is one of EtchedBorder.RAISED EtchedBorder.LOWERED to create an EtchedBorder object public EtchedBorder(Color highlight, Color shadow)

Chapter 1452 Some Border Classes, cont. to create an EmptyBorder object public EmptyBorder(int top, int left, int bottom, int right) to create a LineBorder object public LineBorder(Color theColor, int thickness) to create LineBorder object with rounded corners public LineBorder(Color theColor, int thickness, boolean roundedCorners)

Chapter 1453 Some Border Classes, cont. to create a MatteBorder object public Matte Border(int top, int left, int bottom, int right, Color theColor) to create a MatteBorder object with an icon public Matte Border(int top, int left, int bottom, int right, ImageIcon theIcon)

Chapter 1454 More Layout Managers: Outline The BoxLayout Manager Class Struts and Glue Setting the Spacing Between Components The Box Container Class The CardLayout Manager Class

Chapter 1455 The BoxLayout Manager Class The FlowLayout manager can produce a horizontal array, and the GridLayout manager with a single column can produce a vertical array. However, the BoxLayout manager and the Box container class are more powerful. A Box container is a panel-like class that uses the BoxLayout manager.

Chapter 1456 The BoxLayout Manager Class, cont.

Chapter 1457 The BoxLayout Manager Class, cont. We will consider two different programs that produce this GUI.

Chapter 1458 The BoxLayout Manager Class, cont. class BoxLayoutDemo

Chapter 1459 The BoxLayout Manager Class, cont. The constructor for the BoxLayout manager expects two arguments. –The first argument is the container for which it is the layout manager. –The second argument is one of two constants BoxLayout.X_AXIS BoxLayout.Y_AXIS

Chapter 1460 Struts and Glue Static methods in class Box produce invisible components that can be added to a container. These invisible components add space between visible components. Method createHorizontalStrut creates a strut which is an invisible component with a fixed horizontal size.

Chapter 1461 Struts and Glue, cont. A layout manager cannot change the vertical size of a horizontal strut. Method VerticalStrut creates a strut which is an invisible component with a fixed vertical size. A layout manager cannot change the horizontal size of a vertical strut.

Chapter 1462 Struts and Glue, cont. Like struts, glue components are invisible components. Unlike struts, glue components are not rigid. They are like wet glue and can be made larger or smaller by the layout manager. Struts and glue are best used with a BoxLayout manager.

Chapter 1463 Struts and Glue, cont. Glue components can be horizontal or vertical. to create glue components Component horizontalGlue = Box.createHorizontalGlue(); Component verticalGlue = Box.createVerticalGlue(); to add a glue component, use horizontalBox.add(horizontalGlue);

Chapter 1464 Setting the Spacing Between Components Except for the BoxLayout manager, the layout managers we have discussed use the following methods: public void setHgap(int hgap) public void setVgap(int vgap) where hgap and vgap are expressed in pixels. Alternatively, you can separate components using an EmptyBorder with any layout manager.

Chapter 1465 The Box Container Class An object of class Box behaves like a panel that has a BoxLayout manager. Instead of the JPanel s used in class BoxLayoutDemo, class BoxClassDemo uses Box containers.

Chapter 1466 The Box Container Class Instead of the JPanel s used in class BoxLayoutDemo

Chapter 1467 The Box Container Class class BoxClassDemo uses Box containers.

Chapter 1468 The Box Container Class, cont. Objects of class Box are created using a static method. to create the horizontalBox object Box horizontalBox = Box.createHorizontalBox(); to create the verticalBox object Box verticalBox = Box.createVerticalBox();

Chapter 1469 The Box Container Class, cont. Method setLayout is not used to determine the layout of a Box object. An object of class Box automatically uses the BoxLayout manager.

Chapter 1470 The Box Container Class, cont. equivalent statements Box horizontalBox = new Box (BoxLayout.X_AXIS); and Box horizontalBox = Box.createHorizontalBox();

Chapter 1471 The Box Container Class, cont. more equivalent statements Box verticalBox = new Box (BoxLayout.Y_AXIS); and Box verticalBox = Box.createVerticalBox();

Chapter 1472 The CardLayout Manager Class The CardLayout manager class can add a dynamic element to a Swing GUI. The CardLayout manager class provides a set of views you can change - somewhat like flipping through or selecting from a deck of cards. Any number of views can be added to a container, but only one at a time is viewable.

Chapter 1473 The CardLayout Manager Class, cont. Views can be selected in order or randomly.

Chapter 1474 The CardLayout Manager Class, cont. class CardLayoutDemo

Chapter 1475 The CardLayout Manager Class, cont.

Chapter 1476 The CardLayout Manager Class, cont. We did not use an anonymous variable deckPanel.setLayout(new CardLayout()); Instead we used dealer = new CardLayout(); deckPanel.setLayout(dealer); to permit us to change the displayed “card” and to permit us to refer to the CardLayout manager in more than one method.

Chapter 1477 The CardLayout Manager Class, cont. The first argument of method add names the component provided as the second argument. example deckPanel.add(“start”, startCardPanel); … deckPanel.add(“green”, greenCardPanel); … deckPanel.add(“red”, redCardPanel);

Chapter 1478 The CardLayout Manager Class, cont. Two other methods, first and next, permit you to select a view. examples dealer.first(deckPanel); dealer.next(deckPanel); The container always starts with the first component on view. After the last component, next goes back to the first component.

Chapter 1479 Some Methods in the CardLayout Manager Class to display the first “card” in the container public void first (Container theContainer); to display the last “card” in the container public void last (Container theContainer); to display the next card public void next (Container theContainer);

Chapter 1480 Some Methods in the CardLayout Manager Class, cont. to display the previous “card” public void previous(Container theContainer); to display the “card” that was added with cardName as its name public void show (Container theContainer, String cardName);

Chapter 1481 Inner Classes An inner class is a class defined within another class. Often, inner classes are used as helping classes when programming with Swing. Typically, helping classes are declared private.

Chapter 1482 Helping Classes Swing windows typically use class WindowDestroyer to close a window. Class WindowDestroyer can be an inner class.

Chapter 1483 Helping Classes, cont. class InnerClassDemo

Chapter 1484 Helping Classes, cont.

Chapter 1485 Advantages of Inner Classes Because inner classes are defined within an outer class, they can make the outer class self-contained (or more self-contained). The methods in the inner class have access to all the instance variables and methods of the outer class, including the private methods and variables. This increases efficiency.

Chapter 1486 Advantages of Inner Classes, cont. Inner classes are used frequently as listeners to handle events fired by the outer class or by a component of the outer class. The name of the inner class is local to the class in which it is defined, making it possible to have another class with the same name defined outside the class in which it is defined.

Chapter 1487 Invoking Methods of the Outer Class When there is a method invocation in an inner class, but a method with that name exists only in the outer class, the method of the outer class is invoked (i.e. the calling object is the this of the outer class, not the this of the inner class).

Chapter 1488 More on Events and Listeners: Outline The WindowListener Interface More Details on Updating a GUI

Chapter 1489 The WindowListener Interface When we placed buttons on a window, we made the window itself the button-listener class. But, when we wanted a window listener to respond to window-closing events, we made a separate window-listener class named WindowDestroyer (or an inner class named InnerDestroyer ).

Chapter 1490 The WindowListener Interface, cont. We can make the window itself the window listener. The WindowListener interface makes the window itself the listener just as the ActionListener interface makes a window a button listener.

Chapter 1491 The WindowListener Interface, cont. To make class ButtonDemo (pages 37-38) an action listener and a window listener, it would begin public class ButtonDemo extends JFrame implements ActionListener, Window Listener Unfortunately, interface WindowListener requires 7 methods to be implemented.

Chapter 1492 The WindowListener Interface, cont. These 7 methods are the first of 10 methods in class WindowAdapter. Nevertheless, window class which is derived from class JFrame and which implements the WindowListener interface makes it easy to call a method in the window class within the window listener class, since they are in the same class.

Chapter 1493 Methods in the WindowListener Interface

Chapter 1494 A Window Listener class WindowListenerDemo

Chapter 1495 A Window Listener, cont.

Chapter 1496 Method dispose Because the GUI class is its own window listener this.dispose(); is allowed in method windowClosing. Method dispose is a method in class JFame, and class WindowListenerDemo is derived from class JFrame (so it inherits method dispose ).

Chapter 1497 Method dispose, cont. Method dispose releases any resources used by the window. The program does not end when method dispose is invoked.

Chapter 1498 WindowListener vs. WindowAdapter WindowAdapter is a convenient variant of WindowListener. WindowAdapter implements interface WindowListener by giving every method an empty body. Any class derived from WindowAdapter does not need to provide those empty definitions.

Chapter 1499 WindowListener vs. WindowAdapter, cont. But sometimes, you want a listener class to be derived from class JFrame, which prevents it from being derived from class WindowAdapter. Instead it can implement interface WindowListener.

Chapter Java Tip: Programming the Close-Window Button To program the close-window button of a JFrame to do something other than cause the window to go away or end the program, add setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSING); to the constructor.

Chapter Java Tip: Programming the Close-Window Button, cont. class CloseWindowDemo

Chapter Java Tip: Programming the Close-Window Button, cont.

Chapter Java Tip: Programming the Close-Window Button, cont. If you invoke setDefaultCloseOperation, by default the window disappears but the program does not end. Simply reprogramming method windowClosing does not cancel the default action.

Chapter Java Tip: Programming the Close-Window Button, cont. To prevent the window from going away, you must first reset the default action with method setDefaultCloseOperation. Further, the close-window button is reprogrammed by registering the inner class InnerDestroyer as the window listener.

Chapter Java Tip: Programming the Close-Window Button, cont. Class ConfirmWindow is an inner class of class CloseWindowDemo, making class CloseWindowDemo self-contained.

Chapter Java Tip: More About setDefaultCloseOperation constants you can use with method setDefaultCloseOperation –to permit any desired action to be programmed in method windowClosing WindowConstants.DO_NOTHING_ON_CLOSE

Chapter Java Tip: More About setDefaultCloseOperation cont. –to hide the frame after invoking any registered WindowListener objects WindowConstants.HIDE_ON_CLOSE (the default action of setDefaultCloseOperation is not invoked)

Chapter Java Tip: More About setDefaultCloseOperation, cont. –to hide and dispose of the frame after invoking any registered WindowListener objects WindowConstants.DISPOSE_ON_CLOSE –to exit the application using method System.exit JFrame.EXIT_ON_CLOSE

Chapter Java Tip: More About setDefaultCloseOperation, cont. to program most Swing GUIs to have no window listener and still get the correct action for the close-window button setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Chapter Programmng Example: Components with Changing Visibility class VisibilityDemo

Chapter Programmng Example: Components with Changing Visibility

Chapter Method validate Every container class has a method validate for updating the container. Method validate causes the container to lay out its components again and redisplay them. Some changes, such as adding components or changing visibility require an invocation of method validate.

Chapter More Details on Updating GUIs Most changes to a GUI windowing system are done automatically by the repaint manager. However, a change in the visibility of a component requires an invocation of method validate, as we have discussed. Methods pack and repaint are two other updating methods common in Swing code.

Chapter More Details on Updating GUIs, cont. Method pack causes the window to be resized to an approximation of the “preferred size.” Method repaint repaints the window.

Chapter The Swing Class Hierarchy Reconsidered: Outline Buttons, Menus, and Abstract Buttons Other Swing Classes and Methods

Chapter Buttons, Menus, and Abstract Buttons Class JButton and class JMenuItem both are derived from class AbstractButton, from which all of the basic properties and methods (other than constructors) of class JButton and class JMenuItem are inherited. Class AbstractButton inherits some of these methods from class JComponent.

Chapter Buttons, Menus, and Abstract Buttons

Chapter Buttons, Menus, and Abstract Buttons Because class JMenu is derived from class JMenuItem, a menu is also a menu item, permitting a JMenu to be added to another JMenu to make nest menus possible. Because class JMenuBar is derived from class JComponent, a JMenuBar can be added to a container. –With a suitable layout manager, you can have several JMenuBar s, placed almost anywhere.

Chapter Java Tip: More Constructors for Class JMenuItem to create a menu item with no text or icon (assuming setText or setIcon will be used later) public JMenuItem() to create a menu item with the text on it public JMenuItem(String text) to create a menu item with the icon picture on it public JMenuItem(ImageIcon picture)

Chapter Java Tip: More Constructors for Class JMenuItem, cont. to create a menu item with both the text and the icon picture on it public JMenuItem (String text, ImageIcon picture)

Chapter Other Swing Classes and Methods In Chapter 12 and in this chapter, you have learned a lot about Swing. However, a typically book on Swing is longer than the book for this course, and still doesn’t cover all of Swing. Most of the classes, methods, and facilities that should be in Swing probably are in Swing already.

Chapter Summary You have learned to add menus, icons, borders, and scroll bars to GUIs. You have learned to use the BoxLayout manager and the Box class. You have learned about inner classes. You have learned about the WindowListener interface. You have learned how to change GUI components to visible or invisible.