The JTable controls how the data is displayed, but the TableModel (table model) controls the data itself. So to create a JTable will typically create a TableModel first. It can be implemented TableModel interface completely, but is usually simpler to inherit the kind of help AbtractTableModel:
DataModel contains an array of data, but can also get data from another source like a database. The constructor adds a TableModelListener that prints the array each time the table is modified. The rest of the methods follow the naming conventions Beans, and are used by JTable when it wants to present the information in DataModel. AbstractTableModel provides methods default for setValue () and isCellEditable () that prevents changes in data, so if you will be editing the data layers, should overwrite these methods.
Once you have a TableModel, you just need to handle the JTable constructor. It should take care of all the details of deployment editing and updating. This example also sets the JTable in JScrollPane.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JavaExampleTableImageInJApplet extends JApplet
{
String[] Clmns = {"Burger","Avail","Rate","Date","Picture"};
Date Dt = (new GregorianCalendar(2000,9,2)).getTime();
Object[][] Dta = {
{"Chili", new Boolean(false), new Float(4.99), Dt, new ImageIcon("table.jpg")},
{"BBQ", new Boolean(true), new Float(5.99), Dt, new ImageIcon("table.jpg")},
{"Roman", new Boolean(false), new Float(4.99), Dt, new ImageIcon("table.jpg")},
{"Watercress", new Boolean(true), new Float(4.99), Dt, new ImageIcon("table.jpg")},
{"Cheese", new Boolean(false), new Float(4.99), Dt, new ImageIcon("table.jpg")},
{"Beefed", new Boolean(true), new Float(4.99), Dt, new ImageIcon("table.jpg")}
};
JTable Tbl = new JTable(new newModel(Dta,Clmns));
public void init()
{
getContentPane().add(new JScrollPane(Tbl));
}
}
class newModel extends DefaultTableModel
{
public newModel(Object[][] DATA, Object[] Clmn)
{
super(DATA,Clmn);
}
public boolean isCellEditable(int row, int Clmn)
{
return false;
}
public Class getColumnClass(int Clmn)
{
Vector Vctr = (Vector) dataVector.elementAt(0);
return Vctr.elementAt(Clmn).getClass();
}
}
/*
<APPLET CODE=JavaExampleTableImageInJApplet.class WIDTH=610 HEIGHT=290> </APPLET>*/