introduction to swing components chapter 14. part of the java foundation classes (jfc) provides a...

29
Java Programming Introduction to Swing Components Chapter 14

Upload: maximilian-baldwin

Post on 01-Jan-2016

256 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Java ProgrammingIntroduction to Swing Components

Chapter 14

Page 2: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

What is Java Swing?

Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program with a

graphical user interface (GUI) table controls, list controls, tree controls,

buttons, and labels, and so on…

Page 3: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Java Foundation Classes

In April 1997, JavaSoft announced the Java Foundation Classes (JFC). a major part of the JFC is a new set of user

interface components called Swing.

AWT Swing AccessibilityJava 2D

DragAndDrop

Page 4: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

GUI Packages

AWT java.awt java.awt.color java.awt.datatransfer java.awt.event java.awt.font java.awt.geom java.awt.image

Swing javax.accessibility javax.swing javax.swing.colorchooser javax.swing.event javax.swing.filechooser javax.swing.plaf javax.swing.table javax.swing.text.html javax.swing.tree

Page 5: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Components

A GUI consists of different graphic Component objects which are combined into a hierarchy using Container objects.

Component class An abstract class for GUI components such as menus,

buttons, labels, lists, etc. Container

An abstract class that extends Component. Containers can hold multiple components.

Page 6: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Components

Container Type of component that holds other components Can treat group as single entity Defined in Container class Often takes form of window

Drag Resize Minimize Restore Close

Page 7: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Swing Features

Swing provides: A wide variety of components (tables, trees, sliders,

progress bars, internal frame, …) Swing components can have tooltips placed over them Arbitrary keyboard events can be bound to components Additional debugging support. Support for parsing and displaying HTML based

information

Page 8: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

JFrame window = new JFrame(" title " );

JFrames

A JFrame is a Window with all of the adornments added JFrame inherits from Frame, Window, Container, Component, and Object

A JFrame provides the basic building block for screen-oriented applications

Page 9: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Creating a JFrame

import javax.swing.*;

public class SwingFrame { public static void main( String args[] ) {

JFrame window = new JFrame( "My First GUI Program" );

window.setVisible(true); }}

Page 10: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

JFrames

Set size and titlewindow.setSize(200, 100);window.setTitle("My frame");

Close JFrame Click Close button JFrame becomes hidden and application keeps running

Default behavior To change this behavior

Use setDefaultCloseOperation() methodwindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Page 11: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Creating a JFrame

import javax.swing.*;import java.awt.*;

public class SwingFrame {

public static void main( String args[] ) {JFrame window = new JFrame( "My First GUI Program" );window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

window.setSize( 250, 150 );window.setVisible(true);

}}

Page 12: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

JFrame

JFrames have several panes:

Components are placed in the Content Pane

Glass pane

Layered pane

Menu bar

Content pane

Page 13: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Swing Components

JComponent JComboBox, JLabel, JList, JMenuBar, JPanel, JPopupMenu, JScrollBar, JScrollPane, JTable, JTree, JInternalFrame, JOptionPane, JProgressBar, JRootPane, JSeparator, JSlider, JSplitPane, JTabbedPane, JToolBar, JToolTip, Jviewport, JColorChooser, JTextComponent, …

Page 14: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

label = new JLabel("text", JLabel.RIGHT );Font typeSet = new Font("Arial", Font.BOLD, 36 );

JLabels JLabels are components that you can fill with

text. When creating a label you can specify the initial

value and the alignment you wish to use within the label.

You can use getText() and setText() to get and change the value of the label.

You can use setFont() to change the font, style and size of your text

Page 15: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

JLabelsimport javax.swing.*;

public class SwingFrame { public static void main( String args[] ) {

JFrame window = new JFrame( "My First GUI Program" );JLabel label = new JLabel( "Hello World" );Font typeSet = new Font("Century", Font.BOLD, 28 );

label.setFont(typeSet);window.add( label );window.setSize(250, 100);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);window.setVisible(true);

}}

Page 16: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

