Working with Table models
The back end is modified earlier. Now look at the front end. The GUI application displays events in the table, and if there are no models, the task is tedious. To avoid distractions when discussing the real interface, let's look at how these models work, so it's clear what's going on back there.
What is a table model?
If you've ever worked as a programmer, and I tell you that you need to display information from a table, you know that you need to take a series of steps. You need to schedule different rows and columns, record which row represents which object, and so on. If you also want to edit the table, the list of tasks to complete is longer.
The problem is that the series of activities are fixed. If you know that there are several places to do the same thing in your application, you may decide to establish an automated process to streamline your work. Of course, it's no surprise that others have done so. In fact, the concept of this model is part of swing, which is the Java API that makes up the GUI.
The application uses the Abstracttablemodel object to tell the GUI how to perform actions, such as populating the table and responding to changes to the data. The model is set when the table is created (see Listing 15).
Listing 15. Using the table model
...
this.eventModel = new EventTableModel();
reloadTableModel();
final JTable eventTable = new JTable(this.eventModel);
tablePanel.add(new JScrollPane(eventTable), BorderLayout.CENTER);
...
These behaviors are defined in the Eventtablemodel class, so the Eventtable object immediately knows what to do. Let's take a look at this class.
Defining tables
The first step is to define the class as a subclass of Abstracttablemodel and to define the structure of the table itself, as shown in Listing 16.
Listing 16. Defining the table itself
import java.sql.Connection;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
public class EventTableModel extends AbstractTableModel {
String[] columns = { "Title", "Description", "Reminders To" };
EventClass[] events;
public int getRowCount() {
if (events != null) {
return events.length;
}
return 0;
}
public int getColumnCount() {
return columns.length;
}
public String getColumnName(int col) {
return columns[col];
}
}
Eventclass[] Array represents the data to be displayed by the table. The number of rows is the size of the ZO array. The column is defined with a columns array.
Read and write Data
After the Java code starts to display the table, you need to know what data each cell displays, as shown in Listing 17.
Listing 17. Provide data for a cell
...
Public String getcolumnname (int col) {
return Columns[col];
}
Public Object getvalueat (int rowIndex, int columnindex) {
if (events!= null) && (RowIndex < Events.length)
&& (ColumnIndex < columns.length)) {
Switch (columnindex) {
Case 0:
return Events[rowindex].gettitle ();
Case 1:
return Events[rowindex].getdescription ();
Case 2:
return Events[rowindex].getremindersto ();
Default
return null;
}
} else {
return null;
}
}
public boolean iscelleditable (int rowIndex, int columnindex) {
return true;
}
public void setValueAt (Object avalue, int rowIndex, int columnindex) {
if (events!= null) && (RowIndex < Events.length)
&& (ColumnIndex < columns.length)) {
Switch (columnindex) {
Case 0:
Events[rowindex].settitle (Avalue.tostring ());
Break
Case 1:
Events[rowindex].setdescription (Avalue.tostring ());
Break
Case 2:
Events[rowindex].setremindersto (Avalue.tostring ());
Break
}
}
}
}
If the cell is editable-you can use row and column information to set the property for each cell--gui you need to know what to do if the user edits the cell. Here the GUI sets the appropriate data for the appropriate EventClass object.