gpt4 book ai didi

java - JTable 未在 ActionEvent 中显示

转载 作者:行者123 更新时间:2023-11-30 07:21:57 25 4
gpt4 key购买 nike

我打算在我在本类(class)中创建的 JTable 中显示学生成绩。我的意图是在加载表单时有一个空表,在输入一些参数时有一个空表,查询数据库并将结果显示在 JTable 中。空表正在显示,但在单击按钮时没有任何反应。也不异常(exception)。它只是保持为空。这是我的代码...(部分)

public class ClassExamResults extends JInternalFrame implements ActionListener {

private JPanel jpCer = new JPanel();
private DefaultTableModel dtmCustomer;
private JTable tbCustomer;
private JScrollPane jspTable;
static Vector<Vector<String>> data = new Vector<Vector<String>>();

ClassExamResults() {
super("Class Exam Results", false, true, false, true);
setSize(1350, 620);
jpCer.setBounds(10, 0, 500, 150);
jpCer.setLayout(null);
jpCer.setBackground(Color.LIGHT_GRAY);

//Define table
tbCustomer = makeTable();
jspTable = new JScrollPane(tbCustomer);
jspTable.setBounds(170, 20, 1150, 550);



jpCer.add(jspTable);


getContentPane().add(jpCer);
setVisible(true);

}

private JTable makeTable() {
//String Type Array use to Give Table Column Names.
Vector<String> headers = new Vector<String>();

headers.add("ADMIS. NO.");
headers.add("NAME");
headers.add("EXAM");
headers.add("MATHS");
headers.add("ENG.");
headers.add("KISWA.");
headers.add("PHYC.");
headers.add("CHEM");
headers.add("BIO");
headers.add("HISTO.");
headers.add("GEOG.");
headers.add("CRE");
headers.add("COMP.");
headers.add("HOME SC.");
headers.add("BUSIN");
headers.add("AGRIC.");
headers.add("TOTAL");
headers.add("AVERAGE");
headers.add("POS.");



dtmCustomer = new DefaultTableModel(data, headers);
tbCustomer = new JTable(dtmCustomer) {
public boolean isCellEditable(int iRow, int iCol) {
return false; //Disable All Columns of Table.
}
};
tbCustomer.setRowHeight(25);
tbCustomer.setBackground(new java.awt.Color(89, 151, 207));
tbCustomer.setForeground(Color.BLACK);
tbCustomer.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


return tbCustomer;
}



public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnFind) {
if (level.equalsIgnoreCase("SELECT")) {
JOptionPane.showMessageDialog(this, "Please Select Exam Level",
"Error", JOptionPane.ERROR_MESSAGE);
cboLevel.requestFocus();
} else if (year.equalsIgnoreCase("SELECT")) {
JOptionPane.showMessageDialog(this, "Please Select Exam Academic year",
"Error", JOptionPane.ERROR_MESSAGE);
cboYear.requestFocus();
} else {
Connection conn = null;
Vector<String> headers = new Vector<String>();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "USERNAME", "PASSWORD");
Statement st = conn.createStatement();
String results = "select stu_number as ADMISSION_NUMBER, "
+ " max(stu_name) STUDENT_NAME, "
+ "ex_name as EXAM_NAME, "
+ "max(case when ex_subject = 'MATHEMATICS' then stu_score else 0 end) as MATHEMATICS, "
+ " max(case when ex_subject = 'ENGLISH' then stu_score else 0 end) as ENGLISH, "
+ " max(case when ex_subject = 'KISWAHILI' then stu_score else 0 end) as KISWAHILI, "
+ " max(case when ex_subject = 'PHYSICS' then stu_score else 0 end) as PHYSICS, "
+ " max(case when ex_subject = 'CHEMISTRY' then stu_score else 0 end) as CHEMISTRY, "
+ " max(case when ex_subject = 'BIOLOGY' then stu_score else 0 end) as BIOLOGY, "
+ "max(case when ex_subject = 'HISTORY' then stu_score else 0 end) as HISTORY, "
+ " max(case when ex_subject = 'GEOGRAPHY' then stu_score else 0 end) as GEOGRAPHY, "
+ " max(case when ex_subject = 'CRE' then stu_score else 0 end) as CRE, "
+ " max(case when ex_subject = 'COMPUTER' then stu_score else 0 end) as COMPUTER, "
+ " max(case when ex_subject = 'HOME SCIENCE' then stu_score else 0 end) as HOME_SCIENCE, "
+ " max(case when ex_subject = 'BUSINESS' then stu_score else 0 end) as BUSINESS, "
+ " max(case when ex_subject = 'AGRICULTURE' then stu_score else 0 end) as AGRICULTURE, "
+ " sum(stu_score) as TOTAL_MARKS, "
+ " sum(stu_score)/count(stu_score) as AVERAGE, "
+ " rank () over (order by sum(stu_score)desc) as POSITION "
+ "from EXAMS_MASTER_REGISTER "
+ "where stu_level = '" + level + "' and "
+ " stu_stream = '" + stream + "' and "
+ " ex_semester = '" + semester + "' and "
+ " ex_name = '" + exam + "' and "
+ " academic_year = '" + year + "' "
+ "group by stu_number, ex_name "
+ "order by sum(stu_score) desc";
ResultSet rs = st.executeQuery(results);
while (rs.next()) {
Vector<String> d = new Vector<String>();


d.add(rs.getString("ADMISSION_NUMBER"));
d.add(rs.getString("STUDENT_NAME"));
d.add(rs.getString("EXAM_NAME"));
d.add(rs.getString("MATHEMATICS"));
d.add(rs.getString("ENGLISH"));
d.add(rs.getString("KISWAHILI"));
d.add(rs.getString("PHYSICS"));
d.add(rs.getString("CHEMISTRY"));
d.add(rs.getString("BIOLOGY"));
d.add(rs.getString("HISTORY"));
d.add(rs.getString("GEOGRAPHY"));
d.add(rs.getString("CRE"));
d.add(rs.getString("COMPUTER"));
d.add(rs.getString("HOME_SCIENCE"));
d.add(rs.getString("BUSINESS"));
d.add(rs.getString("AGRICULTURE"));
System.out.println(rs.getString("STUDENT_NAME"));



data.add(d);

}


} catch (Exception db) {
JOptionPane.showMessageDialog(this, db,
"Error", JOptionPane.ERROR_MESSAGE);
}

}
}
}
}

我知道查询执行是从以下时间开始的; System.out.println(rs.getString("STUDENT_NAME"));返回正确的值。我还尝试从蟾蜍运行查询,它显示了正确的结果。我的错误在哪里?

最佳答案

  1. 直接往DefaultTableModel dtmCustomer;里放一条新的Records,DefaultTableModel不知道你往底层的Vector of Vector<里填数据,或者你必须调用 DefaultTableModel#setDataVector

  2. 您忘记重置 TableModel#setDataVector(null)在任何进一步的步骤之前,在这种情况下,您将在 JTable

  3. 的末尾添加一个新行
  4. JDBC 从 Event Dispatch Thread 调用,然后 Swing GUI 将被卡住直到 Resultset 结束,使用一些 Workers Thread , Runnable#ThreadSwingWorker for JDBC

编辑

关于java - JTable 未在 ActionEvent 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13048928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com