Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.

Slides:



Advertisements
Similar presentations
Chapter 14 Advanced GUI ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Advertisements

G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Introduction to Java 2 Programming
User Interfaces II GUI – Awt, Swing, Web
14 Copyright © 2005, Oracle. All rights reserved. User Interface Design: Swing Basics Planning the Application Layout.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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 program.
GridLayout, n*m grid. Column width/row height adjusted to fith the widest/highest Component.
CE203 - Application Programming Autumn 2013CE203 Part 11 Part 1.
Graphical User Interfaces
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
TCU CoSc Programming with Java Visual Design (Chapter 5)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter Day 17. © 2007 Pearson Addison-Wesley. All rights reserved6-2 Agenda Day 17 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 3.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
GUI and event-driven programming An introduction.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Manager Summary
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
3461 Laying Out Components Interior Design for GUIs.
Basics of GUI Programming Chapter 11 and Chapter 22.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
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.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter 7 A First Look at GUI Applications Layout Managers.
Chapter 9: Graphical User Interfaces
Swing JComponents.
Modern Programming Language Java
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
Layout Organization and Management
Graphical User Interface
Presentation transcript:

Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of the two interfaces: LayoutManger or LayoutManger2. There are several predefined layout managers defined in the Java standard class library: Defined in the AWT Defined in Swing Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Box Layout Overlay Layout Introduction to GUI Layout Managers

Unit 122 Every container has a default layout manager, but we can explicitly set the layout manager as well. Each layout manager has its own particular rules governing how the components will be arranged. Some layout managers pay attention to a component's preferred size or alignment, while others do not. A layout manager attempts to adjust the layout as components are added and as containers are resized. We can use the setLayout method of a container to change its layout manager. JPanel panel = new JPanel(); panel.setLayout (new BorderLayout()); Layout Managers

Unit 123 Introduction to Flow Layout FlowLayout places components sequentially on a row from left to right in the order added and then moves to the next row. Rows are created as needed to accommodate all of the components. Components placement depends on the current size of the container. When the container is resized the components are automatically resized. FlowLayout is the default layout for panels. Each row of components is centered horizontally in the window by default, but could also be aligned left or right The horizontal and vertical gaps between the components can be explicitly set also FlowLayout has three constructors: oFlowLayout() oFlowLayout(int align): The alignment argument can be FlowLayout.LEADING, FlowLayout.CENTER, or FlowLayout.TRAILING. oLEADING specifies that the components be left-aligned and TRAILING specifies right alignment. oFlowLayout(int align, int hgap, int vgap)

Unit 124 Example 1: Flow Layout Test 1 import javax.swing.*; 2 class TestFlowLayout extends JFrame{ 3 JPanel panel = new JPanel(); 4 public TestFlowLayout(){ Note: we did not set the layout because Flow Layout is the default one for the panel 5 panel.add(new JButton("1")); 6 panel.add(new JButton("2")); 7 panel.add(new JButton("3")); 8 panel.add(new JButton("4")); 9 panel.add(new JButton("5")); 10 panel.add(new JButton("6")); 11 panel.add(new JButton("7")); 12 setContentPane(panel); 13 setSize(300,300); 14 setTitle("Flow Layout Test"); 15 setVisible(true); } 17 public static void main(String [] args){ 18 new TestFlowLayout(); 19 } }

Unit 125 Introduction to Grid Layout A grid layout presents a containers components in a rectangular grid of rows and columns. One component is placed in each cell of the grid, and all cells have the same size. As components are added to the container, they fill the grid from left-to-right and top-to-bottom (by default). The size of each cell is determined by the overall size of the container. GridLayout has three constructors: oGridLayout() oGridLayout(int rows, int cols) oGridLayout(int rows, int cols, int hgap, int vgap)

Unit 126 Example 2: Grid Layout Test 1 import java.awt.*; import javax.swing.*; 2 import javax.swing.border.*; 3 class TestGridLayout extends TestFlowLayout{ 4 public TestGridLayout(){ 5 panel.add(new JButton("8")); 6 panel.add(new JButton("9")); 7 panel.add(new JButton("*")); 8 panel.add(new JButton("0")); 9 panel.add(new JButton("#")); 10 JLabel jlb = new JLabel(ICS 201", SwingConstants.CENTER); 11 Border b =BorderFactory.createBevelBorder(BevelBorder.RAISED); 12 jlb.setBorder(BorderFactory.createTitledBorder(b,Section 02")); 13 panel.add(jlb); 14 setTitle("Grid Layout Test"); 15 panel.setLayout(new GridLayout(0,3)); 16 setVisible(true); 17 } 18 public static void main(String [] args){ 19 new TestGridLayout(); 20 } 21 } GridLayout class to create an instance that has three columns and as many rows as necessary

