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

Slides:



Advertisements
Similar presentations
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
PREPARED BY PETAR AGOV JTABLE. JTABLE – THE BASICS Jtable is a GUI element in Java, extending the JPane class. It’s basic purpose is to present data into.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
CSE 219 Computer Science III Graphical User Interface.
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Java SE 8 for Programmers, Third Edition
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Field Trip #19 Animations with Java By Keith Lynn.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Pertemuan ke 4 JTable.  Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components  Model The model.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
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.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Topic 13 List Boxes. JList Generates an ListSelectionEvent Event monitored by ListSelectionListener ListSelectionListener invokes method valueChanged.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
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.
Review_6 AWT, Swing, ActionListener, and Graphics.
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
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)
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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 Working with Swing.
Module 13: Swing API Object Oriented Programming(Java)
It attached with method ,class and interface etc…
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Lecture 16 More GUI programming
The practice of JTable in swing
Lecture 27 Creating Custom GUIs
CompSci 280 S Introduction to Software Development
Advanced Swing Tables.
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
A Quick Java Swing Tutorial
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

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

Outline USC CSCI 201L2/19 ▪T▪Tables ▪P▪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 201L3/19 Tables

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 201L4/19 Tables

JTable Class USC CSCI 201L5/19 Tables

JTable Example 1 import javax.swing.JFrame; 2 import javax.swing.JPanel; 3 import javax.swing.JScrollPane; 4 import javax.swing.JTable; 5 6 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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 setSize(200, 300); 27 setLocationRelativeTo(null); 28 setVisible(true); 29 } public static void main(String args[]) { 32 Test t = new Test(); 33 } 34 } USC CSCI 201L6/19 Tables

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 201L7/19 Tables

JTable Properties Example 1 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; 7 8 public class Test extends JFrame { 9 public Test() { 10 super("Table Example"); 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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 33 setSize(500, 200); 34 setLocationRelativeTo(null); 35 setVisible(true); 36 } public static void main(String args[]) { 39 Test t = new Test(); 40 } 41 } USC CSCI 201L8/19 Tables

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 201L9/19 Tables

TableModel API USC CSCI 201L10/19 Tables

Table Model Example 1 import java.awt.Color; 2 import javax.swing.*; // should add imports individually 3 4 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 }; 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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 33 setSize(500, 200); 34 setLocationRelativeTo(null); 35 setVisible(true); 36 } public static void main(String args[]) { 39 Test t = new Test(); 40 } 41 } USC CSCI 201L11/19 Tables

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 201L12/19 Tables

TableColumnModel API USC CSCI 201L13/19 Tables

Table Column Model Example 1 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 }; 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); 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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 setSize(500, 200); 38 setLocationRelativeTo(null); 39 setVisible(true); 40 } public static void main(String args[]) { 43 Test t = new Test(); 44 } 45 } USC CSCI 201L14/19 Tables

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 201L15/19 Tables

Table Row Sorter Example 1 import java.awt.Color; 2 import javax.swing.*; 3 4 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 }; DefaultTableModel tableModel = new DefaultTableModel(data, columnNames); 19 JTable tables = new JTable(tableModel); 20 TableRowSorter sorter = new TableRowSorter (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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 32 setSize(500, 200); 33 setLocationRelativeTo(null); 34 setVisible(true); 35 } public static void main(String args[]) { 38 Test t = new Test(); 39 } 40 } USC CSCI 201L16/19 Tables

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 201L17/19 Tables

Outline USC CSCI 201L18/19 ▪T▪Tables ▪P▪Program

Program ▪Create the following GUI. USC CSCI 201L19/19 Program