CompSci 280 S Introduction to Software Development

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Michael Brockway Advanced Applications Development in Java Model-View-Controller Architecture l MVC Architecture l Delegate-Model Architecture l Observer.
COSC 3461: Module 8 Model-View-Controller (MVC) Architecture.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
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.
© Marty Hall, Larry Brown Web core programming 1 Advanced Swing Custom Data Models and Cell Renderers.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
MVC CompSci 230 S Software Construction. MVC Architecture  A typical application includes software to  maintain application data,  document text.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
DAT602 Database Application Development Lecture 6 JAVA Swing.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 11 Arrays Continued
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
CSE 219 Patterns in Programming More Design Patterns.
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing.
Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model.
3461 Model-View Controller Advanced GUI concepts.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
Advanced Swing Custom Data Models and Cell Renderers.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
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:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
CompSci 230 S Software Construction
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.
CompSci 230 S Programming Techniques
CompSci 280 S Introduction to Software Development
Lecture 16 More GUI programming
The practice of JTable in swing
CHAPTER 7 & 8 REVIEW QUESTIONS
Lecture 27 Creating Custom GUIs
Advanced Swing Lists.
Advanced Swing Tables.
A Quick Java Swing Tutorial
Programming in Java Event Handling
Ellen Walker Hiram College
Event-driven programming for GUI
Introduction to Event Handling
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Lecture Set 10 Windows Controls and Forms
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
A type is a collection of values
Presentation transcript:

CompSci 280 S2 2107 Introduction to Software Development GUI 2

Today’s Agenda Topics: Java Python JList, JComboBox DefaultListModel DefaultComboBoxModel JTable & TableModel DefaultTableMode implements the AbstractTableModel Event Handling Python QListView QAbstractListModel QTableView QAbstractTableModel Lecture17

Java MVC Example Example: JList Example: PhilosophersJList.java <<interface>> Javax.swing.ListModel Javax.swing.JList Add/remove items notifies <<abstract>> AbstractListModel modifies DefaultListModel DefaultListModel Javax.swing.JComponent isEmpty(): boolean getSize(): int addElement(Object) removeElement(Object) elementAt(): Object removeElementAt(int) insertElementAt(int) indexOf(Object): int etc JList Example: JList A ListModel is an object which maintains a list of objects: it knows how big the list is, which object is where in the list, etc, and can insert/remove objects A JList is a graphical component giving a view of the list and able to receive user input to modify the list Lecture17

Java Creating a Model (List and Combo Box) To create a list model: DefaultListModel — everything is pretty much taken care of for you. ListModel — create a new class which implements the ListModel interface and implement the getSize and getElementAt methods To create a combo box model: DefaultComboBoxModel — everything is pretty much taken care of for you. ComboBoxModel — create a new class which implements the ComboBoxModel interface and implement the setSelectedItem and getSelectedItem methods Lecture17

Example: MV_ComboBoxDemo.java Java Examples Example: MV_ComboBoxDemo.java DefaultComboBoxModel: DefaultListModel: JComboBox<String> ppComboBox; ... DefaultComboBoxModel<String> cbm = new DefaultComboBoxModel<String>( new String[] { "person", "student", "employee" }); ppComboBox = new JComboBox<String>(cbm); creates a mutable list model object, puts the initial items in it, and uses the list model to create a list: JList<String> list; ... DefaultListModel<String> listModel = new DefaultListModel<String>(); listModel.addElement("red"); listModel.addElement("orange"); listModel.addElement("yellow"); list = new JList<String>(listModel); Lecture17

import java.swingx.table.*; Java Using JTable Tables are used to display data in a spreadsheet fashion So key concepts in the model of a table: cell, row, column Value(s) in each Every table gets its data from an object that implements the TableModel interface You can use the DefaultTableModel, OR You an create a new class which implements the AbstractTableModel interface and implements the following methods: getColumnName getRowCount getColumnCount getValueAt import java.swingx.table.*; Lecture17

