问题:这个程序查询到的数据不能自己动的更新.查询的结果累计在table中.请高手指点应该怎么?小弟急急急急
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*; 
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector; 
import javax.swing.table.DefaultTableModel;
import test_table.*;
public class Frame_Jtable extends JFrame { 

private Connection connection; 
DataBaseManager dbm=new  DataBaseManager();
Vector vcol = new Vector(); //列名 
Vector vrow = new Vector(); //内容
BorderLayout borderLayout1 = new BorderLayout(); 
JPanel jPanel1 = new JPanel(); 
JPanel jPanel2 = new JPanel(); 
JPanel jPanel3 = new JPanel(); 
JLabel jLabel1 = new JLabel(); 
JTextField jTextField1 = new JTextField(); 
JButton jButton1 = new JButton(); 
FlowLayout flowLayout1 = new FlowLayout(); 
FlowLayout flowLayout2 = new FlowLayout(); 
JButton jButton2 = new JButton(); 
JButton jButton3 = new JButton(); 
JButton jButton4 = new JButton(); 
GridLayout gridLayout1 = new GridLayout(); 
JScrollPane jScrollPane1 = new JScrollPane(); 
JTable jTable1 = new JTable(); 
String[] description = {"所有","kaihua","Recalcitrant","brilliant","Somnescent",
            "Timorous","brilliant","Florid","Putrescent"};
String columnNames[]={"id","CID","姓名","结果","datetime","telpho"};
JComboBox c=new JComboBox(); public Frame_Jtable() { 
try { 
for (int col = 0; col <columnNames.length; col++) { 
vcol.addElement(columnNames[col]); 
}
jbInit(); 

catch (Exception e) { 
e.printStackTrace(); 

} public static void main(String[] args) { 
Frame_Jtable frame1 = new Frame_Jtable(); 
frame1.setSize(new Dimension(600, 550)); 
frame1.show(); } 
private void jbInit() throws Exception { 
this.getContentPane().setLayout(borderLayout1); 
jPanel1.setBackground(Color.red); 
jPanel1.setLayout(flowLayout1); 
jPanel2.setBackground(Color.red); 
jPanel2.setLayout(flowLayout2); 
jPanel3.setBackground(Color.pink); 
jPanel3.setLayout(gridLayout1); 
jLabel1.setPreferredSize(new Dimension(100, 16)); 
jLabel1.setText("请输入查询条件"); 
jTextField1.setPreferredSize(new Dimension(140, 22)); 
jTextField1.setText(""); 
jButton1.setText("查询"); 
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this)); 
flowLayout1.setAlignment(FlowLayout.LEFT); 
flowLayout1.setHgap(5); 
flowLayout1.setVgap(10); 
jButton2.setText("增加"); 
jButton3.setText("删除"); 
jButton4.setText("修改"); 
flowLayout2.setHgap(30); 
flowLayout2.setVgap(5); 
this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
this.getContentPane().add(jPanel1, BorderLayout.NORTH); 
for(int i=0;i<description.length;i++)
c.addItem(description[i]);
jPanel1.add(c,BorderLayout.WEST);
jPanel1.add(jLabel1, BorderLayout.CENTER); 
jPanel1.add(jTextField1, null); 
jPanel1.add(jButton1, BorderLayout.EAST); 
this.getContentPane().add(jPanel2, BorderLayout.SOUTH); 
jPanel2.add(jButton2, null); 
jPanel2.add(jButton3, null); 
jPanel2.add(jButton4, null); 
this.getContentPane().add(jPanel3, BorderLayout.CENTER); 
jPanel3.add(jScrollPane1, null); 
jScrollPane1.getViewport().add(jTable1, null); 
} //模拟查询数据库 
void jButton1_actionPerformed(ActionEvent e) { 

try { 
//制作表  
  DefaultTableModel dtm = new DefaultTableModel(vrow, vcol); 
dtm.fireTableDataChanged();

jTable1 = new JTable(vrow, vcol);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//设置表格调整尺寸模式
jTable1.setCellSelectionEnabled(false);
jTable1.setShowVerticalLines(true);//设置是否显示单元格间的分割线
jTable1.setToolTipText("显示所有的结果");     
jTable1.setShowHorizontalLines(true);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //滚动条设置左右滚 
jScrollPane1.getViewport().add(jTable1, null); //在滚动条中放入表 
//Object rs=dtm.getValueAt(3,4);
//System.out.println(rs);
Container cont = getContentPane(); 
//cont.add( jScrollPane1, BorderLayout.CENTER ); 
//cont.remove(1);
// 刷新Table
cont.validate(); 
// 最后刷新JTable 控件
this.jTable1.repaint();
this.jTable1.updateUI();
String sqb;
if(c.getSelectedItem().equals("所有")){
sqb="select * from kaihua_table";
}else{
sqb="select * from kaihua_table where "
  + c.getSelectedItem()+ "=" +"'"+jTextField1.getText()+"'";
}
//System.out.println(sqb);     
/*
for (int row = 1; row < 101; row++) { 
Vector vr1 = new Vector(); 
for (int col = 1; col < 31; col++) { 
vr1.addElement(row + "/" + col); 

vrow.addElement(vr1); 
} */

//////////////////////////////////////////////////////////// display(sqb);
//Object rs=dtm.getValueAt(3,4);
//System.out.println(rs);
//dtm.removeRow(3);
}catch (Exception ex) { 
JOptionPane.showMessageDialog(null, ex); 
} } 
public void display(String sql1){
   try{ 
  
    dbm.createConnection();
    ResultSet r=dbm.qeury(sql1);
//   定位到达第一条记录
    boolean moreRecords = r.next(); 
//     如果没有记录,则提示一条消息
    if ( ! moreRecords ) { 
          JOptionPane.showMessageDialog( this, "结果集中无记录" ); 
          setTitle( "无记录显示" ); 
          return; 
     } 
 while(r.next()){     
   Vector rvc_vector=new Vector();
    // System.out.println("22212355545");
     rvc_vector.addElement(r.getString(1));
     rvc_vector.addElement(r.getString(2));
     rvc_vector.addElement(r.getString(3));
     rvc_vector.addElement(r.getString(4));
     rvc_vector.addElement(r.getString(5));
     rvc_vector.addElement(r.getString(6));
     vrow.addElement(rvc_vector);
    
            //System.out.println("12222221111111111");   
 }
 
dbm.closeConnection();
}catch(Exception e){}
}} 
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener { 
Frame_Jtable adaptee;  Frame1_jButton1_actionAdapter(Frame_Jtable adaptee) { 
this.adaptee = adaptee; 
}  public void actionPerformed(ActionEvent e) { 
adaptee.jButton1_actionPerformed(e); 

}

解决方案 »

  1.   

    你可以单独定义一个panel类,用于显示数据,然后每次查询都添加一次panel,对原先的panel进行覆盖就可以了。其中显示数据的panel类要接收一个数组参数,数组的内容就是一次查询所要显示的相关数据。
      

  2.   

    在同一个表里更新数据要用TableModel
      

  3.   

    自己写一个 TableModel
    这个TableModel里封装了你自己的Model类,比如可以是一个Collection然后用这个TableModel构建 JTable,JTable的数据显示是根据TableModel来的
    简单说JTable是视图,TableModel是模型你自己所作的只是更新TableModel所引用的Collection,然后在需要的时候调用JTable的updateUI()就可以了
      

  4.   

    同意 leafxx的   ,extends AbstractTableModel 也可以做到的。