我已经搜遍了所有并且还没有找到一个可以帮助我的示例...我需要将我的JTable的最后一列(由ms访问数据库填充)显示为默认情况下未选中的复选框。我从中提取的数据库有数百条记录,但是出于我们的目的,我已经修改了代码和数据库,使其更加简单。我想要做的就是让最后一列显示在Java gui上作为用户可以检查的复选框,并且最后一步保存结果。也许JTable不是最好的方法吗?
对于此代码,我已将Access数据库命名为clientEmployee,并添加了四列,其中最后一列名为“active”,并包含yes / no值(访问中的复选框)。
请帮助我,因为我的作业即将到期。谢谢!
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);
}
}
答案 0 :(得分:2)
实现一个包装List的自定义TableModel(可能是ArrayList而不是Vector)。
如果需要复选框,请确保getColumnClass返回最后一列的Boolean.class。
确保您要编辑的单元格是可编辑的(isCellEditable)。
教程:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data