2014-01-25 113 views
0

我想在每次添加行時顯示JTable。 我在下面寫的代碼向表中添加一個新行,然後顯示jtable 2秒,然後添加另一行,然後顯示錶。但添加的所有行如何「動畫」添加行到JTable

 Vector<Object> rowVector = null; 
     while(rs.next()){ 
      rowVector = new Vector<Object>(); 
      for(int i=1;i<=columnCount;i++){ 
       rowVector.add(rs.getString(i)); 
       //System.out.print(rs.getString(i)); 
      } 

      System.out.println(rowVector.toString()); 
      //data.add(rowVector); 
      model.addRow(rowVector); 
      JTable table1 = new JTable(model); 
      JTableHeader header1 = table1.getTableHeader(); 
      table1.setEnabled(false); 
      header1.setResizingAllowed(false); 
      header1.setReorderingAllowed(false); 
      header1.setForeground(Color.white); 
      header1.setBackground(Color.black); 
      jpanedisplay.setViewportView(table1);   
      jpanedisplay.getViewport().setBackground(Color.white); 
      jpanedisplay.setBorder(BorderFactory.createLineBorder(Color.black)); 
      Thread.sleep(3000); 

     } 

回答

1

你在每次迭代中創建一個新的JTable後JTable沒有顯示, 它只顯示,你不需要做。在循環之前將模型添加到表格中。而就行添加到模型中環路

DefaultTableModel model = new DefaultTableModel(columnNames, 0); 
JTable table = new JTable(model); 

table.setEnabled(false); 
JTableHeader header1 = table.getTableHeader(); 
header1.setResizingAllowed(false); 
header1.setReorderingAllowed(false); 
header1.setForeground(Color.white); 
header1.setBackground(Color.black); 
jpanedisplay.setViewportView(table1);   
jpanedisplay.getViewport().setBackground(Color.white); 
jpanedisplay.setBorder(BorderFactory.createLineBorder(Color.black)); 

... 

while(rs.next()){ 
    Vector<Object> rowVector = new Vector<Object>(); 
    for(int i=1;i<=columnCount;i++){ 
     rowVector.add(rs.getString(i)); 
      //System.out.print(rs.getString(i)); 
    } 
    model.addRow(rowVector); 

    // DON'T CALL Thread.sleep(); 
} 

UPDATE

如果你不想要的表添加到數據庫中的任務完成後,只需使用一個方法

private JTable createTable(ResultSet rs) { 
    DefaultTableModel model = new DefaultTableModel(columnNames, 0); 
    JTable table = new JTable(model); 

    table.setEnabled(false); 
    JTableHeader header1 = table.getTableHeader(); 
    header1.setResizingAllowed(false); 
    header1.setReorderingAllowed(false); 
    header1.setForeground(Color.white); 
    header1.setBackground(Color.black); 

    while(rs.next()){ 
     Vector<Object> rowVector = new Vector<Object>(); 
     for(int i=1;i<=columnCount;i++){ 
      rowVector.add(rs.getString(i)); 

     } 
     model.addRow(rowVector); 
    } 

    return table; 
} 

然後您可以將其添加到任何面板

panel.add(new JScrollPane(createTable(rs)); 

更新#2

後運解釋說,他們試圖得到一個動感的外觀添加了行,而不是所有在同一時間。

我用java.swing.Timer添加行。

enter image description here

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 
import javax.swing.table.DefaultTableModel; 

public class TestTimerTable { 

    String[] cols = {"ID", "Name", "Country Code", "District", "Population"}; 
    DefaultTableModel model = new DefaultTableModel(cols, 0); 
    JTable table = new JTable(model); 
    ResultSet rs = null; 
    JButton start = new JButton("Start"); 
    Timer timer = new Timer(0, null); 

    public TestTimerTable() { 
     initDB(); 

     JScrollPane scroll = new JScrollPane(table); 
     scroll.setViewportView(table); 

     JFrame frame = new JFrame("Test Timer Table"); 
     frame.add(scroll); 
     frame.add(start, BorderLayout.SOUTH); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

     timer = new Timer(200, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        if (rs.next()) { 
         String s1 = rs.getString(1); 
         String s2 = rs.getString(2); 
         String s3 = rs.getString(3); 
         String s4 = rs.getString(4); 
         String s5 = rs.getString(5); 
         model.addRow(new Object[]{s1, s2, s3, s4, s5}); 
        } else { 
         ((Timer) e.getSource()).stop(); 
        } 
       } catch (SQLException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }); 
     start.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       timer.start(); 
      } 
     }); 
    } 

    private void initDB() { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn = DriverManager.getConnection(
        "jdbc:mysql://localhost/...", "...", "..."); 
      PreparedStatement ps = conn.prepareStatement("SELECT * FROM city"); 
      rs = ps.executeQuery(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestTimerTable(); 
      } 
     }); 
    } 
} 
+0

INFACT我想添加的每一行之後,顯示JTable中 – user28681

+0

好時再表添加到面板_after_循環 –

+0

還只是一個音符,長期運行的任務,你應該看看使用像SwingWorker這樣的後臺線程。長數據庫查詢任務應該這樣完成。 –