Chapter 14 – Using Layout Managers and Events Dr. James Burns –

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

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.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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 An introduction.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CSE 219 Computer Science III Graphical User Interface.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Java's Graphical User Interface Toolkit
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
Chapter 15: Advanced GUI Topics. Objectives Use content panes Use color Learn more about layout managers Use JPanel s to increase layout options Create.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Object-Oriented Software Engineering
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Computer Science 209 GUIs Model/View/Controller Layouts.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
GUIs & Event-Driven Programming Chapter 11 Review.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces -- GUIs
Swing JComponents.
INFSY 547: WEB-Based Technologies
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Creating Graphical User Interfaces
Chapter 7-3 (Book Chapter 14)
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
Chapter 14 – Using Layout Managers and Events
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Chapter 14 – Using Layout Managers and Events Dr. James Burns –

Layout Manager A layout manager is an object that controls the size and position of components inside a Container object The layout manager that you assign to the window determines how the components are sized and positioned within the window Layout managers are interface classes that are part of the Java SDK They align your components Then the components do not crowd each other or overlap

Learning about Layout Managers If you are placing more than one component on a JFrame, JApplet or any other container, you spend a lot of time determining exactly where to place each component so the layout is attractive and no component obscures another The alternative is to use a layout manager

Examples… One layout manager arranges components in equally spaced columns and rows Another layout manager centers components within their Container Each layout manager defines methods that arrange components within a Container itself, so you can assign layout managers within layout managers

More on Layout Managers Layout managers can also be a Container itself, so you can assign layout managers within layout managers The Java platform provides very simple layout managers like FlowLayout and GridLayout to the special purpose BorderLayout and CardLayout to the very flexible GridBagLayout and BoxLayout

Table showing when each layout manager should be used Layout Manager When to Use BorderLayout Use when you add components to a maximum of five sections arranged in north, south, east, west, and center positions FlowLayout Use when you need to add components from left to right, Flowlayout automatically moves to the next row when needed, and each component takes its preferred size GridLayout Use when you need to add components into a grid of rows and columns; each component is the same size

Table, Continued Layout ManagerWhen to Use CardLayout Use when you need to add components that are displayed one at a time BoxLayout Use when you need to add components into a single row or a single column GridBagLayout Use when you need to set size, placement, and alignment constraints for every component that you add

Using Border Layout The BorderLayout manager is the default manager class for all content panes You can use the BorderLayout class with any container that has five or fewer components The components will fill the screen in five regions—north, south, east, west, and center

Using add() to add components When you add a component to a container that uses BorderLayout, the add() method uses two arguments—the component and the region to which the component is added The BorderLayout class provides five named constants for the regions— BorderLayout.NORTH,.SOUTH,.EAST,.WEST, and.CENTER

