Advanced Swing Tables.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

MS® PowerPoint.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Graphic User Interfaces Layout Managers Event Handling.
Jeopardy Multiple Choice Fill in the Blank Modified T/F Vocabulary Positioning of data Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400.
Using Macros and Visual Basic for Applications (VBA) with Excel
Advanced Swing. Advanced Layout Managers GridBagLayout – Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout) BoxLayout – Example: Web.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
Tutorial 7: Using Advanced Functions and Conditional Formatting
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
Excel Web App By: Ms. Fatima Shannag.
Tutorial 8: Working with Advanced Functions
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Adobe Dreamweaver CS3 Revealed CHAPTER FIVE: USING HTML TABLES TO LAY OUT A PAGE.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
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 SE 8 for Programmers, Third Edition
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.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
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.
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.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Excel Web App By: Ms. Fatima Shannag.
Microsoft ® Office Access ® 2007 Training Datasheets III: Make data easier to read by formatting columns and rows ICT Staff Development presents:
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
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.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing.
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
CompSci 280 S Introduction to Software Development
A First Look at GUI Applications Radio Buttons and Check Boxes
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Creating LOVs and Editors
The practice of JTable in swing
Positioning Objects with CSS and Tables
Building a User Interface with Forms
Advanced Swing Trees.
CS3015 Beacon Module 4 Messenger & Setting Preferences
Lecture 27 Creating Custom GUIs
Advanced Swing Lists.
CompSci 280 S Introduction to Software Development
Chapter 3 Fundamentals of Programming in Visual Basic 3
Chapter 13: Advanced GUIs and Graphics
Design Patterns A Case Study: Designing a Document Editor
Android Lists and Fragments
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
Chapter 17 Creating User Interfaces
Positioning Objects with CSS and Tables
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Day 3: Working with Tables
Presentation transcript:

Advanced Swing Tables

Contents A Simple Table Table Models Working with Rows and Columns Cell Rendering and Editing

I. A Simple Table

JTable A JTable does not store its own data but obtains its data from a table model. The JTable class has a constructor that wraps a two-dimensional array of objects into a default model. Object[][] cells = { { "Mercury", 2440.0, 0, false, Color.YELLOW }, { "Venus", 6052.0, 0, false, Color.YELLOW }, . . . }

Supply the column names in a separate array of strings String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" }; Construct a table from the cell and column name arrays JTable table = new JTable(cells, columnNames); table.setAutoCreateRowSorter(true); JScrollPane pane = new JScrollPane(table);

II. Table Models The AbstractTableModel class provides default implementations for most of the methods in the TableModel interface. You only need to supply three methods: public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column); The getColumnName method of the AbstractTableModel names the columns A, B, C, and so on. To change column names, override the getColumnName method.

Construct a table that shows some computed values, namely, the growth of an investment under different interest rate scenarios. Number of rows: Number of years Number of columns: determined from minimal rate and maximal rate

III. Working with Rows and Columns Column Classes To give the table more information about the column types Class<?> getColumnClass(int columnIndex) The JTable class uses this information to pick an appropriate renderer for the class Type Rendered As Boolean Checkbox Icon Image Object String

Display images of planets in TablePlanet program Insert one more column: images of planets Create a DefaultTableModel object that overwrite the method getColumnClass Return the class that describe the column type.

Accessing Table Columns The JTable class stores information about table columns in objects of type TableColumn. A TableColumnModel object manages the columns. TableColumn column = table.getColumnModel(). getColumn(columnIndex);

void setPreferredWidth(int width) void setMinWidth(int width) Resizing Columns The TableColumn class gives you control over the resizing behavior of columns void setPreferredWidth(int width) void setMinWidth(int width) void setMaxWidth(int width) void setResizable(boolean resizable) void setWidth(int width) void setAutoResizeMode(int mode)