JButtons

JButton extends Component , displays a string, and delivers an ActionEvent for each mouse click.

Normally buttons are displayed with a border

In addition to text, JButtons can also display icons

button = new JButton( ”text“ );

Page 17: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Buttonsimport javax.swing.*;

public class SwingFrame { public static void main( String args[] ) {

JFrame window = new JFrame( "My First GUI Program" );

JButton button = new JButton( "Click Me!!" );

window.add( button );window.setSize(250, 100);window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

win.setVisible(true); }}

Page 18: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Layout Manager

Layout Manager An interface that defines methods for

positioning and sizing objects within a container. Java defines several default implementations of LayoutManager.

Geometrical placement in a Container is controlled by a LayoutManager object

Page 19: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

19

Components, Containers, and Layout Managers

Containers may contain components (which means containers can contain containers!!).

All containers come equipped with a layout manager which positions and shapes (lays out) the container's components.

Much of the action in Swing occurs between components, containers, and their layout managers.

Page 20: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

20

Layout Managers Layouts allow you to format components on the

screen in a platform-independent way The standard JDK provides many classes that

implement the LayoutManager interface, including: FlowLayout GridLayout BorderLayout BoxLayout CardLayout OverlayLayout GridBagLayout

Page 21: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

21

Invoke the setLayout() method on the container to use the new layout.

setLayout( new FlowLayout() );

The layout manager should be established before any components are added to the container

JFrame will otherwise stack all components on top of each other

Changing the Layout

Page 22: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

22

FlowLayout When you add components to the screen, they

flow left to right (centered) based on the order added and the width of the screen.

Very similar to word wrap and full justification on a word processor.

If the screen is resized, the components' flow will change based on the new width and height

FlowLayout is the default layout for the JPanel class.

Page 23: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

import javax.swing.*;import java.awt.*;

public class LayOut { public static void main( String args[] ) { JFrame window = new JFrame( "Layout Program" ); JLabel title = new JLabel( "Layout Example" ); JLabel name = new JLabel( "Enter your name" ); JTextField textInput = new JTextField( 12 ); JButton button = new JButton( "Enter your name" ); window.setSize(200, 150); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); window.setLayout( new FlowLayout() ); window.add( title ); window.add( name ); window.add( textInput ); window.add( button ); }}

Page 24: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

24

Flow Layout

import javax.swing.*;import java.awt.*;

public class ShowFlowLayout { public static void main( String args[] ) {

JFrame win = new JFrame( "My First GUI Program" );win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

win.getContentPane().setLayout( new FlowLayout() );

for ( int i = 0; i < 10; i++ ) { win.getContentPane().add(

new JButton( String.valueOf( i ) ) ); }

win.setVisible(true); }}

Page 25: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

25

FlowLayout

You can resize with the mouse and the buttons will move to fill in the space

Page 26: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Event Driven Programming

Programs respond to events that are generated outside the control of the program

User types a key The left mouse button is pressed A CD is removed from the CD drive

When an event occurs, it is handled by an event handler

Event driven programming involves 1. writing the handlers, and 2. arranging for the handler to be notified when

certain events occur

Page 27: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Event Handling

An event is represented by an object that gives information about the event and identifies the event source Event sources are typically components, but other

kinds of objects can also be event sources A listener is an object that wants to be

notified when a particular event occurs An event source can have multiple listeners

registered on it A single listener can register with multiple event

sources

Page 28: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Listeners

In order for an object to be notified when a particular event occurs, the object

1. must implement the appropriate Listener interface

2. must be registered as an event listener on the appropriate event source

Page 29: Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java

Swing Listeners

Action Listener Type

User clicks a button, presses return while typing in a text field, or chooses a menu item

ActionListener

Users closes a frame (main window) WindowListener

User presses a mouse button while the cursor is over a component

MouseListener

User moves the mouse over a component MouseMotionListener

A component becomes visible ComponentListener

A component gets the keyboard focus FocusListener

A table of list selection changes ListSelectionListener