Defaulttablemodel
We have mentioned the defaulttablemodel class and explained that this class inherits the abstracttablemodel abstract class and implements the getcolumncount (), getrowcount () and getvalueat () methods. therefore, in actual use, defaulttablemodel is much simpler and more often used than abstracttablemodel. defaulttablemodel uses vector internally to use table data. If the table format to be displayed by ulttablemodel is relatively simple, we recommend that you use defaulttablemodel
Convenience is much simpler. if the data mode to be displayed by the primary node is very complex, such as the score table and Student Course Selection information, the information displayed in such tables is usually different from each other, therefore, it is easier to design acttablemodel.
The constructor of defaulttablemodel is as follows:
- Defaulttablemodel (): Creates a defaulttablemodel without any data.
- Defaulttablemodel (INT numrows, int numcolumns): Creates a defaulttablemodel with a specified number of columns.
- Defaulttablemodel (object [] [] data, object [] columnnames): Creates a defaulttablemodel. The input data format is object array. The system automatically calls setdatavector () to set data.
- DefaultTableModel (Object [] columnNames, int numRows): Creates a DefaultTableModel with the Column Header name and number of rows.
- DefaultTableModel (Vector columnNames, int numRows): Creates a DefaultTableModel with the column Header name and number of rows.
- DefaultTableModel (Vector data, Vector columnNames): Creates a DefaultTableModel. The input data format is Vector. The system automatically calls the setDataVector () method to set the data.
The defaulttablemodel class provides many useful methods, such as getcolumncount (), getrowcount (), getvalueat (), and iscelleditable () setvalueat. Defaulttablemodel also provides AddColumn () and addrow () Methods to add table data at any time. Below is an example of dynamically adding table fields:
Import java. awt. *; import java. awt. event. *; import java. util. vector; import javax. swing. *; import javax. swing. event. *; import javax. swing. table. *; public class AddRemoveCells implements ActionListener {JTable table = null; DefaultTableModel defaultModel = null; public AddRemoveCells () {JFrame f = new JFrame (); string [] name = {"Field 1", "Field 2", "Field 3", "field 4", "Field 5 "}; string [] [] data = new String [5] [5]; int Value = 1; for (int I = 0; I <data. length; I ++) {for (int j = 0; j <data [I]. length; j ++) data [I] [j] = String. valueOf (value ++);} defaultModel = new DefaultTableModel (data, name); table = new JTable (defaultModel); table. setPreferredScrollableViewportSize (new Dimension (400, 80); JScrollPane s = new JScrollPane (table); JPanel panel = new JPanel (); JButton B = new JButton ("add row "); panel. add (B); B. addActionList Ener (this); B = new JButton ("add column"); panel. add (B); B. addActionListener (this); B = new JButton ("Delete row"); panel. add (B); B. addActionListener (this); B = new JButton ("delete column"); panel. add (B); B. addActionListener (this); Container contentPane = f. getContentPane (); contentPane. add (panel, BorderLayout. NORTH); contentPane. add (s, BorderLayout. CENTER); f. setTitle ("AddRemoveCells"); f. pack (); f. setVisible (true); f. addWindowListener (New WindowAdapter () {public void windowClosing (WindowEvent e) {System. exit (0) ;}}) ;}/ ** to delete a column, you must use the removeColumn () method defined on the TableColumnModel interface. Therefore, I first obtain the * TableColumnModel object from the getColumnModel * () method of the JTable class, and then retrieve the TableColumn * of the column to be deleted from the getColumn () method of the TableColumnModel *. this TableColumn object is treated as a * removeColumn () parameter. After this column is deleted, you must reset the number of columns, that is, use the setColumnCount * () method of DefaultTableModel. */Public void actionreceivmed (ActionEvent e) {if (e. getActionCommand (). equals ("add column") defaultModel. addColumn ("add column"); if (e. getActionCommand (). equals ("add row") defaultModel. addRow (new Vector (); if (e. getActionCommand (). equals ("delete column") {int columncount = defaultModel. getColumnCount ()-1; if (columncount> = 0) // if columncount <0, no columns exist. {TableColumnModel columnModel = table. getColumnModel (); TableColumn tableColumn = columnModel. getColumn (columncount); columnModel. removeColumn (tableColumn); defaultModel. setColumnCount (columncount) ;}} if (e. getActionCommand (). equals ("Delete row") {int rowcount = defaultModel. getRowCount ()-1; // getRowCount returns the number of rows. rowcount <0 indicates that no rows exist. If (rowcount> = 0) {defaultModel. removeRow (rowcount); defaultModel. setRowCount (rowcount); // you can use the removeRow () method of DefaultTableModel to delete rows. After deleting a row, you must reset the number of columns, that is, use the setRowCount () method of DefaultTableModel .}} Table. revalidate ();} public static void main (String args []) {new AddRemoveCells ();}}