Resizing Rows Row heights are managed directly by the JTable class. table.setRowHeight(height); // All rows table.setRowHeight(row, height); // a row table.setRowMargin(margin);

table.getSelectionModel().setSelectionMode(mode); Selecting Rows, Columns, and Cells Depending on the selection mode, the user can select rows, columns, or individual cells in the table. By default, row selection is enabled. Clicking inside a cell selects the entire row. Row Selection void setRowSelectionAllowed(boolean allowed) To set selection mode Retrieve the selection model and use its setSelectionMode method table.getSelectionModel().setSelectionMode(mode);

mode is one of the three values: ListSelectionModel.SINGLE_SELECTION ListSelectionModel.SINGLE_INTERVAL_SELECTION ListSelectionModel.MULTIPLE_INTERVAL_SELECTION

Column Selection Column selection is disabled by default. You turn it on with the call table.setColumnSelectionAllowed( true) Cell Selection Enabling both row and column selection is equivalent to enabling cell selection. The user then selects ranges of cells. You can also enable that setting with the call table.setCellSelectionEnabled(tr ue)

removeColumn(TableColumn column); Hiding and Displaying Columns The removeColumn method of the JTable class removes a column from the table view. The column data are not actually removed from the model—they are just hidden from view. removeColumn(TableColumn column); If you have the column number (for example, from a call to getSelectedColumns), you need to ask the table model for the actual table column object: TableColumnModel columnModel = table.getColumnModel(); TableColumn column = columnModel.getColumn(i); table.removeColumn(column);

Cell Rendering and Editing The column type determines how the cells are rendered. There are default renderers: Boolean → A checkbox Icon → An Image All other types → A string , otherwise we need to install a custom renderer. Table cell renderers are similar to the list cell renderers. They implement the TableCellRenderer interface.

To tell the table to use this renderer with all objects of a type: public interface TableCellRenderer { Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column); } That method getTableCellRendererComponent is called when the table needs to draw a cell. You return a component whose paint method is then invoked to fill the cell area. To tell the table to use this renderer with all objects of a type: setDefaultRenderer(Class columnClass, TableCellRenderer renderer)

To choose an appropriate renderer for the header value: Rendering the Header To display an object (icon ...) in the header, call setHeaderValue of TableColumn: setHeaderValue(Object headerValue) To choose an appropriate renderer for the header value: setHeaderRenderer(TableCellRenderer headerRenderer)

public boolean isCellEditable(int r, int c) Cell Editing To enable cell editing, the table model must indicate which cells are editable by defining the isCellEditable method. public boolean isCellEditable(int r, int c) Most commonly, you will want to make certain columns editable. In the example program, we allow editing in four columns. public boolean isCellEditable(int r, int c) { // return true if c satisfied some condition }

A DefaultCellEditor can be constructed with a JTextField, a JCheckBox, or a JComboBox. Call setCellEditor to set an DefaultCellEditor object to a TableColumn object The JTable class automatically installs a checkbox editor for Boolean cells and a text field editor for all editable cells that don't supply their own renderer.

Ex: Set a combo box to a TableColumn object JComboBox moonCombo = new JcomboBox(); for (int i = 0; i <= 20; i++) moonCombo.addItem(i); TableColumnModel columnModel = table.getColumnModel(); TableColumn moonColumn = columnModel.getColumn(columnIndex); moonColumn.setCellEditor( new DefaultCellEditor(moonCombo));

Custom Editors Extend the AbstractCellEditor class and implement the TableCellEditor interface. Define the getTableCellEditorComponent method to supply a component. Define the shouldSelectCell, stopCellEditing, and cancelCellEditing methods to handle the start, completion, and cancellation of the editing process. The stopCellEditing and cancelCellEditing methods should call the superclass methods to ensure that listeners are notified. Define the getCellEditorValue method to return the value that is the result of the editing process.

To tell the table to use the editor with all objects of a type: setDefaultEditor(Class columnClass, TableCellEditor editor)