Unit 127 Introduction to Border Layout BorderLayout places components according to five areas: "North", "South", "East", "West" and "Center". When you enlarge a container the center area grabs as much of the new space as possible. Each area displays one component (which could be another container such as a JPanel ) If nothing is added to the outer areas, they take up no space and other areas expand to fill the void BorderLayout is good for maintaining a row of buttons in one of the areas near the edges. BorderLayout has two constructors: oBorderLayout() oBorderLayout(int hgap, int vgap) North South CenterEastWest

Unit 128 Example 3: Border Layout Test 1 import java.awt.*; import javax.swing.*; 2 public class TestBorderLayout extends TestGridLayout{ 3 public TestBorderLayout() { 4 setTitle("Border Layout Test."); 5 JPanel jp1 = (JPanel)getContentPane(); 6 JPanel jp2 = new JPanel(); 7 jp2.setLayout(new BorderLayout()); 8 9 jp2.add(new JButton("NORTH"), "North"); 10 jp2.add(new JButton("WEST"), "West"); 11 jp2.add(new JButton("EAST"), "East"); 12 jp2.add(new JButton("SOUTH"), "South"); jp2.add(jp1); 15 setContentPane(jp2); 16 setVisible(true); 17 } 18 public static void main(String args [] ) { 19 new TestBorderLayout(); 20 } 21 }

Unit 129 Introduction to Box Layout A box layout organizes components either horizontally (in one row) or vertically (in one column). Components are placed top-to-bottom or left-to-right in the order in which they are added to the container. By combining multiple containers using box layout, many different configurations can be created. Invisible components can be added to a box layout container to take up space between components. –Rigid areas have a fixed size –Glue specifies where excess space should go. A rigid area is created using the createRigidArea method of the Box class Glue is created using the createHorizontalGlue or createVerticalGlue methods.

Unit 1210 BoxLayout

Unit 1211 Further GUI Programming Examples Example 1: Enhancing the Telephone Handset Example Example 2: Menu Test Example 3: File Dialog Test Example 4: Popup Menu Test

Unit 1212 Introduction to Example 1 This example builds on Example 4 of the preceding section. Example 4 of the preceding section developed a simple GUI for a telephone handset. We will now add event-handling code to display typed numbers. Here is a sample output of the program:

Unit 1213 Example 1: Flow Layout 1 import javax.swing.*; 2 class TestFlowLayout extends JFrame{ 3 JPanel panel = new JPanel(); 4 public TestFlowLayout(){ 5 panel.add(new JButton("1")); 6 panel.add(new JButton("2")); 7 panel.add(new JButton("3")); 8 panel.add(new JButton("4")); 9 panel.add(new JButton("5")); 10 panel.add(new JButton("6")); 11 panel.add(new JButton("7")); 12 setContentPane(panel); 13 setSize(300,300); 14 setTitle("Flow Layout Test"); 15 setVisible(true); 16 } 17 public static void main(String [] args){ 18 new TestFlowLayout(); 19 } 20 } Note: we did not set the layout because Flow Layout is the default one for the panel

Unit 1214 Example 1: Grid Layout Test 1 import java.awt.*; import javax.swing.*; 2 import javax.swing.border.*; 3 class TestGridLayout extends TestFlowLayout{ 4 public TestGridLayout(){ 5 panel.add(new JButton("8")); 6 panel.add(new JButton("9")); 7 panel.add(new JButton("*")); 8 panel.add(new JButton("0")); 9 panel.add(new JButton("#")); 10 JLabel jlb = new JLabel( ", SwingConstants.CENTER); 11 Border b =BorderFactory.createBevelBorder(BevelBorder.RAISED); 12 jlb.setBorder(BorderFactory.createTitledBorder(b,Telephone")); 13 panel.add(jlb); 14 setTitle("Grid Layout Test"); 15 panel.setLayout(new GridLayout(0,3)); 16 setVisible(true); 17 } 18 public static void main(String [] args){ 19 new TestGridLayout(); 20 } 21 }

