JTables
JTables
Creating a Simple JTable
- Create a String array that contains the headings that you would like to see at the top of the JTable
- Initialize all the entries into a double dimension object array (Will have to constantly edit this list. Instead of the AotearoaAccommodation Array, use a double dimension object array
- Create a JTable using (object data array, column name).
- Can also use JTable (row data, column data) or JTable (Vector rowData, Vector columnData)
- These constructors automatically make every single text editable
- They treat everything as a string, so instead of showing a check box or something, it'll end up showing "true" or "false"
- They force you to put all your data into an array or a vector.
Adding an Array to A Container
- Create a JScrollPane with the table in the brackets. (i.e.: JScrollPane scrollPane = new JScrollPane(table);
- Then, setFillsViewPort(true);
- JScrollPane is telling Java to add the table to the scroll pane.
- setFillsViewPort(true) tells Java to allow drag and drop (by expanding or decreasing column lengths).
User Selections
- In the normal config, it is totally fine with multiple selections I don't see how I need this in the program though. It's not like the program can sort by 2 headings.
Creating a A Table Model
- All table models use a table model object.
- If the user does not specify what type of model object, java automatically creates the DefaultTableModel.
- Simple Model Object vs Table Demo (Simple JTable)
- The Simple Model Object has the ability to recognize what is a number, what is a boolean and what a string is. In this case, we can represent a boolean by a JCheckBox and sort by numbers unlike in the previous table example.
- Simple Table does not let you edit the table headings. You can do this in the Simple Model Object (not sure if I want it to though).
Listening for Data Changes
- Can use the TableModelListener
- Notified when the data in the table is changed (if I set it to uneditable, it shouldn't be changed by the user).
Data Change Events
- Your class must invoke one of the data change events:
- fireTableCellUpdated: Update of one specific cell
- fireTableRowsUpdated: Update of one row
- fireTableDataChanged: Update of entire table (data only)
- fireTableRowsInserted: New rows added
- fireTableRowsDeleted: Deleted row
- fireTableStructureChanged: Invalidate structure and table (including data)
Editors and Renderers
- Each cell in a table is ! a different component
- Single Cell Renderer usually creates all cells that are that type of data.
- Boolean: JCheckBox (check mark if true)
- Number: JTextField (right aligned)
- Double/Float: JTextField (but using NumberFormat from Java APIs).
- Date: JTextField (rendered by a DateFormat)
- ImageIcon: Centered JLabel
- Object: JLabel with string's value.
Tool Tips for Cells
- By default, it depends on the cell renderer (we can override JTable implement of getToolTipText(Mouse event)
- Override the getToolTipText(MouseEvent e) located in the java tutorials
- To get the value of one cell, the method is getValueAt (row index, column index);
Specifying Tool Tips for Table Headers
- For all headers: .getTableHeader().setToolTipText("Enter text here") will make this the default for all table headings
- Customization
- Create an array of what you want the tool tip headings to be.
- When you create the JTable, use code from tutorials
Sorting
- Easiest Way: .setAutoCreateRowSorter(true);
- More control over sorting: Declare a TableRowSorter just for the table.
- TableRowSorter looks at rows in the following order:
- Checks to see if the programmer has invoked a comparator..and if they have, then to defaultly use that one.
- Returns String.class: Checks to see if they are strings (and if they are, sorts it based on the "locale")
- Returns Comparable.class: Sorts string representations based on locale.
- If a string converter has been used to convert it into a different type, sort the string "mutant"
- If none of the above, sort by toString.
Using a ComboBox Inside The Table
- Just create a ComboBox, and to add things, there is an addItem class.
Validating User Entered Text
- Default
- Error Checking is free for everything but String and Object. This should be good enough, though.
- Error ends up being just a side effect of attempting to convert that object into something else.
- The text turns red and you cannot move anywhere.
- Customization
No comments:
Post a Comment