2012-06-26 30 views
1

我已經找遍了顯示覆選框和還沒有找到一個例子,可以幫助我......我需要讓我的JTable的最後一列(這是由ms訪問數據庫填充)顯示爲默認未選中的複選框。我從中提取的數據庫有數百條記錄,但是出於我們的目的,我修改了代碼和數據庫以使其更簡單。我想要做的就是讓最後一列出現在Java GUI上,作爲用戶可以檢查並最終保存結果的複選框。也許JTable不是最好的方式來做到這一點?使用矢量從數據庫中填充的JTable,並在最後一列

對於這個代碼,我有一個名爲Access數據庫clientEmployee並添加四列其中最後一個被命名爲「積極」和含(訪問複選框)是/否值。

請幫助我,因爲我的任務即將到期。謝謝!

This is the main Gui..... 

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.sql.SQLException; 


public class TheMainGUI extends JFrame { 

    //Elements of Notes Tab 
    private JButton buttons5[]; 
    private JLabel labels5[]; 
    private String fldLabel5[] = {"Client","Employee"}; 
    private JPanel p1d; 
     static String sql; 

public TheMainGUI() { 

//creates the main tab pane object 
JTabbedPane jtp = new JTabbedPane(); 

//adds tabbed pane to the frame 
getContentPane().add(jtp); 

//Creats all of the tabs 

JPanel jp5 = new JPanel();//checklist tab 

//This adds the tabs to the tabbed pane and sets their titles 

jtp.addTab("Stakeholders", jp5); 


     labels5 = new JLabel[2]; 
     buttons5 = new JButton[2]; 
     p1d = new JPanel(); 

     p1d.setLayout(new GridLayout(2,4,5,5)); 

     jp5.setLayout(new FlowLayout()); 


     for(int count=0;count<buttons5.length && count<labels5.length; count++) { 
      labels5[count] = new JLabel(fldLabel5[count]); 
       buttons5[count] = new JButton(fldLabel5[count]); 

       p1d.add(buttons5[count]); 
} 
       buttons5[0].addActionListener(
     new ActionListener() { 

      //Handle JButton event if it is clicked 
      public void actionPerformed(ActionEvent event) { 

          sql = "Select * from client"; 
          DatabaseForm.dbFrame(); 
          //setVisible(false);   
      } 
     } 
     ); 
       buttons5[1].addActionListener(
     new ActionListener() { 

      //Handle JButton event if it is clicked 
      public void actionPerformed(ActionEvent event) { 

          sql = "Select * employee'"; 
          DatabaseForm.dbFrame(); 
          //setVisible(false);   
      } 
     } 
     ); 

      jp5.add(p1d); 
} 

    //Main method creates GUI 
    public static void main (String []args){ 
     TheMainGUI frame = new TheMainGUI();  
     frame.setTitle("Stakeholders"); 
     frame.setSize(400,500); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.setVisible(true); 
} 
    } 






This is the database form class........ 


import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.*; 
import java.sql.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.table.*; 
import javax.swing.JCheckBox; 

public class DatabaseForm extends JFrame{ 

    public ResultSet rs; 
    public Statement stmt; 
    public Connection con; 
    JButton save; 
    public Checkbox box[]; 

public DatabaseForm() { 

    Vector columnNames = new Vector(); 
    Vector data = new Vector(); 

    try { 
      // connects to database  
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      con =DriverManager.getConnection("jdbc:odbc:clientEmployee"); 

       //declare statment variable 
       stmt = con.createStatement(); 
       //declare result set and get query from the main gui 
       rs = stmt.executeQuery(TheMainGUI.sql); 

       //declare metadata variable 
       ResultSetMetaData md = rs.getMetaData(); 

     //set variable colmns to get column count     
     int columns = md.getColumnCount(); 

     //for loop retreives all column names from the database 
     for (int i = 1; i <= columns; i++) { 
     columnNames.addElement(md.getColumnName(i)); 
     } 


    while (rs.next()) { 


     Vector row = new Vector(columns); 

     //for loop that retreives data from the columns 
     for (int i = 1; i <= columns; i++) { 

     row.addElement(rs.getObject(i)); 
     //row.add(box[i]); 
     } 
     data.addElement(row); 


} 
      rs.close(); 
      stmt.close(); 
} 
    catch(Exception e) { 
      System.out.println(e); 
} 
    //create JTable 
      JTable table = new JTable(data, columnNames); 
    // create scroll feature 
     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 

     JPanel buttonPanel = new JPanel(); 
     // add save button 
     buttonPanel.setLayout(new FlowLayout()); 
       save = new JButton("Save"); 
        buttonPanel.add(save); 
     getContentPane().add(buttonPanel, BorderLayout.SOUTH); 

       save.addActionListener(
     new ActionListener() { 

      //Handle JButton event if it is clicked 
      public void actionPerformed(ActionEvent event) { 

        setVisible(false); 
      } 
     } 
     ); 
     } 

     static void dbFrame(){ 
      DatabaseForm frame = new DatabaseForm(); 
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
      frame.pack(); 
      frame.setTitle("Stakeholder Checklist"); 
      frame.setVisible(true); 
      frame.setLocation(450,50); 
      frame.setSize(1000,900); 
} 
} 
+2

請妥善縮進代碼,並顯示使用相關代碼:其中規定了表數據的代碼。 –

+0

要完成@JBNizet評論:刪除所有與數據庫相關的代碼,並使用一些硬編碼數據,以便我們可以運行代碼而無需設置數據庫(請參見[SSCCE.org](http://sscce.org) ) – Robin

回答

2

實現一個自定義的TableModel,它圍繞List(可能是ArrayList而非Vector)。

這裏是一個只讀樣本:http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup

確保,如果你想有一個複選框的getColumnClass返回Boolean.class最後一列。

確保您要編輯的單元格是可編輯的(isCellEditable)。

教程:

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

+0

您是否有一個從數據庫中提取數據的示例,我無法遵循該示例並將其轉換爲我的代碼。非常感謝您的寶貴時間! – Bruce

+0

我建議使用JPA來獲取POJO列表:http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#createQuery%28java.lang.String,%20java.lang。類%29 否則,如果您使用普通JDBC,則必須手動填充POJO,或者採用以數據爲中心的方法(對象列表列表),而不是面向對象的方法(POJO列表)。 – Puce