Unit 1215 Example 1: The Telephone Handset 1 import java.awt.*; import javax.swing.*; import java.awt.event.*; 2 class TelephoneTest extends TestGridBagLayout implements ActionListener{ 4 public TelephoneTest(){ 5 Component components[] = getContentPane().getComponents(); 6 JPanel cancelPanel = (JPanel)components[13]; 7 JButton cancel = (JButton)cancelPanel.getComponent(0); 8 for(int i=0;i<components.length; i++){ 9 if(components[i] instanceof JButton) 10 ((JButton)components[i]).addActionListener(this); 11 } 12 cancel.addActionListener(this); 13 } 14 public void actionPerformed(ActionEvent ae) { 15 if (ae.getActionCommand().equals("Cancel")) 16 display.setText(""); 17 else 18 display.setText(display.getText()+ae.getActionCommand()); 19 } 20 public static void main(String [] args){ 21 new TelephoneTest().setVisible(true); 22 }}

Unit 1216 Introduction to Example 2 In this example we demonstrate how menus, separator, mnemonic and accelerators can be added into an application. The output of the example is as follows:

Unit 1217 Class Hierarchy for Menus Classes used to define menus are: oJMenuBar oJMenuItem oJMenu oJCheckButtonMenuItem oJRadioButtonMenuItem

Unit 1218 Mnemonics A mnemonic provides a keyboard alternative for pushing a button or selecting a menu option. The mnemonic character should be chosen from the component's label, and is underlined. The user activates the component by holding down the ALT key and pressing the mnemonic character. A mnemonic is established using the setMnemonic method JButton button = new JButton ("Calculate"); button.setMnemonic ("C");

Unit 1219 Example 2: Menus and Mnemonics 1 import java.awt.event.*; 2 import javax.swing.*; 3 class MenuTest extends JFrame { 4 private JMenuBar menuBar = new JMenuBar(); 5 protected JMenu fileMenu = new JMenu("File"); 6 protected JMenuItem neW, open, quit, save, print; 7 private JMenuItem saveCurrent, saveAs, saveAll; 8 MenuTest(String title){ 9 super(title); 10 setJMenuBar(menuBar); 11 menuBar.add(fileMenu); 12 fileMenu.setMnemonic('F'); 13 fileMenu.add(neW = new JMenuItem ("New")); 14 fileMenu.add(open = new JMenuItem ("Open")); 15 open.setMnemonic('o'); 16 fileMenu.add(save = new JMenu ("Save")); 17 save.add(saveCurrent = new JMenuItem ("Save Current")); 18 save.add(saveAs = new JMenuItem ("Save As")); 19 save.add(saveAll = new JMenuItem ("Save All")); 20 fileMenu.add(save); 21 fileMenu.add(print = new JCheckBoxMenuItem ("Print")); 22 fileMenu.addSeparator();

Unit 1220 Menus and Mnemonics (contd) 23 fileMenu.add(quit = new JMenuItem("Quit")); 24 quit.setMnemonic(KeyEvent.VK_Q); 25 quit.setAccelerator( 26 KeyStroke.getKeyStroke(KeyEvent.VK_Q,KeyEvent.CTRL_MASK)); 27 quit.addActionListener(new ActionListener(){ 28 public void actionPerformed(ActionEvent e){ 29 int result=JOptionPane.showConfirmDialog(MenuTest.this, 30 "Quit/Close Menu Demos Window?"); 31 if (result == JOptionPane.YES_OPTION) 32 System.exit(0); 33 } 34 }); 35 setSize(300,300); 36 } 37 public static void main(String args[]){ 38 MenuTest t = new MenuTest("Menus Test"); 39 t.setVisible(true); 40 } 41 }

Unit 1221 Introduction to Example 3 This example extends Example 2 to launch a file dialog on selecting the Open menu. The selected file name is simply printed on another display window. The output is as follows:

