1

我已经搜索了所有内容,但还没有找到可以帮助我的示例...我需要使我的 JTable 的最后一列(由 ms 访问数据库填充)显示为默认未选中的复选框。我从中提取的数据库有数百条记录,但是出于我们的目的,我已将代码和数据库修改为更简单。我想要做的就是让最后一列作为复选框出现在 Java gui 上,用户可以检查并最终保存结果。也许 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);
}   
}
4

1 回答 1

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/示例/ParticipantTableModel.java?revision=13&view=markup

如果需要复选框,请确保 getColumnClass 返回最后一列的 Boolean.class。

确保要编辑的单元格是可编辑的 (isCellEditable)。

教程:

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

于 2012-06-26T15:08:00.380 回答