我发现了类似的项目,但我就是无法掌握如何做到这一点的概念。我有一个名为 HealthTracker 的类,其中包含一个 JTable。然后我有另一个名为 SQL 的类,它具有所有与 sql 相关的方法。其中一种方法称为 populateTable。我可以执行查询等,但我不知道如何从 SQL 类访问位于 HealthTracker 类中的 JTable。这是我的 SQL 类的代码。
public void populateTable(int qryType){
try{
DefaultTableModel tblModel = new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
Connection dbconn = SQL.dbConn();
Statement stmt = dbconn.createStatement();
String qry = "SELECT * FROM Services";
ResultSet rs = stmt.executeQuery(qry);
int numCols = rs.getMetaData().getColumnCount();
for (int col = 1; col <= numCols; col++){
tblModel.addColumn(rs.getMetaData().getColumnLabel(col));
}
int row = 0;
while (rs != null && rs.next()){
tblModel.addRow(new Object[0]);
tblModel.setValueAt(rs.getString("ServiceID"), row, 0);
tblModel.setValueAt(rs.getString("Institution"), row, 1);
tblModel.setValueAt(rs.getString("Comments"), row, 2);
row++;
}
rs.close();
// This is the line that gives me the error
HealthTracker.this.tblMain.setModel(tblModel);
} catch (Exception e){
e.printStackTrace();
}
}
这很棒,但是我仍然需要能够从其他类访问该 JTable。当应用程序首次加载以初始化表时,会看到此代码,但用户需要能够过滤数据。因此,我创建了一个过滤器按钮,它显示另一个窗口(SearchRecord.java 类),用户可以在其中输入参数,然后单击“查找”。单击“查找”按钮后,我将运行查询,并且应该使用新结果重新加载表。也许我的处理方式是错误的?
public class SearchRecord {
private JFrame frame;
private JTextField txtInstitution;
private JTextField txtStartDate;
private JTextField txtEndDate;
// Launch the application
public static void searchForm() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SearchRecord window = new SearchRecord();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// Create the application
public SearchRecord() {
initialize();
}
// Initialize the contents of the frame
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 462, 180);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("Enter Search Parameters");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Tahoma", Font.BOLD, 12));
label.setBounds(10, 11, 414, 14);
frame.getContentPane().add(label);
JLabel lblInstitution = new JLabel("Institution: ");
lblInstitution.setBounds(10, 47, 85, 14);
frame.getContentPane().add(lblInstitution);
lblInstitution.setHorizontalAlignment(SwingConstants.RIGHT);
lblInstitution.setFont(new Font("Tahoma", Font.BOLD, 12));
txtInstitution = new JTextField();
txtInstitution.setBounds(98, 45, 326, 20);
frame.getContentPane().add(txtInstitution);
txtInstitution.setColumns(10);
JLabel lblStartDate = new JLabel("Start Date: ");
lblStartDate.setBounds(10, 78, 85, 14);
frame.getContentPane().add(lblStartDate);
lblStartDate.setHorizontalAlignment(SwingConstants.RIGHT);
lblStartDate.setFont(new Font("Tahoma", Font.BOLD, 12));
txtStartDate = new JTextField();
txtStartDate.setBounds(98, 76, 175, 20);
frame.getContentPane().add(txtStartDate);
txtStartDate.setColumns(10);
JButton button = new JButton("...");
button.setBounds(283, 76, 25, 23);
frame.getContentPane().add(button);
JButton button_1 = new JButton("...");
button_1.setBounds(283, 106, 25, 23);
frame.getContentPane().add(button_1);
JLabel lblEndDate = new JLabel("End Date: ");
lblEndDate.setBounds(10, 109, 85, 14);
frame.getContentPane().add(lblEndDate);
lblEndDate.setHorizontalAlignment(SwingConstants.RIGHT);
lblEndDate.setFont(new Font("Tahoma", Font.BOLD, 12));
txtEndDate = new JTextField();
txtEndDate.setBounds(98, 107, 175, 20);
frame.getContentPane().add(txtEndDate);
txtEndDate.setColumns(10);
JButton btnFind = new JButton("Find");
btnFind.setFont(new Font("Tahoma", Font.BOLD, 12));
btnFind.setBounds(354, 106, 70, 23);
frame.getContentPane().add(btnFind);
// Find Records
btnFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Gather all the fields from form
String[] fields = new String[3];
fields[0] = txtInstitution.getText();
fields[1] = txtStartDate.getText();
fields[2] = txtEndDate.getText();
// Refresh Table w/Filtered Data from DB
SQL loadTbl = new SQL();
try{
HealthTracker.this.tblMain.setModel(loadTbl.populateTable(0));
} catch (SQLException e1){
e1.printStackTrace();
}
}
});
}
}
最佳答案
让您的 populateTable
方法返回 TableModel
的实例,而不是让它尝试将模型应用到 JTable
的实例
public TableModel populateTable(int qryType) throws SQLException{
DefaultTableModel tblModel = new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
String qry = "SELECT * FROM Services";
try (Connection dbconn = SQL.dbConn();
Statement stmt = dbconn.createStatement();
ResultSet rs = stmt.executeQuery(qry)) {
int numCols = rs.getMetaData().getColumnCount();
for (int col = 1; col <= numCols; col++){
tblModel.addColumn(rs.getMetaData().getColumnLabel(col));
}
int row = 0;
while (rs != null && rs.next()){
tblModel.addRow(new Object[0]);
tblModel.setValueAt(rs.getString("ServiceID"), row, 0);
tblModel.setValueAt(rs.getString("Institution"), row, 1);
tblModel.setValueAt(rs.getString("Comments"), row, 2);
row++;
}
}
return model;
}
这意味着该方法只有一项工作,加载TableModel
,仅此而已。这也意味着您可以随时随地以您喜欢的方式调用它
关于java - 从另一个类访问 jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035365/