Unit 1222 Example 3: File Dialog Test 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 class FileDialogTest extends MenuTest { 5 JEditorPane textPane= new JEditorPane(); 6 FileDialogTest(String title){ 7 super(title); 8 open.addActionListener( new ActionListener() { 9 public void actionPerformed(ActionEvent e ) { 10 FileDialog fd = new FileDialog(FileDialogTest.this); 11 fd.setVisible(true); 12 textPane.setText("Selected file: "+fd.getFile()); 13 } }); 14 getContentPane().add(textPane); 15 } 16 public static void main(String args[]){ 17 FileDialogTest t = new FileDialogTest("File Dialog Test"); 18 t.setVisible(true); } 21 }

Unit 1223 Introduction to Example 4 This example extends Example 2 of the preceding section to add pop-up menus.. The output looks like: The menu consists of three colors that can be used to reset a components background color.

Unit 1224 Example 4: Popup Menu Test 1 import java.awt.*; import java.awt.event.*; import javax.swing.*; 2 public class PopupMenuTest extends TestGridLayout{ 3 Component selectedComponent; 4 JPopupMenu colorMenu = new JPopupMenu(); 5 JMenuItem blue, white, yellow; 6 public PopupMenuTest(){ 7 setTitle("Popup Menu Test"); 8 colorMenu.add(blue=new JMenuItem("Blue")); 9 colorMenu.add(white=new JMenuItem("White")); 10 colorMenu.add(yellow=new JMenuItem("Yellow")); 11 Component components[] = getContentPane().getComponents(); 12 class MyListener extends MouseAdapter { 13 public void mousePressed(MouseEvent e){checkPopup(e);} 14 public void mouseClicked(MouseEvent e){checkPopup(e);} 15 public void mouseReleased(MouseEvent e){checkPopup(e);} 16 public void checkPopup(MouseEvent e) { 17 if (e.isPopupTrigger()){ 18 selectedComponent = e.getComponent(); 19 colorMenu.show(e.getComponent(),e.getX(),e.getY()); 20 } 21 } 22 }

Unit 1225 Popup Menu Test (contd) 23 MouseListener mylistener = new MyListener(); 24 for(int i=0;i<components.length-1; i++){ 25 JButton b = (JButton)components[i]; 26 b.addMouseListener(mylistener); 27 } 28 blue.addActionListener(new ActionListener () { 29 public void actionPerformed(ActionEvent ae){ 30 selectedComponent.setBackground(Color.blue); 31 }}); 32 white.addActionListener(new ActionListener() { 33 public void actionPerformed(ActionEvent ae){ 34 selectedComponent.setBackground(Color.white); 35 }}); 36 yellow.addActionListener(new ActionListener() { 37 public void actionPerformed(ActionEvent ae){ 38 selectedComponent.setBackground(Color.yellow); 39 }}); 40 } 41 public static void main(String[] args) { 42 new PopupMenuTest().setVisible(true); 43 } 44 }

Unit 1226 Exercises 1.Move the call to the setVisible() method from the main() method to the TetFlowLayout constructor in Example 1. Then compile and run each of Examples 2, 3 and 4. You will notice some changes when displaying the frames. Explain. 2. Modify Example 2 to add two other types of borders to it. 3.Compare Example 2 and Example 3. In Example 2 we inherited a panel, added more components to it and then changed the panelÿ layout manager. In Example 3 we had to create an additional panel. Can Example 3 be written in similar manner to Example 2 without the additional panel? If it is possible, write the equivalent program otherwise explain why it is not possible. 4.Change the fill constraint variable on Line 13 from BOTH to each of the following: NONE, HORIZONTAL and VERTICAL. Run the program in each case and study the effect. 5.Change the value of the gridy constraint variable on Lines 29 to a smaller value. Run the program and observe the result. Do the same for the value on Line 32. Also change these values to bigger values and observe the results.

Unit 1227 Review Exercises 1What are the event sources and event listeners in Example 1? 2Can the event handler in Example 1 be implemented in a single anonymous class? If not, why not? 3Modify Example 1 to use two inner classes instead of one. Can the two inner classes both be anonymous? Is the program with one inner class better than the one with two? Explain. 4Enhance Example 1 so that when the Redial button is pushed the last text displayed on the text component is displayed again. 5Write a Java program to illustrate how a component event can be handled. 6Modify Example 3 so that when a file is double-clicked from the file dialog window the file is opened rather than displaying its name in the editor pane. 7When you run Example 4, you will find that the lower part of the window is not responding to the popup menu. Modify this program to ensure that that part of the window also responds to the popup menu events like the buttons in the window.