Stanley Mungai
Stanley Mungai

Reputation: 4150

JTable not displaying at ActionEvent

I Intend to Display student results in a JTable that I have Created in this class. My Intention is to have an Empty Table when the form loads and them when some Parameters are entered, the database is queried and the results are shown in the JTable. The Empty table is displaying but at Button Click nothing is happening. No exception also. It just remains Empty. Here is My code...(part)

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);
            }

        }
    }
}
}

I know the query execution is happening since the; System.out.println(rs.getString("STUDENT_NAME")); returns the correct values. I have also tried to run the Query from toad and it is displaying the right results. Where is My Mistake?

Upvotes: 2

Views: 131

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  1. put a new Records to the DefaultTableModel dtmCustomer; directly, DefaultTableModel doesn't know that you fill data to the underlaying Vector of Vector, or you have to call DefaultTableModel#setDataVector

  2. you forgot to reset the TableModel#setDataVector(null) before any further steps, in this case you'll add a new rows to the end of JTable

  3. JDBC is called from Event Dispatch Thread, then Swing GUI will be freeze untill Resultset ended, use some of Workers Thread, Runnable#Thread or SwingWorker for JDBC

EDIT

Upvotes: 4

Related Questions