import javax.swing.*;
import java.awt.*;
import javax.swing.table.AbstractTableModel;public class Frame1 extends JFrame {
JTable jTable1 = new JTable(); public Frame1() { MyTableModel tableModel=new MyTableModel();
tableModel.setValueAt("test",0,0);
jTable1.setModel(tableModel); this.getContentPane().add(jTable1); }
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(500,150);
frame1.show();
}
}
class MyTableModel extends AbstractTableModel { private String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
private Object[][] data = {
{"Mary", "Campione","Snowboarding", new Integer(5), new Boolean(false)} ,
{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath","Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour","Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne","Pool", new Integer(10), new Boolean(false)}
}; public int getColumnCount() {
return columnNames.length;
} public int getRowCount() {
return data.length;
} public String getColumnName(int col) {
return columnNames[col];
} public Object getValueAt(int row, int col) {
return data[row][col];
} /*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
} /*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
}
else {
return true;
}
} /*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}}
import java.awt.*;
import javax.swing.table.AbstractTableModel;public class Frame1 extends JFrame {
JTable jTable1 = new JTable(); public Frame1() { MyTableModel tableModel=new MyTableModel();
tableModel.setValueAt("test",0,0);
jTable1.setModel(tableModel); this.getContentPane().add(jTable1); }
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(500,150);
frame1.show();
}
}
class MyTableModel extends AbstractTableModel { private String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
private Object[][] data = {
{"Mary", "Campione","Snowboarding", new Integer(5), new Boolean(false)} ,
{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath","Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour","Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne","Pool", new Integer(10), new Boolean(false)}
}; public int getColumnCount() {
return columnNames.length;
} public int getRowCount() {
return data.length;
} public String getColumnName(int col) {
return columnNames[col];
} public Object getValueAt(int row, int col) {
return data[row][col];
} /*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
} /*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
}
else {
return true;
}
} /*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}}
JTable table = new JTable(model);