Presentation is loading. Please wait.

Presentation is loading. Please wait.

CompSci 280 S Introduction to Software Development

Similar presentations


Presentation on theme: "CompSci 280 S Introduction to Software Development"— Presentation transcript:

1 CompSci 280 S2 2107 Introduction to Software Development
GUI 2

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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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


Download ppt "CompSci 280 S Introduction to Software Development"

Similar presentations


Ads by Google