Home > Software design >  How to fill jtable with the right data from another table?
How to fill jtable with the right data from another table?

Time:04-22

I have a JTable with 6 columns, the rows for the first 3 columns comes from table1(subject_records_bsit_first_year) and the last 3 will come from table2(evaluation_permission). The two database table have two joined columns(subject_code and subject_title). I'm trying to fill the last 3 columns of my JTable with data from table2—using the db table connection to display the right data.

My code:

public static void loadFirstYearSubjectList() {
        
        ConnectionManager connectionManager = new ConnectionManager();
        //String sqlChooseStudent = "SELECT * FROM student_bsit_" studentID.toLowerCase()  " WHERE year_level = '" yearLevel "'";
        try {
            Connection con = connectionManager.createConnection();
            Statement stmt = con.createStatement();
            String sql = "SELECT * FROM `subject_records_bsit_first_year` ORDER BY `subject_records_bsit_first_year`.`trimester_period` ASC";
            ResultSet rs = stmt.executeQuery(sql);
            
            DefaultTableModel tableModel = new DefaultTableModel();
            tableModel.addColumn("Subject Code");
            tableModel.addColumn("Subject Title");
            tableModel.addColumn("Trimester");
            tableModel.addColumn("Section");
            tableModel.addColumn("Professor");
            tableModel.addColumn("Evaluation");
            
            table.setModel(tableModel);
            
            TableColumnModel columnModel = table.getColumnModel();
            columnModel.getColumn(0).setPreferredWidth(95);
            columnModel.getColumn(1).setPreferredWidth(266);
            columnModel.getColumn(2).setPreferredWidth(90);
            columnModel.getColumn(3).setPreferredWidth(77);
            columnModel.getColumn(4).setPreferredWidth(179);
            columnModel.getColumn(5).setPreferredWidth(78);
            
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
            renderer.setBackground(new Color(120, 120, 120, 10));
            renderer.setOpaque(true);
            columnModel.getColumn(0).setCellRenderer(renderer);
            columnModel.getColumn(1).setCellRenderer(renderer);
            columnModel.getColumn(2).setCellRenderer(renderer);
            columnModel.getColumn(3).setCellRenderer(renderer);
            columnModel.getColumn(4).setCellRenderer(renderer);
            columnModel.getColumn(5).setCellRenderer(renderer);
            while (rs.next()) {
                String subjectCode = rs.getString("subject_code");
                String subjectTitle = rs.getString("subject_title");
                String trimesterPeriod = rs.getString("trimester_period");
                
                tableModel.addRow(new Object[] {subjectCode, subjectTitle, trimesterPeriod});
            }
            
        } catch (Exception e) {
            
        } finally {
            try {
                connectionManager.closeConnection();
            } catch (Exception e2) {

            }
        }
    }

table 2 data I'm trying to display the section, subject_teacher and evaluation_permission from this table, this table is joined from another table that already displays the first 3 columns on my jtable. The two db table is joined with the same column names; subject_code and subject_title.

CodePudding user response:

Create your own implementation of TableModel that is able to retrieve the data as you described above. Then use JTable's setModel() to make the JTable actually use your TableModel implementation.

An example how to use it is in the Java tutorials.

CodePudding user response:

SELECT subject_records_bsit_first_year.subject_code,
       subject_records_bsit_first_year.subject_title,
       subject_records_bsit_first_year.trimester_period,
       evaluation_permission.section, evaluation_permission.subject_teacher,
       evaluation_permission.permission_status
FROM subject_records_bsit_first_year
INNER JOIN evaluation_permission
      ON subject_records_bsit_first_year.subject_code = evaluation_permission.subject_code;

Got what I'm trying to do, thanks for the help! Sometimes people learn from example.

  • Related