19
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/ ~CSCI201 USC CSCI 201L

Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Embed Size (px)

Citation preview

TablesCSCI 201L

Jeffrey Miller, Ph.D.

HTTP://WWW-SCF.USC.EDU/~CSCI201

USC CSCI 201L

Outline

USC CSCI 201L 2/19

▪ Tables▪ Program

Tables Overview

▪ A table is a component that displays data in rows and columns as a two-dimensional grid

▪ If you want a table to scroll, you have to add an instance of a JTable to a JScrollPane since JTables don’t support scrolling directly

USC CSCI 201L 3/19Tables

Support Models

▪ JTable has three supporting models› Table model is used for storing and processing data› Column model represents all of the columns in the table› List-selection model is the same one used by JList for selecting rows,

columns, and cells in the table

▪ The TableColumn class contains the information about particular columns

▪ The JTableHeader class can be used to display the header of a JTable

▪ The TableCellEditor interface can be used to create a custom editor for a JTable

▪ The TableCellRenderer interface can be used to create a custom renderer for a JTable› There are default editors and renderers on a JTable, so this is optional

USC CSCI 201L 4/19Tables

JTable Class

USC CSCI 201L 5/19Tables

JTable Example1 import javax.swing.JFrame;2 import javax.swing.JPanel;3 import javax.swing.JScrollPane;4 import javax.swing.JTable;56 public class Test extends JFrame {7 public Test() {8 super("Table Example");9 10 JPanel jp = new JPanel();11 String [] columnNames = {"Tables", "# People"};12 Object [][] data = {13 {"Table 1", 4},14 {"Table 5", 6},15 {"Table 7", 4},16 {"Table 10", 3},17 {"Table 12", 4},18 {"Table 13", 6}19 };20 JTable tables = new JTable(data, columnNames);21 JScrollPane jsp = new JScrollPane(tables);22 jp.add(jsp);23 add(jp);2425 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);26 setSize(200, 300);27 setLocationRelativeTo(null);28 setVisible(true);29 }30 31 public static void main(String args[]) {32 Test t = new Test();33 }34 }

USC CSCI 201L 6/19Tables

Resizing Columns

▪ The autoResizeMode property specifies how columns are resized› JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS makes

resizing one column affect the widths of all the subsequent columns

• This is the default behavior

› JTable.AUTO_RESIZE_OFF makes resizing one column not affect the widths of the other columns

› JTable.AUTO_RESIZE_LAST_COLUMN makes resizing one column only affect the width of the last column

› JTable.AUTO_RESIZE_NEXT_COLUMN makes resizing one column affect the width of the next column

› JTable.AUTO_RESIZE_ALL_COLUMNS makes resizing a column affect the widths of all the columns

USC CSCI 201L 7/19Tables

JTable Properties Example1 import java.awt.Color;2 import javax.swing.JFrame;3 import javax.swing.JPanel;4 import javax.swing.JScrollPane;5 import javax.swing.JTable;6 import javax.swing.ListSelectionModel;78 public class Test extends JFrame {9 public Test() {10 super("Table Example");11 12 JPanel jp = new JPanel();13 String [] columnNames = {"Tables", "# People", "Open?", "Color"};14 Object [][] data = {15 {"Table 1", 4, "Yes", "Green"},16 {"Table 5", 6, "No", "Red"},17 {"Table 7", 4, "No", "Red"},18 {"Table 10", 3, "Yes", "Green"},19 {"Table 12", 4, "No", "Red"},20 {"Table 13", 6, "No", "Red"}21 };22 JTable tables = new JTable(data, columnNames);

23 tables.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);24 tables.setSelectionForeground(Color.WHITE);25 tables.setSelectionBackground(Color.RED);26 tables.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);27 tables.setGridColor(Color.BLUE);28 JScrollPane jsp = new JScrollPane(tables);29 jp.add(jsp);30 add(jp);3132 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);33 setSize(500, 200);34 setLocationRelativeTo(null);35 setVisible(true);36 }37 38 public static void main(String args[]) {39 Test t = new Test();40 }41 }

USC CSCI 201L 8/19Tables

Table Models

▪ JTable delegates data storing and processing to its table data model, implemented as the TableModel interface› A table model defines the methods for registering table model

listeners, manipulating cells, and obtaining row count, column count, column class, and column name

▪ The AbstractTableModel class provides a partial implementation of the TableModel interface with the exception of the following methods› public int getRowCount()› public int getColumnCount()› public Object getValueAt(int row, int column)

▪ The DefaultTableModel class extends the AbstractTableModel class› The data stored in the table is in a vector of objects

USC CSCI 201L 9/19Tables

TableModel API

USC CSCI 201L 10/19Tables

Table Model Example1 import java.awt.Color;2 import javax.swing.*; // should add imports individually34 public class Test extends JFrame {5 public Test() {6 super("Table Example");7 JPanel jp = new JPanel();8 String [] columnNames = {"Tables", "# People", "Open?", "Color"};9 Object [][] data = {10 {"Table 1", 4, "Yes", "Green"},11 {"Table 5", 6, "No", "Red"},12 {"Table 7", 4, "No", "Red"},13 {"Table 10", 3, "Yes", "Green"},14 {"Table 12", 4, "No", "Red"},15 };16

