CS410G: GUI Programming. The Model/View/Controller Pattern. Model. Controller. View. MVC is a popular architecture for building GUIs

Size: px
Start display at page:

Download "CS410G: GUI Programming. The Model/View/Controller Pattern. Model. Controller. View. MVC is a popular architecture for building GUIs"

Transcription

1 CS410G: GUI Programming The Model/View/Controller design pattern provides a clean distinction between the your application s data (model), your GUI (view), and the how they interact (controller). Many swing components follow the MVC pattern, easing use and implementation, and providing a clean abstract for multiple looks and feels. Model/View/Controller The MVC design pattern Basic MVC widgets Swing tables and trees The Model/View/Controller Pattern MVC is a popular architecture for building GUIs There are three participants in the MVC pattern Model: Underlying logical representation of the data being displayed View: The visual representation of the data (widgets) Controller: Specifies how to handle user input Usually updates the View and Model accordingly When Model changes, it notifies its views You can easily have multiple views of the same data Copyright c by David M. Whitlock. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and full citation on the first page. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or fee. Request permission to publish from whitlock@cs.pdx.edu. 1 You can create views without changing the model The Observer and Observable interfaces have been around since JDK Model The model maintains a component s state Is a button pressed? The character data in a label The model does not know about the view and the controller Communicates with them indirectly using broadcasts (events) View The view determines the visual representation of the model The view is the look of the component The view renders itself based on indirect messages from the model and direct messages from the controller Controller The controller determines how the component should react to input events The controller is the feel of the component Controller receives direct message from the view and indirect messages from the model Example: User clicks a button The button s controller determines that the click was meant for it Controller sends a message to the model The model updates itself accordingly and broadcasts that it has changed View receives broadcast and updates itself based on the new state of the model In MVC a model is bound to a single controller. The controller may have multiple views of same model. 3 4

2 MVC in Swing Swing combines the View and Controller into an object called a delegate Model and Delegate for Buttons All Swing buttons (JButtons, JCheckboxes, JRadioButtons, etc.) are subclasses of AbstractButton A delegate displays the model and adjusts the model based on user input A JComponent always has a model and delegate (UI) associated with it The View part of the delegate determines the widget s look and feel The look and feel (L&F) can be changed Windows L&F, Motif L&F, custom L&F A ButtonModel defines the model for a button Methods for adding ActionListeners and ItemListeners ispressed, isselected A DefaultButtonModel knows about a button s group, etc. A javax.swing.plaf.buttonui is responsible for the button s L&F A button invokes methods of it ButtonUI when it needs to be painted or updated ButtonUI determines the size of the button Your programs need not invoke these methods 5 6 More About UI Delegates All UI delegates are a subclass of ComponentUI The static createui factory method creates a ComponentUI for a given JComponent There is usually one ComponentUI instance per component type One MetalButtonUI that is used by all JButtons The installui and uninstallui methods add/remove listeners that connect the component (or its model) to the UI A ComponentUI is also involved with laying out a component getpreferredsize(jcomponent), getminimumsize, getmaximumsize The javax.swing.plaf package contains a hierarchy of abstract classes for UIs ButtonUI, MenuBarUI, FileChooserUI, etc. Pluggable Look and Feel Because Swing widgets adhere to the MVC pattern, they provide a pluggable look and feel A Swing GUI can look like its running on Windows, Motif, etc., or you can define a custom L&F The LookAndFeel class maps JComponents to their UIs and specifies a L&F A UIManager maintains the current L&F Has methods like getfont, geticon getlookandfeel returns current L&F setlookandfeel sets L&F using class name If you are changing L&F in the middle of your program (not recommended), your GUI can be updated by invoking SwingUtilities.updateComponentTreeUI See edu.pdx.cs410g.mvc.looksandfeels 7 8

