Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

27
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components

description

Use the ButtonGroup classes Create a drop-down list and combo box using the JComboBox class Create JScrollPanes Create JToolBars

Transcript of Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Page 1: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Java Programming, Second Edition

Chapter ThirteenUnderstanding Swing

Components

Page 2: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

In this chapter, you will:• Use the JFrame class• Use additional JFrame class

methods• Use Swing event listeners• Use JPanel class methods• Use the JCheckBox class

Page 3: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Use the ButtonGroup classesCreate a drop-down list and combo box using the JComboBox classCreate JScrollPanesCreate JToolBars

Page 4: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using the JFrame ClassGUI Components

Insert the import statement import javax.swing.*; to take advantage of the Swing GUI components and their methodsAlso called widgets, which stands for windows gadgetsWithin the awt package, components are defined in the Component classWhen you use components in a Java program, you usually place them in containers

Page 5: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using the JFrame ClassContainer- Type of component that holds other components so that you can treat a group of several components as a single entity

Usually takes the form of a windowDefined in the Container class

Page 6: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using the JFrameThe JFrame class is a subclass of the awt Component classThe JFrame is the best container option for hosting Java applicationsThe setSize() method allows you to set the physical size of a JFrameThe setVisible() method makes the JFrame component visible or invisibleCreate a JFrame so that you can place other objects within it for display using a JPanel

Page 7: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using the JFrameThe JFrame class has four constructors

JFrame() constructs a new frame that is initially invisibleJFrame(GraphicsConfiguration gc) creates a JFrame in the specified GraphicsConfiguration of a screen device and a blank titleJFrame(String title) creates a new, initially invisible JFrame with the specified titleJFrame(String title, GraphicsConfiguration gc) creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen

Page 8: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using Additional JFrame Class Methods

When you extend the JFrame class, you inherit several useful methods

The syntax to use any of these methods is to use a JFrame object, a dot, and the method name

A JFrame’s action in response to a user clicking the Close button is set by passing an argument to the setDefaultCloseOperation() method placed inside the JFrame constructor method

The most common action is to close the application using the argument JFrame.EXIT_ON_CLOSE

Page 9: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using Additional JFrame Class Methods

EXIT_ON_CLOSE exists the program when the JFrame is closedDISPOSE_ON_CLOSE closes the frame, disposes of the JFrame object, and keeps running the applicationDO_NOTHING_ON_CLOSE keeps the JFrame and continues runningHIDE_ON_CLOSE closes the JFrame and continues running

Page 10: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Page 11: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using Swing Event Listeners

Classes that respond to user events must implement an interface that deals with the eventsThese interfaces are called event listeners

Each listener can handle a specific event typeA class can implement as many event listeners as needed

Page 12: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Page 13: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Page 14: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using Swing Event Listeners

When a user event takes place, the appropriate method is automatically called by the system

Page 15: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using JPanel Class MethodsComponents are added to a container’s content pane using the following steps:

Create a JPanel objectAdd components to the JPanel using the add() method and using the component as the argumentCall the setContentPane() method with the panel object created to set the application's content pane

Page 16: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using JPanel Class Methods

Swing components have a default size for the objects createdA component’s size can be changed using the component’s setPreferredSize() method

Page 17: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using the JCheckBox ClassJCheckBox Class- Consists of a JLabel positioned beside a squareYou can click the square to display or remove a check mark

Page 18: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Page 19: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Using the ButtonGroup Classes

ButtonGroup ClassesCan group several JCheckBoxes so a user can select only one at a timeWhen you group JCheckBox objects, all other JCheckBoxes are automatically turned off when the user selects any one checkboxEither create a ButtonGroup and then create the individual JCheckBoxes, or you can create the JCheckBoxes and then create the Button groupSimilar to a set of radio buttons but more than one can be checked

Page 20: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Creating a Drop-Down List and Combo Box Using the

JComboBox ClassJComboBox class- For picking items from a list or entering text into a fieldAnother option is a drop-down list, also called a choice listA drop-down list can also be configured to be a combo box

Page 21: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Creating a Drop-Down List and Combo Box Using the

JComboBox ClassYou can build a JComboBox by using a constructor with no arguments and then adding items to the list with the addItem() methodThe following statements create a JComboBox with three options

JComboBox majorChoice = newJComboBox();majorChoice.addItem(“English”);majorChoice.addItem(“Math”);majorChoice.addItem(“Sociology”);

Page 22: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Page 23: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Creating JScrollPanesA JScrollPane is a container whose methods can be used to hold any component that can be scrolledAdd a JTextArea component to use both multiple rows and columns

Page 24: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

JScrollPane formsThe JScrollPane constructor takes one of four forms:

JScrollPane() creates an empty JScrollPane where both horizontal and vertical scrollbars appear when neededJScrollPane(Component) creates a JScrollPane that displays the contents of the specified componentJScrollPane(Component, int, int) creates a JScrollPane that displays the specified component, vertical scrollbar, and horizontal scrollbar.JScrollPane(int, int) creates a scroll pane with specified vertical and horizontal scrollbars

Page 25: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Horizontal and vertical scrollbar constants

HORIZONTAL_SCROLLBAR_AS_NEEDEDHORIZONTAL_SCROLLBAR_ALWAYSHORIZONTAL_SCROLLBAR_NEVERVERTICAL_SCROLLBAR_AS_NEEDEDVERTICAL_SCROLLBAR_ALWAYSVERTICAL_SCROLLBAR_NEVER

Page 26: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Creating JToolBarsA Swing GUI can be designed so that a user can move a toolbar from one section of a graphical user interface to another section

This type of toolbar is called dockable toolbarThe process that allows you to move and then attach the toolbar is called dockingA toolbar is created in Swing with the JToolBar class

Page 27: Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Creating JToolBarsConstructor methods for the JToolBar class include the following:

JToolBar() creates a new toolbar that will line up components in a horizontal directionJToolBar(int) creates a new toolbar with a specified orientation of horizontal or vertical