Like a tree, tables are quite large and powerful in swing. They were originally intended to be designed as a "grid" database interface with Java database links (JDBC, introduced in Chapter 15) as media, and therefore they have great flexibility so that we no longer feel complex. There is no doubt that this is enough to become a mature spreadsheet of the basic conditions and may provide a good basis for the entire book. However, if we understand the underlying condition of this, it is also possible to create a related simple jtable. The
JTable controls how the data is displayed, but TableModel controls its own data. So before we create jtable, we should create a tablemodel. We can execute the TableModel interface in its entirety, but it usually simply inherits from the Abstracttablemodel of the helper class:
: Table.java//Simple demonstration of JTable package c13.swing;
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import javax.swing.table.*;
Import javax.swing.event.*; The TableModel controls all the Data:class Datamodel extends Abstracttablemodel {object[][] data = {{"One", "t
Wo "," three "," four "}, {" Five "," six "," seven "," eight "}, {" Nine "," Ten "," Eleven "," Twelve "},}; Prints data when table Changes:class TML implements Tablemodellistener {public void tablechanged (Tablemodeleven T e) {for (int i = 0; i < data.length i++) {for (int j = 0; J < Data[0].length; J +) System
. Out.print (Data[i][j] + "");
System.out.println ();
}} Datamodel () {Addtablemodellistener ()} (New TML ());
public int getColumnCount () {return data[0].length;
public int GetRowCount () {return data.length; Public Object getvalueat (int row, int col) {return data[row][col];
public void setValueAt (Object val, int row, int col) {Data[row][col] = val;
Indicate the change has happened:firetabledatachanged ();
public boolean iscelleditable (int row, int col) {return true;
}
};
public class Table extends JPanel {public table () {setlayout (New BorderLayout ());
JTable table = new JTable (new Datamodel ());
JScrollPane ScrollPane = jtable.createscrollpanefortable (table);
Add (ScrollPane, borderlayout.center);
public static void Main (String args[]) {show.inframe (New Table (), 200,200); }
} ///:~
Datemodel includes a set of data, but we can also get data from other places, such as from the database. The builder adds a tablemodellistener used to print the set after each table has been changed. The remaining methods follow the bean's naming convention and are invoked when jtable needs to display information in Datemodel. Abstracttablemodel provides the default setValueAt () and isCellEditable () methods to prevent this data from being modified, so if we want to modify this data, we must overload these methods.
Once we have a tablemodel, we just have to assign it to the JTable builder. All details about display, edit and update will be processed for us. Note that this program example also places jtable in JScrollPane, because JScrollPane requires a special jtable method.