import javax.swing.*; import java.awt.*; public class JDemoBorderLayout extends JApplet { private JButton nb = new JButton("North Button"); private JButton sb = new JButton("South Button"); private JButton eb = new JButton("East Button"); private JButton wb = new JButton("West Button"); private JButton cb = new JButton("Center Button"); public void init() { setLayout(new BorderLayout()); add(nb, BorderLayout.NORTH); add(sb, BorderLayout.SOUTH); add(eb, BorderLayout.EAST); add(wb, BorderLayout.WEST); add(cb, BorderLayout.CENTER); }

Html doc to host the applet

Using FlowLayout You can use the FlowLayout manager class to arrange components in rows across the width of a Container With FlowLayout, each Component that you add is placed to the right of previously added components in a row, or if the current row is filled, the Component is placed to start a new row

Comparing BorderLayout… With BorderLayout, the Components are sized to fit the pane With FlowLayout, each Component retains its default size, or preferred size. If there is text in the Component, that text may not be visible when it gets resized with BorderLayout

FlowLayout class contains three constants you can use to align Components FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT If you do not specify alignment, Components are center-aligned in a FlowLayout Container by default

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JDemoFlowLayout extends JApplet implements ActionListener { private JButton lb = new JButton("L Button"); private JButton rb = new JButton("R Button"); private Container con = getContentPane(); private FlowLayout layout = new FlowLayout(); public void init() { con.setLayout(layout); con.add(lb); con.add(rb); lb.addActionListener(this); rb.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == lb) layout.setAlignment(FlowLayout.LEFT); else layout.setAlignment(FlowLayout.RIGHT); con.invalidate(); con.validate(); }

You can Dynamically reposition Components As illustrated in Figures 14-5, 6 and 7 Involving use of con.invalidate(); con.validate(); The invalidate() call marks the container as needing to be laid out The validate() call causes the components to be rearranged based on the newly assigned layout

Using GridLayout Use GridLayout if you want to arrange components into equal rows and columns When you create a GridLayout object, you indicate the numbers of rows and columns you want, and then the container surface is divided into a grid

For example,… The following statement establishes an anonymous GridLayout with four horizontal rows and five vertical columns in a Container named con Con.setLayout(new GridLayout(4, 5));

Similarly, within a JApplet, you can establish the same layout with either of the following: This.setLayout(new GridLayout(4, 5)); setLayout(new GridLayout(4, 5)); As you add new components to a Gridlayout, they are positioned left- to-right across each row in sequence

More on GridLayouts You cannot skip a position or specify an exact position for a component You can add a blank label to a grid position and give the illusion of skipping a position

Gaps… You can specify vertical and horizontal gaps measured in pixels, using two additional arguments. Private GridLayout layout = new GridLayout(3, 2, 5, 5);

import javax.swing.*; import java.awt.*; public class JDemoGridLayout extends JApplet { private JButton b1 = new JButton("Button 1"); private JButton b2 = new JButton("Button 2"); private JButton b3 = new JButton("Button 3"); private JButton b4 = new JButton("Button 4"); private JButton b5 = new JButton("Button 5"); private GridLayout layout = new GridLayout(3, 2, 5, 5); public void init() { setLayout(layout); add(b1); add(b2); add(b3); add(b4); add(b5); }

Using CardLayout You use CardLayout when you want multiple compnoents to share the same display space The CardLayout manager generates a stack of containers or components, one on top of another Each component is a ‘card’ and each ‘card’ can be any component type—ex., JButton, JLabel, or JPanel

CardLayout uses one of two constructors CardLayout() creates a card layout without a horizontal or vertical gap CarLayout(int hgap, int vgap) creates a card layout with the specified horizontal and vertical gaps Horizontal gaps are placed at the left and right edges Vertical gaps are placed at the bottom and top edges

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JDemoCardLayout extends JApplet implements ActionListener { private CardLayout cards = new CardLayout(); private JButton b1 = new JButton("Ace of Hearts"); private JButton b2 = new JButton("Three of Spades"); private JButton b3 = new JButton("Queen of Clubs"); public void init() { setLayout(cards); add("ace", b1); b1.addActionListener(this); add("three", b2); b2.addActionListener(this); add("queen", b3); b3.addActionListener(this); } public void actionPerformed(ActionEvent e) { cards.next(getContentPane()); }

Using Advanced Layout Managers Professioinal Java programmers create new components and new layout managers Examples include… GridBagLayout manager—allows you to add Components to precise locations within the grid, as well as indicate that specific Components should span multiple rows or columns within the grid If you want to create a JPanel with six JButtons, in which two are twice as wide as the others, use GridBagLayout

Using JPanels to Increase Layout Options You can use the JPanel class to greatly increase the number of possible component arrangements JPanel is similar to a JWindow or a JFrame, in that a JPanel is a surface on which you can place components But JPanel is not a child of JWindow or JFrame

JPanel is a type of JComponent…. JPanel is a Container, meaning it can contain other components For example, you can create a JApplet using BorderLayout and place a JPanel in any of the five regions Then within the north JPanel, you can place four JButtons using GRidLayout and within the east JPanel, you can place three JLabels using FlowLayout.

JPanels…. By using JPanels within JPanels, you can create an infinite variety of screen layouts JPanels support double buffering, which is the default buffering strategy resulting in an updated screen only when it is complete so that no flickering takes place while the JPanel is being redrawn

JPanel Constructors… JPanel() creates a JPanel with double buffering and a flow layout JPanel(LayoutManager layout) creates a JPanel with the specified layout manager and double buffering JPanel(Boolean isDoubleBuffered) creates a JPanel with flow layout and the specified double-buffering strategy JPanel(LayoutManager layout, Boolean isDoubleBuffered) creates a JPanel with the specified layout manager and the specified doublebuffering strategy

Examples… The following statements create a JPanel that uses a named BorderLayout manager BorderLayout border = new BorderLayout(); JPanel myPanel = new JPanel(border);

The following statement accomplishes the same thing as the previous two, combining bothstatements by using an anonymous layout manager: JPanel myPanel = new JPANEL(new BorderLayout());

import javax.swing.*; import java.awt.*; public class JDemoManyPanels extends JApplet { // Twelve buttons JButton button01 = new JButton("One"); JButton button02 = new JButton("Two"); JButton button03 = new JButton("Three"); JButton button04 = new JButton("Four"); JButton button05 = new JButton("Five"); JButton button06 = new JButton("Six"); JButton button07 = new JButton("Seven"); JButton button08 = new JButton("Eight"); JButton button09 = new JButton("Nine"); JButton button10 = new JButton("Ten"); JButton button11 = new JButton("Eleven"); JButton button12 = new JButton("Twelve"); // Four panels JPanel panel01 = new JPanel(new GridLayout(2, 0)); JPanel panel02 = new JPanel(new FlowLayout()); JPanel panel03 = new JPanel(new FlowLayout()); JPanel panel04 = new JPanel(new GridLayout(2, 0));

public void init() { setLayout(new BorderLayout()); add(panel01, BorderLayout.WEST); add(panel02, BorderLayout.CENTER); add(panel03, BorderLayout.SOUTH); add(panel04, BorderLayout.EAST); panel01.add(button01); panel01.add(button02); panel01.add(button03); panel02.add(button04); panel02.add(button05); panel02.add(button06); panel03.add(button07); panel04.add(button08); panel04.add(button09); panel04.add(button10); panel04.add(button11); panel04.add(button12); }

Checker Board Panel The following program creates a checkerboard panel, as shown in figure 14-16

import java.awt.*; import javax.swing.*; public class Checkerboard extends JFrame { final int ROWS = 8; final int COLS = 8; final int GAP = 2; final int NUM = ROWS * COLS; int x; JPanel pane = new JPanel(new GridLayout(ROWS, COLS, GAP, GAP)); JPanel[] panel = new JPanel[NUM]; Color color1 = Color.WHITE; Color color2 = Color.BLUE; Color tempColor; public Checkerboard() { super("Checkerboard"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(pane); for(x = 0; x < NUM; ++x) { panel[x] = new JPanel(); pane.add(panel[x]);

if(x % COLS == 0) { tempColor = color1; color1 = color2; color2 = tempColor; } if(x % 2 == 0) panel[x].setBackground(color1); else panel[x].setBackground(color2); } public static void main(String[] arguments) { Checkerboard frame = new Checkerboard(); final int SIZE = 300; frame.setSize(SIZE, SIZE); frame.setVisible(true); }

Understanding Events and Event Handling Now that you understand inheritance and abstract classes, you can take a deeper look at event handling Like all Java classes, events are Objects. Specifically, events are objects that the user initiates, such as key presses or mouse clicks

EventObject… EventObject is the parent class for all event objects, which descends from the Object class EventObject is the parent of AWTEvent, which in turn is the parent of specific event classes, such as ActionEvent and ComponentEvent

ComponentEvent… ComponentEvent is a child of EventObject But is also a parent of KeyEvent and MouseEvent

Rules… ActionEvents are generated by components that users can click, such as JButtons and JCheckBoxes TextEvents are generated by components into which the user enters text, such as a JTextField MouseEvents include determining the location of the mouse and distinguishing between a single- and double-click.

User ActionResulting Event type Click a ButtonActionEvent Click a componentMouseEvent Click an item in a list box ItemEvent Click an item in a check box ItemEvent Change text in a text field TextEvent Open a windowWindowEvent Iconify a windowWindowEvent Press a keyKeyEvent

ActionEvents and MouseEvents Because ActionEvents and MouseEvents involve the mouse, it is easy to confuse ActionEvents and MouseEvents Use ActionEvents when a mouse click changes a component, like a JButton Use MouseEvents when your program is focusing on what the user is doing manually with the mouse, such as clicking the left mouse button

For events in which the program has no interest… No listeners have to be set up for such events For events that you care about, your must set up a listener Every Listener interface method has the return type void and each takes one argument—the object thatis an instanceof the corresponding event class.

ActionListener… Is an interface that has a method named actionPerformed() Interface methods such as actionPerformed(), which are called automatically when an appropriate event occurs, are called event handlers

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JDemoKeyFrame extends JFrame implements KeyListener { JLabel prompt = new JLabel("Type keys below:"); JLabel outputLabel = new JLabel("Key Typed:"); JTextField textField = new JTextField(10); public JDemoKeyFrame() { setTitle("JKey Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; setLayout(new BorderLayout()); add(prompt, BorderLayout.NORTH); add(textField, BorderLayout.CENTER); add(outputLabel, BorderLayout.SOUTH); addKeyListener(this); textField.addKeyListener(this); }

public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); outputLabel.setText("Last key typed: " + c); } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public static void main(String[] args) { JDemoKeyFrame keyFrame = new JDemoKeyFrame(); final int WIDTH = 250; final int HEIGHT = 100; keyFrame.setSize(WIDTH, HEIGHT); keyFrame.setVisible(true); }

An Event-Handling Example: KeyListener You use the KeyListener interface when you are interested in actions the user initiates from the keyboard The KeyListener interface contains three methods keyPressed() keyTyped() keyReleased()

Using AWTEvent Class Methods Methods in these classes will tell you which component was involved in an event You use the getComponent() method to find the specific component involved

Handling Mouse Events Users spend most of their time operating a mouse rather than keying on a keyboard The MouseMotionListener interface provides you with methods named mouseDragged() and mouseMoved() that detect the mouse being rolled or dragged across a component surface

MouseListener interface Provides methods, such as mousePressed() mouseClicked() mouseReleased() mouseEntered() mouseExited() The last two methods tell you when a component has a mouse pointer on it or extied from it

MouseInputListener Interface Implements all the methods in both the MouseListener and MouseMotionListener interfaces, but has no methods of its own

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JMouseActionsFrame extends JFrame implements MouseListener { final int MAX = 20; final int STARTX = 10; final int STARTY = 20; int x, y; String message[] = new String[MAX]; int msgCount = 0; public JMouseActionsFrame() { setTitle("Mouse Actions"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; addMouseListener(this); }

public void mouseClicked(MouseEvent e) { int whichButton = e.getButton(); String msg; if(msgCount == MAX) clearScreen(); message[msgCount] = "You pressed the mouse."; if (whichButton == MouseEvent.BUTTON1) msg = "button 1."; else if(whichButton == MouseEvent.BUTTON2) msg = "button 2."; else msg = "button 3.";

message[msgCount] = message[msgCount] + " You used " + msg; message[msgCount] = message[msgCount] + " You are at position " + e.getX() + ", " + e.getY() + "."; if (e.getClickCount() == 2) message[msgCount] = message[msgCount] + " You double-clicked"; else message[msgCount] = message[msgCount] + " You single-clicked"; ++msgCount; repaint(); }

public void mouseEntered(MouseEvent e) { if(msgCount == MAX) clearScreen(); message[msgCount] = "You entered the frame"; ++msgCount; repaint(); } public void mouseExited(MouseEvent e) { if(msgCount == MAX) clearScreen(); message[msgCount] = "You exited the frame"; ++msgCount; repaint(); }

public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void paint(Graphics g) { super.paint(g); x = STARTX; y = STARTY; for(int a = 0; a < msgCount; ++a) g.drawString(message[a], x, y += 20); }

public void clearScreen() { msgCount = 0; for(int a = 0; a < MAX; ++a) message[a] = " "; repaint(); } public static void main(String[] args) { JMouseActionsFrame mFrame = new JMouseActionsFrame(); final int WIDTH = 750; final int HEIGHT = 500; mFrame.setSize(WIDTH, HEIGHT); mFrame.setVisible(true); }