Import javax.swing.JTable;
import Javax.swing.table.AbstractTableModel;
import Javax.swing.DefaultCellEditor;
import Javax.swing.JScrollPane;
import Javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
/**
* <p>title: Table of your own definition </p>
* <p>description: Inheriting Abstracttablemodel classes, implementing your own tables </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:MyTableDemo.java</p>
* @version 1.0
*/
public class Mytabledemo extends JFrame {
public Mytabledemo () {
super ("Mytabledemo");
//Declare your own form and add it to jtable
Mytablemodel MyModel = new Mytablemodel ();
JTable table = new JTable (MyModel);
table.setpreferredscrollableviewportsize (New Dimension (500, 70));
//Add a table to a scrollable panel
JScrollPane ScrollPane = new jscrollpane (table);
//Add the scroll panel to the form
Getcontentpane (). Add (ScrollPane, borderlayout.center);
//Add monitor
Addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
system.exit (0);
}
});
}
/**
* <p>title: Define your own table patterns </p>
* <p>description: Define your own table schema by inheriting the Abstracttablemodel class,
* This allows the third to be edited </p>
*/
class Mytablemodel extends Abstracttablemodel {
//Definition table header
final string[] ColumnNames = {"Name",
"Gender",
"Education",
"Age",
"is married"};
//Initialization of table data
final object[][] data = {
{"John", "Male",
"Da Ben", New Integer (a), new Boolean (False)},
{"Dick", "male",
"Da Ben", New Integer (a), new Boolean (True)},
{"Harry", "Male",
"High School", New Integer (a), new Boolean (False)},
{"Zhao", "female",
"College", New Integer (g), New Boolean (True)},
{"Zhou Da", "male",
"Da Ben", New Integer, new Boolean (FALSE)}
};
/**
*<br> Method Description: Inheritance Abstracttablemodel must implement methods
*<br> input Parameters:
*<br> return type: int column number
*/
public int getColumnCount () {
return columnnames.length;
}
/**
*<br> Method Description: Inheritance Abstracttablemodel must implement methods
*<br> input Parameters:
*<br> return type: int column number
*/
public int GetRowCount () {
return data.length;
}
/**
*<br> Method Description: Inheritance Abstracttablemodel must implement methods
*<br> input Parameters:
*<br> return type: String column name
*/
public String getcolumnname (int col) {
return Columnnames[col];
}
/**
*<br> Method Description: Inherit Abstracttablemodel methods that must be implemented to get the data in the table
*<br> input parameter: int row row number
*<br> input parameter: int col number of columns
*<br> return Type: Object Data Objects
*/
public Object getvalueat (int row, int col) {
return Data[row][col];
}
/**
*<br> Method Description: Implement this method so that the last column does not display true and false. Instead, use the check box
*<br> input Parameters:
*<br> return type:
*/
public Class getcolumnclass (int c) {
return getvalueat (0, C). GetClass ();
}
/**
*<br> Method Description: This method does not necessarily need to be implemented. This is to define editable column
*<br> input Parameters:
*<br> return Type: Boolean editable
*/
public boolean iscelleditable (int row, int col) {
if (Col < 2) {
return false;
} else {
return true;
}
}
/**
*<br> Method Description: Implements this method, obtains edits the data.
*<br> input Parameters:
*<br> return type:
*/
public void setValueAt (Object value, int row, int col) {
SYSTEM.OUT.PRINTLN ("Modify data location:" + row + "," + col
+ "New data is:" + value);
Data[row][col] = value;
firetablecellupdated (Row, col);
System.out.println ("New Tabular Data:");
Printdebugdata ();
}
/**
*<br> Method Description: Output new data in the table
*<br> input Parameters:
*<br> return type:
*/
private void Printdebugdata () {
int numrows = GetRowCount ();
int numcols = getColumnCount ();
for (int i=0 i < numrows; i++) {
System.out.print ("line" + i + ":");
for (int j=0 J < Numcols; J + +) {
System.out.print ("" + data[i][j]);
}
System.out.println ();
}
System.out.println ("--------------------------");
}
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
jframe.setdefaultlookandfeeldecorated (TRUE);
Mytabledemo frame = new Mytabledemo ();
Frame.pack ();
frame.setvisible (TRUE);
}
}