public void init() { for (int i=0;i<5;i++) model.addColumn("column "+i);
for (int row =0;row<10;row++) { for (int col=0;col<5;col++) { data[col]="cell"+row+","+col; } model.addRow(data); } contentpane.add(jsp,BorderLayout.CENTER); contentpane.add(p,BorderLayout.SOUTH); }
class pane extends JPanel implements ActionListener { JButton b1=new JButton("add row"), b2=new JButton("add col"); public pane() { add(b1); add(b2); b1.setToolTipText(" Add Row Button"); b2.setToolTipText(" Add Col Button"); b1.addActionListener(this); b2.addActionListener(this); }
public void actionPerformed(ActionEvent e) { if (e.getSource()==b1) { int row =model.getRowCount(); int col =model.getColumnCount(); Object object[]=new Object[col]; for (int i=0;i<col;i++) object[i]="cell"+row+","+i; model.addRow(object); } else if (e.getSource()==b2) { int row =model.getRowCount(); int col =model.getColumnCount();
model.addColumn("column"+col); for (int ii=0;ii<row;ii++) model.setValueAt("cell"+ii+","+col,ii,col);
class Ta extends AbstractTableModel{…………}
Ta mt=new Ta();
JTable t=new JTAble(mt);
JComboBox………………
t.getColumnModel().………………
自己写一个监听程序用于刷新
model.addRow(Vector,Vector)
还有好多方法查doc吧
// header - edit "Data/yourJavaAppletHeader" to customize
// contents - edit "EventHandlers/Java Applet/onCreate" to customize
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;public class ex4 extends JApplet //implements ActionListener
{
Object []data=new Object[5];
DefaultTableModel model=new DefaultTableModel();
JTable jtable=new JTable(model);
Container contentpane=getContentPane();
JScrollPane jsp=new JScrollPane(jtable);
pane p=new pane();
public void init()
{
for (int i=0;i<5;i++)
model.addColumn("column "+i);
for (int row =0;row<10;row++)
{
for (int col=0;col<5;col++)
{
data[col]="cell"+row+","+col;
}
model.addRow(data);
}
contentpane.add(jsp,BorderLayout.CENTER);
contentpane.add(p,BorderLayout.SOUTH);
}
class pane extends JPanel implements ActionListener
{
JButton b1=new JButton("add row"),
b2=new JButton("add col");
public pane()
{
add(b1);
add(b2);
b1.setToolTipText(" Add Row Button");
b2.setToolTipText(" Add Col Button");
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)
{
int row =model.getRowCount();
int col =model.getColumnCount();
Object object[]=new Object[col];
for (int i=0;i<col;i++)
object[i]="cell"+row+","+i;
model.addRow(object);
}
else if (e.getSource()==b2)
{
int row =model.getRowCount();
int col =model.getColumnCount();
model.addColumn("column"+col);
for (int ii=0;ii<row;ii++)
model.setValueAt("cell"+ii+","+col,ii,col);
jtable.sizeColumnsToFit(0);
}
}
}
}