GUIs Model/View/Controller Layouts

Slides:



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

Corresponds with Chapter 12
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
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.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
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.
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Computer Science 209 Specializing by Subclassing.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
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.
A Quick Java Swing Tutorial
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Pattern Using BreezySwing
Java GUI.
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Creating Graphical User Interfaces
A Quick Java Swing Tutorial
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
GUI building with the AWT
Specializing by Subclassing
Graphical User Interface
Presentation transcript:

GUIs Model/View/Controller Layouts Computer Science 209 GUIs Model/View/Controller Layouts

Graphical User Interfaces A GUI provides a human user with a view of the state of a data model and with controls for manipulating it A GUI consists of windows, icons, a mouse, and pull-down menus (WIMP), among other things

Event-Driven Programming An application sets up and displays a window The application waits for user events, such as mouse clicks on buttons or menu items The application responds to these events by manipulating the data model and updating the display

Model/View/Controller Pattern In the MVC pattern The model is responsible for managing the data and updating the view The view is responsible for displaying the data and controls (buttons, etc.) The controller listens for user events and informs the model of them

Model/View/Controller Pattern displays View listens Controller Model notifies

Separation of Concerns We can keep the model fixed and change the view We can keep the view fixed and change the model Each control in the view has its own set of listeners, each of which is responsible for acting on a distinct event

Which Toolkit to Use? BreezySwing is for quick and easy GUIs in Java Swing and AWT are standard and are for professional-quality GUIs A newer toolkit, called JavaFX, available starting with Java 7

Example: A Data Model public class Die{ private int value; public Die(){ roll(); } public void roll(){ value = (int)(Math.random() * 6) + 1; public String toString(){ return "" + value; All files in GUIExamples.zip in the Resources folder on Sakai

Example: An Empty Window import javax.swing.JFrame; public class GUIApp{ public static void main(String[] args){ final JFrame view = new JFrame(); view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); view.setSize(200, 200); view.setVisible(true); } main instantiates the frame and sets its principal attributes

Specializing the View import javax.swing.JFrame; public class GUIApp{ public static void main(String[] args){ final JFrame view = new MainView1(); view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); view.setSize(200, 200); view.setVisible(true); } Define a subclass of JFrame to represent the view import javax.swing.*; import java.awt.*; public class MainView1 extends JFrame{ // Code for specializing the view }

Add Two Controls The view’s constructor sets the attributes of the controls and layout and adds them to the frame import javax.swing.*; import java.awt.*; public class MainView1 extends JFrame{ private JTextField diceField = new JTextField(1); private JButton rollButton = new JButton("Roll"); public MainView1(){ this.setTitle("Roll the Die"); diceField.setEditable(false); diceField.setHorizontalAlignment(JTextField.CENTER); Container c = this.getContentPane(); c.add(diceField, BorderLayout.NORTH); c.add(rollButton, BorderLayout.SOUTH); }

Hook Up the Model and the View import javax.swing.JFrame; public class GUIApp{ public static void main(String[] args){ final JFrame view = new MainView2(new Die()); view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); view.setSize(200, 200); view.setVisible(true); } The GUIApp class instantiates the model and the view and connects them

Display the Data Model import javax.swing.*; import java.awt.*; public class MainView2 extends JFrame{ private JTextField diceField = new JTextField(1); private JButton rollButton = new JButton("Roll"); private Die model; public MainView2(Die model){ this.model = model; setTitle("Roll the Die"); diceField.setEditable(false); diceField.setHorizontalAlignment(JTextField.CENTER); diceField.setText(model.toString()); Container c = this.getContentPane(); c.add(diceField, BorderLayout.NORTH); c.add(rollButton, BorderLayout.SOUTH); }

Listening and Responding import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainView3 extends JFrame{ private JTextField diceField = new JTextField(1); private JButton rollButton = new JButton("Roll"); private Die model; public MainView3(Die model){ this.model = model; this.setTitle("Roll the Die"); diceField.setEditable(false); diceField.setHorizontalAlignment(JTextField.CENTER); diceField.setText(model.toString()); rollButton.addActionListener(new RollListener()); Container c = this.getContentPane(); c.add(diceField, BorderLayout.NORTH); c.add(rollButton, BorderLayout.SOUTH);

Listening and Responding import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainView3 extends JFrame{ private JTextField diceField = new JTextField(1); private JButton rollButton = new JButton("Roll"); private Die model; … private class RollListener implements ActionListener{ public void actionPerformed(ActionEvent e){ model.roll(); diceField.setText(model.toString()); }

Components, Containers, and Layouts Some GUI components are primitives, such as buttons and fields Others are containers in which components can be placed, such as frames and panels (panels can be nested recursively) The manner of organizing components can vary with the container and with the application

Layout Managers Components are added to a container under the influence of a layout manager The default layout manager for frames and dialogs is BorderLayout The default layout manager for panels and applets is FlowLayout

The Layout Strategy The different layout managers implement the LayoutManager interface A container calls methods in this interface to lay out the components The user of the container supplies an instance of this interface for a particular type of layout

Common Layouts FlowLayout Wrap around effect BorderLayout 5 distinct areas GridLayout Two-dimensional grid of equal-sized areas GridBagLayout Allows stretching of cells across rows and columns

Flow Layouts Can specify alignment and margins between components public FlowView(){ Container c = getContentPane(); c.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15)); c.add(new JButton("One")); c.add(new JButton("Two")); c.add(new JButton("Three")); c.add(new JButton("Four")); } Can specify alignment and margins between components Components occupy the minimum space necessary

Border Layouts Components stretch to fill their areas public BorderView(){ Container c = getContentPane(); c.add(new JButton("North") , BorderLayout.NORTH); c.add(new JButton("East") , BorderLayout.EAST); c.add(new JButton("South") , BorderLayout.SOUTH); c.add(new JButton("West") , BorderLayout.WEST); c.add(new JButton("Center”), BorderLayout.CENTER); } Components stretch to fill their areas Filled areas expand to fill areas left empty

Grid Layouts Cells are filled in row major order public GridView(){ Container c = getContentPane(); c.setLayout(new GridLayout(2, 2)); c.add(new JButton("One")); c.add(new JButton("Two")); c.add(new JButton("Three")); c.add(new JButton("Four")); } Cells are filled in row major order Components stretch to fill their cells

Gridbag Layouts public GridBagView(){ GridBagLayout layout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); Container c = getContentPane(); c.setLayout(layout); constraints.gridx = 0; constraints.gridy = 0; layout.setConstraints(widget1, constraints); c.add(widget1); constraints.gridx = 1; layout.setConstraints(widget2, constraints); c.add(widget2); constraints.gridy = 1; constraints.gridwidth = 2; constraints.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(widget3, constraints); c.add(widget3); }

Planning a Layout Draw a picture of the desired look Use nested panels to structure components where necessary Choose appropriate layout managers for each subarea in the main window

Refine the Layout import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainView3 extends JFrame{ private JTextField diceField = new JTextField(1); private JButton rollButton = new JButton("Roll"); private Die model; public MainView3(Die model){ … Container c = this.getContentPane(); JPanel dicePanel = new JPanel(); dicePanel.add(diceField); JPanel rollPanel = new JPanel(); rollPanel.add(rollButton); this.setLayout(new GridLayout(2, 1)); c.add(dicePanel); c.add(rollPanel); this.setSize(200, 100);

A GUI for a Student Object