2013-04-23 83 views
0

我想在表中添加JButton。我正在使用表來顯示數據庫記錄。其實我想爲表格中的每條記錄添加按鈕,但是,按鈕不會顯示在表格中。它不顯示任何錯誤。請幫忙。提前致謝。將組件添加到JTable中

package addPanel; 

import java.sql.*; 
import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 

public class panelShowData extends JPanel 
{ 



    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultSet = null; 

    String url = "jdbc:mysql://localhost:3306/records"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "root"; 
    String password = ""; 

    JScrollPane scrollPane; 
    JTable table; 
    DefaultTableModel tableModel; 

    String nameSearch=""; 


    public panelShowData() 
    { 

     this.setLayout(null); 
     setVisible(true); 
     setBounds(0, 200, 500, 450); 
    } 

    public void searchData(String nameSearch) 
    { 

     tableModel = new DefaultTableModel(); 

      try 
      {   
       Class.forName(driver).newInstance(); 
       connection = DriverManager.getConnection(url, userName, password); 

       statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
            ResultSet.CONCUR_UPDATABLE); 

       resultSet = statement.executeQuery("select * from registration where firstname='" 
            + nameSearch 
           + "'or lastname ='" 
           + nameSearch + "'"); 

       System.out.println("Query executed"); 
       System.out.println("nameSearch="+nameSearch); 

       String firstName; 
       String lastName; 
       int id; 

       JButton add=new JButton("ADD"); 

        while (resultSet.next()) 
        {  
         System.out.print(resultSet.getString(2) + "\t"); 
         System.out.print(resultSet.getString(4) + "\n"); 

         firstName = resultSet.getString(2); 
         lastName = resultSet.getString(4); 
         id = resultSet.getInt(1); 

         String[ ] columnName = { "Id","First Name", "Last Name","click" }; 
         Object[ ] data = { id, ""+firstName, "" + lastName, add }; 


         System.out.println("Names is:"+firstName); 
         tableModel.setColumnIdentifiers(columnName); 
         tableModel.addRow(data); 
         tableModel.fireTableDataChanged(); 
        } 

       table = new JTable(tableModel); 
       table.setEnabled(false); 
       scrollPane = new JScrollPane(table); 
       scrollPane.setBounds(10, 10, 350, 100); 
       scrollPane.revalidate(); 
       scrollPane.repaint(); 

       add(scrollPane); 
       connection.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       JOptionPane.showMessageDialog( null, "Record Not Found", 
           "Sorry", JOptionPane.ERROR_MESSAGE); 
      } 
    } 
} 

回答

1
  1. 在您的文章所有的代碼行是重要原因,爲什麼ResultSetTableModel,TableFromDatabase(和/或從SwingWorker的JDBC調用,可運行#線程)是否有
  2. 從未調用tableModel.fireTableDataChanged() ;,
    • XxxTableModel確定指標外
    • 的DefaultTableModel,已執行了通知,並正確
    • 您的代碼重用因爲談論了Swing中的Concurency(Oracle tutorial),所以要重寫這個通知器,關於我的觀點1。
  3. 一切重要的是有,請給read this answer about ListModel and JList,所有的點,同樣的問題,
  4. 的JPanel已FlowLayout中的API實現的,沒有理由使用NullLayout,即改變爲BorderLayout的
  5. 覆蓋的getPreferredSize合理的尺寸對於JPanel,包含JTable包裹在JScrollPane中
  6. JButton added in JTable is here solved a few times