Java Using the DefaultTableModel Example: DefaultTableModelDemo.java Model might hold its data in an array, vector or from an database Object[][] data = { {"Mary", "Campione","Snowboarding", new Integer(5) ...} {"Alison", "Huml","Rowing", new Integer(3), new Boolean(true)} ...}; String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"}; DefaultTableModel defaultTableModel = new DefaultTableModel(data, columnNames); ... table = new JTable(defaultTableModel); Lecture17

Java Using the AbstractTableModel Example: AbstractTableModelDemo.java Steps in creating and using AbstractTableModel Create an AbstractTableModel subclass Implement the getRowCount() , getColumnCount() , and getValueAt() methods JTable table = new JTable(new AbstractTableModel() { public int getColumnCount() { return 10; } public int getRowCount() { return 5;} public Object getValueAt(int row, int col) { return new Integer((row+1)*(col+1)); } }); getContentPane().add(table, BorderLayout.CENTER); extends AbstractTableModel Lecture17

Example: DefaultTableModelDemo.java Java Controller Example: DefaultTableModelDemo.java A controller that takes user input on the view and translates that to changes in the model.” Example: You do NOT directly update JTable, T Instead, you update the TableModel, TM, which automatically updates the Table, T changeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultTableModel tm = (DefaultTableModel) table.getModel(); for (int row=0; row<tm.getRowCount(); row++) { int years = (Integer) tm.getValueAt(row, 3); tm.setValueAt(years+1, row, 3); } }); Change the TableModel Lecture17

Java Example Update the ListModel addButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { String name = JOptionPane.showInputDialog(PhilosophersJList.this, "Enter Name" ); philosophers.addElement( name ); } }); Change the ListModel removeButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { philosophers.removeElement(list.getSelectedValue() ); } }); Change the ListModel Lecture17

MVC Example in Pyton QListView: Provides a selectable list of items in ListMode QComboBox: Provides a dropdown list of items to select from QTableView: provides a default model/view implementation of a table. lm = MyListModel(list_data, self) lv = QListView() lv.setModel(lm) tablemodel = MyTableModel(my_array, self) tableview = QTableView() tableview.setModel(tablemodel) Lecture17

Python Creating Models QAbstractListModel – for single-column lists QAbstractListModel provides a standard interface for models that represent their data as a simple non-hierarchical sequence of items. It is not used directly, but must be subclassed QAbstractTableModel – for tables QAbstractTableModel provides a standard interface for models that represent their data as a two-dimensional array of items. It is not used directly, but must be subclassed. It can be subclassed to create table models. Lecture17

Python QAbstractListModel Example: PhilosophersJList.py Create a new class which extends the QAbstractListModel and implement the following methods: Constructor rowCount: return the number of items in the list data:  to retrieve items from the list. class MyListModel(QAbstractListModel): def __init__(self, datain, parent=None, *args): QAbstractListModel.__init__(self, parent, *args) self.listdata = datain def rowCount(self, parent): return len(self.listdata) def data(self, index, role): if index.isValid() and role == Qt.DisplayRole: return QVariant(self.listdata[index.row()]) else: return QVariant() Lecture17

Python QAbstractTableModel Example: TableModelDemo.py Create a new class which extends the QAbstractTableModel and implement the following methods: Constructor The rowCount() and columnCount() functions return the dimensions of the table data:  to retrieve items from the table. headerData class MyTableModel(QAbstractTableModel): def __init__(self, datain, parent=None, *args): QAbstractTableModel.__init__(self, parent, *args) self.arraydata = datain def rowCount(self, parent): return len(self.arraydata) def columnCount(self, parent): return len(self.arraydata[0]) def data(self, index, role): if not index.isValid(): return QVariant() elif role != Qt.DisplayRole: return QVariant(self.arraydata[index.row()][index.column()]) Lecture17

Python Modifying Models A model can change for a number of reasons Modifying Structure Inserting and removing rows and columns must be communicated from the model, the layoutChanged signal must be emitted class MyTableModel(QAbstractTableModel): self.arraydata = ... self.layoutChanged.emit() Lecture17