2013-05-11 55 views
0

我有一個JTable。我已經在這樣的方法中添加了列。填充JTable時重複的值

private void createSearchResultTable() { 
    DefaultTableColumnModel columnModel = new DefaultTableColumnModel(); 
    String columnNames[] = {"Title", "Author", "Edition", "Availability", "Reserve"}; 

    for (int i = 0; i < columnNames.length; i++) { 
     TableColumn column = new TableColumn(); 
     column.setHeaderValue(columnNames[i]); 
     columnModel.addColumn(column); 
    } 
    tblBookSearchResults.setColumnModel(columnModel); 

    ButtonColumn buttonColumn = new ButtonColumn(tblBookSearchResults, reserveBook, 4); 
    buttonColumn.setMnemonic(KeyEvent.VK_ENTER); 
} 

enter image description here

現在,我從MySQL數據庫中檢索的數據填充的JTable。

private boolean populateSearchResultTable(String title, String author, String publisher) { 
    con = DatabaseHandler.connectToDb(); 
    try { 
     if (title.trim().length() != 0) { 
      pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE title LIKE ? "); 
      pst.setString(1, "%" + title + "%"); 
     } 
     else if (author.trim().length() != 0) { 
        // Say, this query is getting executed 
      pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE author LIKE ? "); 
      //pst.setString(1, "%" + author + "%"); 
        pst.setString(1, "Dan"); 
     } 
     else if (publisher.trim().length() != 0) { 
      pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE publisher LIKE ? "); 
      pst.setString(1, "%" + publisher + "%"); 
     } 
     rs = pst.executeQuery(); 
     int rowNum = 0; 
     while (rs.next()) {     
      tblBookSearchResults.setValueAt(rs.getString(1), rowNum, 1); 
     } 
     return true; 
    } catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); 
    } 
    finally { 

    } 
    return false; 
} 

數據集被檢索沒有問題,但是當我將值設置爲JTable時,它看起來像這樣。

enter image description here

的第一個值被重複中的所有列。我無法弄清楚爲什麼會發生這種情況?任何建議如何糾正這將不勝感激。

謝謝。

+0

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。硬編碼一些數據。 – 2013-05-11 10:31:12

回答

2

更新JTable時不要使用JTable#setValue,而應該在模型中添加新行或修改現有行。

而且,你不遞增rowNum值,所以你總是與表的第一行

簡單的例子交互

使用一個Swing Timer添加一個簡單的例子新一行到模型...

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.table.DefaultTableModel; 

public class TestTableModel01 { 

    public static void main(String[] args) { 
     new TestTableModel01(); 
    } 

    public TestTableModel01() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       final DefaultTableModel model = new DefaultTableModel(new Object[]{"A", "B", "C", "D", "E"}, 0); 
       JTable table = new JTable(model); 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JScrollPane(table)); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

       Timer timer = new Timer(500, new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         if (model.getRowCount() < 100) { 
          int row = model.getRowCount(); 
          model.addRow(new Object[]{ 
           row + "x" + 0, 
           row + "x" + 1, 
           row + "x" + 2, 
           row + "x" + 3, 
           row + "x" + 4 
          }); 
         } else { 
          ((Timer)(e.getSource())).stop(); 
         } 
        } 
       }); 
       timer.start(); 
      } 
     }); 
    } 
} 
+0

我對Java很陌生,所以我不太熟悉你提到的這個模型。你能否詳細說明一下?並感謝您指出增加的錯誤。我糾正了,但現在看起來像[this](http://i.imgur.com/HP08Htz.png) – Isuru 2013-05-11 10:43:31

+0

請看[如何使用表格](http://docs.oracle.com/javase/教程/ uiswing /組件/ table.html)。 Swing採用MVC(模型,控件,視圖)範例的形式,將視圖/控件從模型中分離出來 – MadProgrammer 2013-05-11 10:54:23

+0

嗨!我嘗試了你的例子的幫助。數據正在加載,除了將表格添加到「JFrame」部分有一個問題。在上面的代碼示例中,它創建了一個全新的'JFrame',對吧?我想把表添加到現有的'JFrame'中。 [Here's](http://pastebin.com/DCMjYdqw)我到目前爲止的代碼。目前只是表格顯示減去表格。 – Isuru 2013-05-13 17:46:27