3 An MVC Example Let s say that you have a program in which the user enters numbers Display all of the numbers entered and the average of those numbers Traditional widget and event handling When user hits ENTER, a JTextField s ActionListener adds text to a JList Another ActionListener on JTextField reads all entries in JList and computes their average Widgets are tightly coupled: Doesn t scale Order of ActionListeners is unknown MVC solution: Store data outside of the widgets Keep all of the numbers in a list The widgets observe the list When the list changes, the widgets are notified and widgets get the info they want from the list and display it appropriately 9 Modeling a list of data A ListModel models a list of data getsize and getelementat Each ListModel may have multiple ListDataListeners Callback methods that each get a ListDataEvent: contentschanged, intervaladded, intervalremoved An AbstractListModel is a helper class that handles the coordination between the ListModel and its ListDataListeners Subclasses invoke firecontentschanged, etc. when model changes DefaultListModel provides a ListModel that provides Vector-like behavior addelement adds an object to the underlying model and invokes firecontentschanged to alert listeners of the change 10 A JList Views a ListModel A JList can be created from a ListModel When the model is changed, the JList receives a ListDataEvent and updates itself appropriately A JList may have an array of Objects or a Vector as its model All of the event handling between the model and view is taken care of for you Other ListDataListeners can act as controllers between a ListModel and a widget Note that ListDataListener and ListDataEvent are in the javax.swing.event package 11 An Example of ListModel Creating a ListModel and adding data to it: public class DisplayAverage extends JPanel { public DisplayAverage() { // The data we re modeling final DefaultListModel model = new DefaultListModel(); // A panel for entering a number JPanel enter = new JPanel(); enter.add(new JLabel("Enter a number")); final JTextField input = new JTextField(8); input.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { // When the user hits ENTER, add the // number to the data model model.addelement(new Integer(input.getText ); enter.add(input); // A JList that displays the data in the model JList list = new JList(model); this.add(new JScrollPane(list), BorderLayout.CENTER); 12

4 An Example of ListModel Adding a controller (ListDataListener) between the model and another view (average JLabel) final JLabel average = new JLabel("Average:"); model.addlistdatalistener(new ListDataListener() public void intervaladded(listdataevent e) { displayaverage(); public void intervalremoved(listdataevent e) displayaverage(); public void contentschanged(listdataevent e) displayaverage(); So, what? By separating what is being displayed (model) from how it is displayed (view), we have made our GUI easy to understand and extend Could add a standard deviation view without changing any of the other views private void displayaverage() { int size = model.getsize(); int total = 0; for (int i = 0; i < size; i++) { Integer datum = (Integer) model.getelementat(i); total += datum.intvalue(); average.settext("average: " + (((double) total)/((double) size))); ); JSpinner JSpinner is a widget that was added in JDK 1.4 and allows the user to spin through values Behavior defined by a SpinnerModel Several kinds of SpinnerModels SpinnerNumberModel: Can set initial, max, min values and increment step SpinnerDateModel: Can spin on one of the fields of a Date (e.g. Calendar.HOUR) SpinnerListModel: Can spin through a List of objects Once again, the JSpinner delivers the user events to the model which, in turn, notifies the spinner that the model has changed See edu.pdx.cs410g.mvc.jspinnerexample Swing Tables The javax.swing.table package contains a framework for creating MVC-based tables A TableModel abstractlly represents a table getcolumncount, getrowcount, getvalueat getcolumnclass: Returns the type of data in a column Data may be displayed differently based on type (e.g. checkbox for booleans) A TableModel may have multiple TabelModelListeners tablechanged invoked with a TabelModelEvent when TableModel changes TabelModelEvent consists of First row, last row, and column that changed The type of change: INSERT, UPDATE, or DELETE A DefaultTableModel is built from a two-dimensional array of objects and an array of objects that specify the names of the columns 15 16

5 A JTable Views a TableModel A JTable can be created with a default number of rows and columns, ordered data such as an object array or Vector or from a TableModel public class JTableExample extends JPanel { public JTableExample() { this.setlayout(new BorderLayout()); String[] columnnames = { "Name", "Quiz1", "Quiz2", "Quiz3" ; Object[][] data = { { "Mary", new Double(85.0), new Double(92.5), { "Sunil", new Double(94.3), new Double(82.9), { "Fred", new Double(84.0), new Double(72.5), { "Jin", new Double(98.2), new Double(92.5), n ; TableModel model = new DefaultTableModel(data, columnnames); JTable tree = new JTable(model); this.add(new JScrollPane(tree), BorderLayout.CENTER); A More Complex JTable We are going write a GUI that models a checkbook using a JTable A simple Check class models a check with a number, amount, description, whether it has cleared, etc. A TableModel models the checks in a checkbook and determines how they should be displayed CheckbookTableModel extends AbstractTableModel which provides fire methods that force the view to be updated The CheckbookGUI creates, configures, and displays the JTable When the button is clicked, a new Check is created and added to the model public void createcheck() { this.checks.add(new Check(nextNumber++)); int row = checks.size() - 1; this.firetablerowsinserted(row, row); Note that the JTable must be in a JScrollPane in order for the table headings to be displayed Editing a Check The model specifies which cells can be edited (iscelleditable) The Number and Balance columns cannot be edited The model specifies the types of data in the columns The Date column is a java.util.date, Cleared is a Boolean By default, the JTable will parse the contents of the typed cell and return it This behavior can be customized with a TableCellEditor A DefaultCellEditor can be created from a JCheckbox, JComboBox, or JTextField The Transaction column has a custom editor 19 Displaying a Check A TableCellRenderer specifies how a cell in a JTable should be drawn When the balance drops below zero, display it in red public class CheckbookCellRenderer extends DefaultTableCellRenderer { public Component gettablecellrenderercomponent(jtable table, Object value, boolean isselected, boolean hasfocus, int row, int column) { if (column == CheckbookTableModel.BALANCE_COLUMN if (value!= null && value instanceof Number) Color color; double balance = ((Double) value).doublevalu if (balance < 0) { color = Color.RED; else { color = Color.BLACK; this.setforeground(color); this.settext(string.valueof(balance)); return this; return super.gettablecellrenderercomponent(table 20

6 Creating a JTable Each column in the table is represented by a TableColumn From CheckbookGUI: CheckbookTableModel checkbook = new CheckbookTableModel(); JTable table = new JTable(checkbook); String columnname = columnnames[balance_column]; TableColumn column = table.getcolumn(columnname); column.setcellrenderer(new CheckbookCellRenderer() columnname = columnnames[transaction_column]; column = table.getcolumn(columnname); JComboBox combo = new JComboBox(new String[] { DEPOSIT, WITHDRAWL column.setcelleditor(new DefaultCellEditor(combo)) this.add(new JScrollPane(table), BorderLayout.CENT The moral of the story is: The data is completely separate from how it is displayed and edited! Tree-like Data The JTree widget displays hierarchical data A JTree can be created from a Hashtable, object array or Vector that specifies the tree s root A JTree can also be created from a TreeModel (similar to a TableModel) See package javax.swing.tree It may also make more sense to model your tree data with a TreeNode Just models one node in the tree, instead of the entire tree Has methods like children, getchildat, getparent, and isleaf An Example TreeNode A FileNode models a node in a directory hierarchy: public class FileNode implements TreeNode { public TreeNode getparent() { File parent = file.getparentfile(); if (parent == null) { return null; else { return new FileNode(file); public boolean isleaf() { return!this.file.isdirectory(); //... Displaying nodes in a JTree A TreeCellRenderer specifies how a node in a tree should be drawn DefaultTreeCellRenderer provides a lot of useful behavior Can customize: background color, font, icons (closed, open, leaf) It is a subclass of JLabel so it has methods like settext and settooltiptext The gettreecellrenderercomponent is invoked when a node is drawn The value provided is the node being drawn 23 24

7 An Example TreeCellRenderer public class FileNodeRenderer extends DefaultTreeCel public Component gettreecellrenderercomponent(jtree tree, Object value, boolean selected, boolean expanded, boolean isleaf, int row, boolean hasfocus) { JLabel c = (JLabel) super.gettreecellrenderercomponent(tree, value selected, expanded, isleaf, row, hasfocus); if (value instanceof FileNode) { FileNode node = (FileNode) value; File file = node.getfile(); if (!file.isdirectory()) { c.settext(node.tostring()); if (file.ishidden()) { c.setforeground(color.red); return c; Other Swing MVC Models BoundedRangeModel Has a (current) value, extent, min, and max Used with JProgressBar, JScrollBar, JSlider ComboBoxModel A subinterface of ListModel that adds the notion of a selected item in the list MutableComboBoxModel A ComboBoxModel with add/remove/insert methods javax.swing.colorchooser.colorselectionmodel Used with a JColorChooser return c; Has a selected color Note that most container widgets (JApplet, JFrame, JDesktopPane, etc.) do not have models Summary The Model/View/Controller design pattern separates data from how it is displayed The model is the data being displayed The view is the widget(s) that display the data The controller handles user input and updates the model and view 27

Basics of programming 3. Java GUI and SWING

Basics of programming 3. Java GUI and SWING Basics of programming 3 Java GUI and SWING Complex widgets Basics of programming 3 BME IIT, Goldschmidt Balázs 2 Complex widgets JList elements can be selected from a list JComboBox drop down list with

More information

Swing. By Iqtidar Ali

Swing. By Iqtidar Ali Swing By Iqtidar Ali Background of Swing We have been looking at AWT (Abstract Window ToolKit) components up till now. Programmers were not comfortable when doing programming with AWT. Bcoz AWT is limited

More information

Swing. Component overview. Java UI, summer semester 2017/2018 1

Swing. Component overview. Java UI, summer semester 2017/2018 1 Swing Component overview 1 Label class JLabel for displaying short text image both 2 Buttons many kinds of buttons all of them extends AbstractButton regular button (JButton) "click" button toggle button

More information

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

Swing JTable. JTable

Swing JTable. JTable Swing JTable Corso di Principi di Progettazione del Software, a.a. 2017/18 12 Dicembre 2017 ing. Roberto Vergallo 1 JTable Tables are used to display data in a spreadsheet fashion The JTable is oriented

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 10(b): Working with Controls Agenda 2 Case study: TextFields and Labels Combo Boxes buttons List manipulation Radio buttons and checkboxes

More information

Uppsala University. Assignment 3. Separation into Model-View TableModel ListModel ( multiple inheritance or adapter) Renderer (delegation)

Uppsala University. Assignment 3. Separation into Model-View TableModel ListModel ( multiple inheritance or adapter) Renderer (delegation) ToDo-list Assignment 3 Separation into Model-View TableModel ListModel ( multiple inheritance or adapter) Renderer (delegation) A new component Extend Swing with your own design Theme Modify look&feel

More information

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p. Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p. 17 MVC Architecture p. 19 The MVC Architecture p. 20 Combined

More information

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo Widgets Overview Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo Widget Widget is a generic name for parts of an interface that have their own behavior: buttons, progress

More information

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert Java Programming in Java Java Swing Programming Java Swing Design Goals The overall goal for the Swing project was: To build a set of extensible GUI components to enable developers to more rapidly develop

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers Course Name: Advanced Java Lecture 19 Topics to be covered Java Swing Lists Trees Tables Styled Text Components Progress Indicators Component Organizers AWT to Swing AWT: Abstract Windowing Toolkit import

More information

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff Swing UI by Vlad Costel Ungureanu for Learn Stuff User Interface Command Line Graphical User Interface (GUI) Tactile User Interface (TUI) Multimedia (voice) Intelligent (gesture recognition) 2 Making the

More information

Design Case Study: GUI with Swing (2)

Design Case Study: GUI with Swing (2) Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Design Case Study: GUI with Swing (2) Christian Kästner Charlie Garrod School of Computer Science 1 Administrivia

More information

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class. Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space

More information

Widget. Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down

Widget. Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down Widgets Jeff Avery Widget Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down menus, spinners, file dialog boxes, etc are

More information

Charlie Garrod Bogdan Vasilescu

Charlie Garrod Bogdan Vasilescu Principles of So3ware Construc9on: Objects, Design, and Concurrency Part 2: Designing (sub-) systems A GUI design case study Charlie Garrod Bogdan Vasilescu School of Computer Science 1 Administrivia Homework

More information

Chapter 12 GUI Basics

Chapter 12 GUI Basics Chapter 12 GUI Basics 1 Creating GUI Objects // Create a button with text OK JButton jbtok = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblname = new JLabel("Enter your

More information

John Zukowski's Definitive Guide to Swing for Java 2

John Zukowski's Definitive Guide to Swing for Java 2 John Zukowski's Definitive Guide to Swing for Java 2 JOHN ZUKOWSKI APress Media, LLC John Zukowski's Definitive Guide to Swing for Java 2 Copyright ~1999 by John Zukowski Originally published by Apress

More information

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

Design Case Study: GUI with Swing

Design Case Study: GUI with Swing Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Design Case Study: GUI with Swing Christian Kästner Bogdan Vasilescu School of Computer Science 1 Administrivia

More information

Java 1.9 Swing Index

Java 1.9 Swing Index One Introduction to Java 2 Usage of Java 3 Structure of Java 4 Flexibility of Java Programming 5 Swing and AWT in Java 6 Two Using Java in DOS 9 Using the DOS window 10 DOS Operating System Commands 11

More information

Java IDE Programming-I

Java IDE Programming-I Java IDE Programming-I Graphical User Interface : is an interface that uses pictures and other graphic entities along with text, to interact with user. User can interact with GUI using mouse click/ or

More information

Java 11 Swing with Eclipse Index

Java 11 Swing with Eclipse Index One Introduction to Java 1 Usage of Java 2 Structure of Java 4 Flexibility of Java Programming 5 Swing and AWT in Java 7 Using Eclipse 9 Two Dialog Boxes 10 Using Dialog Boxes 11 Using Message Dialogs

More information

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7 PROGRAMMING DESIGN USING JAVA (ITT 303) Graphical User Interface Unit 7 Learning Objectives At the end of this unit students should be able to: Build graphical user interfaces Create and manipulate buttons,

More information

Java 11 Swing Index. Section Title Page

Java 11 Swing Index. Section Title Page One Introduction to Java 2 Usage of Java 3 Structure of Java 4 Flexibility of Java Programming 5 Swing and AWT in Java 6 Two Using Java in DOS 9 Using the DOS window 10 DOS Operating System Commands 11

More information

Java 1.8 Swing with Eclipse Oxygen Index

Java 1.8 Swing with Eclipse Oxygen Index One Introduction to Java 1 Usage of Java 2 Structure of Java 4 Flexibility of Java Programming 5 Using the Eclipse software 6 Swing and AWT in Java 7 Two Running Java in Eclipse 10 Introduction 11 Using

More information

Java 1.8 Swing with Eclipse Mars Index

Java 1.8 Swing with Eclipse Mars Index One Introduction to Java 1 Usage of Java 2 Structure of Java 4 Flexibility of Java Programming 5 Using the Eclipse software 6 Swing and AWT in Java 7 Two Running Java in Eclipse 10 Introduction 11 Using

More information

Java 1.8 Swing with Eclipse Neon Index

Java 1.8 Swing with Eclipse Neon Index One Introduction to Java 1 Usage of Java 2 Structure of Java 4 Flexibility of Java Programming 5 Using the Eclipse software 6 Swing and AWT in Java 7 Two Running Java in Eclipse 10 Introduction 11 Using

More information

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI CBOP3203 Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI components like button, text input, scroll bar and others.

More information

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords Goals By the end of this lesson, you should: Lecture 7 More GUI programming 1. Be able to write Java s with JTextField, JList, JCheckBox and JRadioButton components 2. Be able to implement a ButtonGroup

More information

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static 18 7 17 1 1.1 GUI ( ) GUI ( ) JFrame public class XXX extends JFrame { public XXX() { // XXX // init()... // ( )... init() main() public static public class XXX extends JFrame { public XXX() { // setsize(,

More information

CS108, Stanford Handout #29. MVC Table

CS108, Stanford Handout #29. MVC Table CS108, Stanford Handout #29 Fall, 2007-08 Nick Parlante MVC Table GUI Model and View How Does a GUI Work? You see pixels on screen representing your data. You click and make gestures, and this appears

More information

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON- Ordering Information: Advanced Java 2 Platform How to Program View the complete Table of Contents Read the Preface Download the Code Examples To view all the Deitel products and services available, visit

More information

To gain experience using GUI components and listeners.

To gain experience using GUI components and listeners. Lab 5 Handout 7 CSCI 134: Fall, 2017 TextPlay Objective To gain experience using GUI components and listeners. Note 1: You may work with a partner on this lab. If you do, turn in only one lab with both

More information

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

Contents Introduction 1

Contents Introduction 1 SELF-STUDY iii Introduction 1 Course Purpose... 1 Course Goals...1 Exercises... 2 Scenario-Based Learning... 3 Multimedia Overview... 3 Assessment... 3 Hardware and Software Requirements... 4 Chapter 1

More information

Jonathan Aldrich Charlie Garrod

Jonathan Aldrich Charlie Garrod Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduction to GUIs Jonathan Aldrich Charlie Garrod School of Computer Science 1 Administrivia Homework

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

Is image everything?

Is image everything? Is image everything? Review Computer Graphics technology enables GUIs and computer gaming. GUI's are a fundamental enabling computer technology. Without a GUI there would not be any, or much less: Computer

More information

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1 Java Swing based on slides by: Walter Milner Java Swing Walter Milner 2005: Slide 1 What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes

More information

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons CompSci 125 Lecture 17 GUI: Graphics, Check Boxes, Radio Buttons Announcements GUI Review Review: Inheritance Subclass is a Parent class Includes parent s features May Extend May Modify extends! Parent

More information

Java Graphical User Interfaces

Java Graphical User Interfaces Java Graphical User Interfaces 1 The Abstract Windowing Toolkit (AWT) Since Java was first released, its user interface facilities have been a significant weakness The Abstract Windowing Toolkit (AWT)

More information

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014 Introduction to GUIs Principles of Software Construction: Objects, Design, and Concurrency Jonathan Aldrich and Charlie Garrod Fall 2014 Slides copyright 2014 by Jonathan Aldrich, Charlie Garrod, Christian

More information

Lecture 18 Java Graphics and GUIs

Lecture 18 Java Graphics and GUIs CSE 331 Software Design and Implementation The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction Lecture 18 Java Graphics and GUIs None

More information

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. Chapter 13 GUI Basics 1 Motivations The design of the API for Java GUI programming is an excellent example of how the object-oriented principle is applied. In the chapters that follow, you will learn the

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT INSTITUTIONEN FÖR Tool Kits, Swing SMD158 Interactive Systems Spring 2005 Jan-28-05 2002-2005 by David A. Carr 1 L Overview Tool kits in the abstract An overview of Swing/AWT Jan-28-05 2002-2005 by David

More information

Graphical User Interfaces

Graphical User Interfaces Graphical User Interfaces Java Programming (Languages in Depth Series) Christoph Angerer April 26th 2007 Why Java UI Programming is Special Platform independence ( write once runs anywhere ): Windows,

More information

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing Introduction to Graphical Interface Programming in Java Introduction to AWT and Swing GUI versus Graphics Programming Graphical User Interface (GUI) Graphics Programming Purpose is to display info and

More information

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005 Advanced Java Programming Swing Eran Werner, Tel-Aviv University Summer, 2005 19 May 2005 Advanced Java Programming, Summer 2005 1 Introduction to Swing The Swing package is part of the Java Foundation

More information

I VE GOT A LITTLE LIST

I VE GOT A LITTLE LIST 1 I VE GOT A LITTLE LIST James W. Cooper As someday it may happen that a sorted list must be found, Java s got the list. I was thinking about the problem of sorted terms in a list box when I was writing

More information

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Hal Perkins Winter 2018 Java Graphics and GUIs 1 The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction

More information

Outline. Composite Pattern. Model-View-Controller Pattern Callback Pattern

Outline. Composite Pattern. Model-View-Controller Pattern Callback Pattern Outline Composite Pattern Motivation Structure Transparent vs Safe composite Applications: AWT & Swing Composite Problems: Alias references Model-View-Controller Pattern Callback Pattern 1 Composite Pattern

More information

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Graphical User Interfaces. Swing. Jose Jesus García Rueda Graphical User Interfaces. Swing Jose Jesus García Rueda Introduction What are the GUIs? Well known examples Basic concepts Graphical application. Containers. Actions. Events. Graphical elements: Menu

More information

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT Lecturer: Michael Hotan slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer,

More information

MVC Table / Delegation

MVC Table / Delegation CS108, Stanford Handout #27 Winter, 2006-07 Nick Parlante MVC Table / Delegation Delegate MVC Table Censor Example Uses the delegate strategy to build a variant table model Uses table model listener logic

More information

Custom Data Models and Cell Renderers

Custom Data Models and Cell Renderers 2010 Marty Hall Advanced Swing & MVC Custom Data Models and Cell Renderers Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/course-materials/java5.html 2 Customized Java

More information

Workaround Swing Bugs

Workaround Swing Bugs Workaround Swing Bugs David Qiao, JIDE Software, Inc. BOF-5133 Background of Myself and JIDE CTO and Founder of JIDE Software, Inc. Heavily use Swing in the last several years Build components and frameworks

More information

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC). The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness The Abstract Windowing Toolkit (AWT) was part of the JDK form the beginning,

More information

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation CSE 331 Software Design and Implementation Lecture 18 Java Graphics and GUIs Announcements Leah Perlmutter / Summer 2018 Announcements Quiz 6 due Thursday 8/2 Homework 7 due Thursday 8/2 Regression testing

More information

MODEL UPDATES MANIPULATES USER

MODEL UPDATES MANIPULATES USER 1) What do you mean by MVC architecture? Explain its role in modern applications and list its advantages(may-2013,jan-2013,nov-2011). In addition to dividing the application into three kinds of components,

More information

Swing Programming Example Number 2

Swing Programming Example Number 2 1 Swing Programming Example Number 2 Problem Statement (Part 1 and 2 (H/w- assignment) 2 Demonstrate the use of swing Label, TextField, RadioButton, CheckBox, Listbox,Combo Box, Toggle button,image Icon

More information

Part 3: Graphical User Interface (GUI) & Java Applets

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming Chapter 12 GUI Basics 1 Motivations The design of the API for Java GUI programming is an excellent example of how the object-orientedoriented principle is applied. In the chapters that follow, you will

More information

Bringing Life to Swing Desktop Applications

Bringing Life to Swing Desktop Applications Bringing Life to Swing Desktop Applications Alexander Potochkin Sun Microsystems Kirill Grouchnikov Amdocs Inc. TS-3414 2007 JavaOne SM Conference Session TS-3414 Presentation Goal Learn advanced painting

More information

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1 Widgets Widgets Widget Toolkits 2.3 Widgets 1 User Interface Widget Widget is a generic name for parts of an interface that have their own behavior: buttons, drop-down menus, spinners, file dialog boxes,

More information

Static Detection of Brittle Parameter Typing

Static Detection of Brittle Parameter Typing Static Detection of Brittle Parameter Typing Michael Pradel, Severin Heiniger, and Thomas R. Gross Department of Computer Science ETH Zurich 1 Motivation void m(a a) {... } Object A.. B C D E F 2 Motivation

More information

Computer Science 210: Data Structures. Intro to Java Graphics

Computer Science 210: Data Structures. Intro to Java Graphics Computer Science 210: Data Structures Intro to Java Graphics Summary Today GUIs in Java using Swing in-class: a Scribbler program READING: browse Java online Docs, Swing tutorials GUIs in Java Java comes

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068 Welcome to! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) Overview JAVA and GUIs: SWING Container, Components, Layouts Using SWING Streams and Files Text

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 2 2 Develop the layout of those elements 3 3 Add listeners to the elements 9 4 Implement custom drawing 12 1 The StringArt Program To illustrate

More information

Programming graphics

Programming graphics Programming graphics Need a window javax.swing.jframe Several essential steps to use (necessary plumbing ): Set the size width and height in pixels Set a title (optional), and a close operation Make it

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 3 2 Develop the layout of those elements 4 3 Add listeners to the elements 12 4 Implement custom drawing 15 1 The StringArt Program To illustrate

More information

Widgets. Widgets Widget Toolkits. User Interface Widget

Widgets. Widgets Widget Toolkits. User Interface Widget Widgets Widgets Widget Toolkits 2.3 Widgets 1 User Interface Widget Widget is a generic name for parts of an interface that have their own behavior: buttons, drop-down menus, spinners, file dialog boxes,

More information

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III Electrical and Computer Engineering Object-Oriented Topic : User Interface Components with Swing GUI Part III Maj Joel Young Joel.Young@afit.edu 17-Sep-03 Maj Joel Young Creating GUI Apps The Process Overview

More information

EVENTS, EVENT SOURCES AND LISTENERS

EVENTS, EVENT SOURCES AND LISTENERS Java Programming EVENT HANDLING Arash Habibi Lashkari Ph.D. Candidate of UTM University Kuala Lumpur, Malaysia All Rights Reserved 2010, www.ahlashkari.com EVENTS, EVENT SOURCES AND LISTENERS Important

More information

UI Software Organization

UI Software Organization UI Software Organization The user interface From previous class: Generally want to think of the UI as only one component of the system Deals with the user Separate from the functional core (AKA, the app

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2 Interactive

More information

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation KF5008 Program Design & Development Lecture 1 Usability GUI Design and Implementation Types of Requirements Functional Requirements What the system does or is expected to do Non-functional Requirements

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The layout manager helps lay out the components held by this container. When you set a layout to null, you tell the

More information

Trees CSCI 201 Principles of Software Development

Trees CSCI 201 Principles of Software Development Trees CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Outline Trees Program USC CSCI 201L Trees Overview JTree is a component that displays data in a treelike hierarchy

More information

Design patterns for graphical user interface applications

Design patterns for graphical user interface applications Design patterns for graphical user interface applications Prof.Asoc. Alda Kika Department of Informatics Faculty of Natural Sciences University of Tirana Outline Pattern Concept Design pattern in computer

More information

Java: Graphical User Interfaces (GUI)

Java: Graphical User Interfaces (GUI) Chair of Software Engineering Carlo A. Furia, Marco Piccioni, and Bertrand Meyer Java: Graphical User Interfaces (GUI) With material from Christoph Angerer The essence of the Java Graphics API Application

More information

2110: GUIS: Graphical User Interfaces

2110: GUIS: Graphical User Interfaces 2110: GUIS: Graphical User Interfaces Their mouse had a mean time between failure of a week it would jam up irreparably, or... jam up on the table--... It had a flimsy cord whose wires would break. Steve

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs) CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox

More information

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions. Parts of a Contract Syntax - Method signature Method name Parameter list Return type Semantics - Comments Preconditions: requirements placed on the caller Postconditions: what the method modifies and/or

More information

Outline. Observer Pattern: Pitfalls. Observer Applications

Outline. Observer Pattern: Pitfalls. Observer Applications Outline Observer Pattern: Pitfalls NotifyObservers Invocation Problem M:N Problem ConcurrentModificationException Problem Cyclic Dependency Problem Causality of State Changes Problem Memory Management

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

Agenda. Container and Component

Agenda. Container and Component Agenda Types of GUI classes/objects Step-by-step guide to create a graphic user interface Step-by-step guide to event-handling PS5 Problem 1 PS5 Problem 2 Container and Component There are two types of

More information

Chapter 17 Creating User Interfaces

Chapter 17 Creating User Interfaces Chapter 17 Creating User Interfaces 1 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity and knowledge of how GUI components work.

More information

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Graphics programming COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Overview Aims To provide an overview of Swing and the AWT To show how to build

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT-V TWO MARKS QUESTION & ANSWER 1. What is the difference between the Font and FontMetrics class? Font class is used to set or retrieve the screen fonts.the Font class maps the characters of the language

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Kevin Zatloukal Fall 2017 Java GUIs (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal Perkins, Zach Tatlock) Reminders HW8 due today Section tomorrow

More information

International Swinging - Making Swing Components Locale-Sensitive

International Swinging - Making Swing Components Locale-Sensitive International Swinging - Making Swing Components Locale-Sensitive Volker Simonis WSI für Informatik, Universität Tübingen, Germany email: simonis@informatik.uni-tuebingen.de June 17, 2002 Abstract Although

More information