我是 Java 新手。
我正在尝试构建一个 CRUD 程序。现在,我尝试从数据库中读取数据并将它们显示在jtable
. 所以每次我都会创建一个新表并将其添加到jpanel
. 但是程序在更新时不显示表格。有人能帮我吗?
package com.accsoftware.ui;
import java.sql.Date;
import java.util.List;
import javax.swing.JTable;
import com.accsoftware.common.Table1;
import com.accsoftware.util.DataHandler;
public class SearchAndDisplayPanel extends javax.swing.JPanel {
private static final long serialVersionUID = -9078860779973523031L;
public SearchAndDisplayPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
chooseTableButtonGroup = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
date = new com.toedter.calendar.JDateChooser();
jLabel2 = new javax.swing.JLabel();
date1 = new com.toedter.calendar.JDateChooser();
displayPanel = new javax.swing.JPanel();
generateReport = new javax.swing.JButton();
Search = new javax.swing.JButton();
chooseTablePanel = new javax.swing.JPanel();
table1RadioButton = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jLabel1.setText("Start:");
jLabel2.setText("End:");
javax.swing.GroupLayout displayPanelLayout = new javax.swing.GroupLayout(displayPanel);
displayPanel.setLayout(displayPanelLayout);
displayPanelLayout.setHorizontalGroup(
displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
displayPanelLayout.setVerticalGroup(
displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 173, Short.MAX_VALUE)
);
generateReport.setText("Generate Report");
generateReport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
generateReportActionPerformed(evt);
}
});
Search.setText("Search");
Search.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SearchActionPerformed(evt);
}
});
chooseTableButtonGroup.add(table1RadioButton);
table1RadioButton.setText("Table1");
chooseTableButtonGroup.add(jRadioButton2);
jRadioButton2.setText("Table2");
javax.swing.GroupLayout chooseTablePanelLayout = new javax.swing.GroupLayout(chooseTablePanel);
chooseTablePanel.setLayout(chooseTablePanelLayout);
chooseTablePanelLayout.setHorizontalGroup(
chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(chooseTablePanelLayout.createSequentialGroup()
.addComponent(table1RadioButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addComponent(jRadioButton2)
.addContainerGap())
);
chooseTablePanelLayout.setVerticalGroup(
chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(chooseTablePanelLayout.createSequentialGroup()
.addGroup(chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(table1RadioButton)
.addComponent(jRadioButton2))
.addGap(0, 6, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(displayPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(date1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(chooseTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(generateReport)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Search)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(date1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(generateReport)
.addComponent(Search, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(chooseTablePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
private void generateReportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
Date start = dateToString(date);
Date end = dateToString(date1);
String table = table1RadioButton.isSelected()?"Table1":"Table2";
List result = DataHandler.readAll(table, start, end);
int col = table1RadioButton.isSelected()?7:10;
Object[][] data = new Object[result.size()][col];
if(table.equalsIgnoreCase("Table1")){
int id = 0;
for(Object element:result){
Table1 table1 = (Table1) element;
data[id][0] = table1.getDate();
data[id][1] = table1.getItem();
data[id][2] = table1.getFee();
data[id][3] = table1.getGst();
data[id][4] = table1.getVehicle();
data[id][5] = table1.getWages();
data[id][6] = table1.getStationary();
id++;
}
String[] columnNames = { "Date", "Reference/Item", "Cost", "GST",
"Vehicle", "Wages", "Stationary" };
JTable displayTable = new JTable(data, columnNames);
this.displayPanel.add(displayTable);
this.displayPanel.revalidate();
this.displayPanel.repaint();
}
}
@SuppressWarnings("deprecation")
private Date dateToString(com.toedter.calendar.JDateChooser date){
return new Date(date.getDate().getYear(),
date.getDate().getMonth(), date.getDate().getDay());
}
// Variables declaration - do not modify
private javax.swing.JButton Search;
private javax.swing.ButtonGroup chooseTableButtonGroup;
private javax.swing.JPanel chooseTablePanel;
private com.toedter.calendar.JDateChooser date;
private com.toedter.calendar.JDateChooser date1;
private javax.swing.JPanel displayPanel;
private javax.swing.JButton generateReport;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JRadioButton table1RadioButton;
private javax.swing.JRadioButton jRadioButton2;
// End of variables declaration
}