17 DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);18 tableModel.addColumn("Capacity");19 tableModel.addRow(new Object[]{"Table 15", 4, "Yes", "Green", 6});20 tableModel.removeRow(1);21 tableModel.setValueAt(8, 0, 4);22 JTable tables = new JTable(tableModel);23 tables.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);24 tables.setSelectionForeground(Color.WHITE);25 tables.setSelectionBackground(Color.RED);26 tables.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);27 tables.setGridColor(Color.BLUE);28 JScrollPane jsp = new JScrollPane(tables);29 jp.add(jsp);30 add(jp);3132 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);33 setSize(500, 200);34 setLocationRelativeTo(null);35 setVisible(true);36 }37 38 public static void main(String args[]) {39 Test t = new Test();40 }41 }

USC CSCI 201L 11/19Tables

Table Column Model

▪ Table column models manage columns in a table› They can be used to select, add, move, and remove table

columns

▪ A table column model must implement the TableColumnModel interface› This defines methods for registering table column model

listeners, accessing columns, and manipulating columns

▪ DefaultTableColumnModel is a concrete class that implements TableColumnModel and PropertyChangeListener› The columns are stored in a vector

USC CSCI 201L 12/19Tables

TableColumnModel API

USC CSCI 201L 13/19Tables

Table Column Model Example1 import java.awt.Color;2 import javax.swing.*;3 public class Test extends JFrame {4 public Test() {5 super("Table Example");6 7 JPanel jp = new JPanel();8 String [] columnNames = {"Tables", "# People", "Open?", "Color"};9 Object [][] data = {10 {"Table 1", 4, "Yes", "Green"},11 {"Table 5", 6, "No", "Red"},12 {"Table 7", 4, "No", "Red"},13 {"Table 10", 3, "Yes", "Green"},14 {"Table 12", 4, "No", "Red"},15 };16 17 DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);18 tableModel.addColumn("Capacity");19 tableModel.addRow(new Object[]{"Table 15", 4, "Yes", "Green", 6});20 tableModel.removeRow(1);21 tableModel.setValueAt(8, 0, 4);22 23 JTable tables = new JTable(tableModel);

24 TableColumnModel columnModel = tables.getColumnModel();25 columnModel.moveColumn(2, 4);26 columnModel.removeColumn(columnModel.getColumn(1));27 tables.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);28 tables.setSelectionForeground(Color.WHITE);29 tables.setSelectionBackground(Color.RED);30 tables.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);31 tables.setGridColor(Color.BLUE);32 JScrollPane jsp = new JScrollPane(tables);33 jp.add(jsp);34 add(jp);3536 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);37 setSize(500, 200);38 setLocationRelativeTo(null);39 setVisible(true);40 }41 42 public static void main(String args[]) {43 Test t = new Test();44 }45 }

USC CSCI 201L 14/19Tables

Sorting and Filtering

▪ To sort on any column in a JTable, create an instance of TableRowSorter with a table model and set the JTable row sorter› When the table is displayed, you will be able to sort by

clicking on a column head

▪ You can also filter rows based on one or more columns by creating a RowFilter with a regular expression› To enable filtering, you have to associate a filter with a

TableRowSorter

USC CSCI 201L 15/19Tables

Table Row Sorter Example1 import java.awt.Color;2 import javax.swing.*;34 public class Test extends JFrame {5 public Test() {6 super("Table Example");7 8 JPanel jp = new JPanel();9 String [] columnNames = {"Tables", "# People", "Open?", "Color"};10 Object [][] data = {11 {"Table 1", 4, "Yes", "Green"},12 {"Table 5", 6, "No", "Red"},13 {"Table 7", 4, "No", "Red"},14 {"Table 10", 3, "Yes", "Green"},15 {"Table 12", 4, "No", "Red"},16 };17 18 DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);19 JTable tables = new JTable(tableModel);

20 TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);21 tables.setRowSorter(sorter);22 tables.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);23 tables.setSelectionForeground(Color.WHITE);24 tables.setSelectionBackground(Color.RED);25 tables.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);26 tables.setGridColor(Color.BLUE);27 JScrollPane jsp = new JScrollPane(tables);28 jp.add(jsp);29 add(jp);3031 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);32 setSize(500, 200);33 setLocationRelativeTo(null);34 setVisible(true);35 }36 37 public static void main(String args[]) {38 Test t = new Test();39 }40 }

USC CSCI 201L 16/19Tables

More Table Details

▪ Table cells are painted by cell renderers› By default a cell object’s string representation is displayed› A custom table model must be used to enable renderers

for other types of objects, such as Boolean values (checkboxes) and images

▪ JTable does not fire table events, but it inherits MouseEvent, KeyEvent, and ComponentEvent from the JComponent class› Table events are fired by table models, table column

models, and table selection models• Table models fire TableModelEvent• Table column models fire TableColumnModelEvent• Table selection models fire ListSelectionEvent

USC CSCI 201L 17/19Tables

Outline

USC CSCI 201L 18/19

▪ Tables▪ Program

Program

▪ Create the following GUI.

USC CSCI 201L 19/19Program