Search code examples
sqlswingjtabledefaulttablemodel

Combining two different jTables and adding button into jTable


I try to do for forum using java swing. Here are my codes for table :

    public void SetUpJTable() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
    String[] data = new String[4];
    db.setUp("IT Innovation Project");
    String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = "
            + topicId + "";
    ResultSet resultSet = null;
    resultSet = db.readRequest(sql);
    try {
        while (resultSet.next()) {
            data[0] = resultSet.getString("topic_title");
            data[1] = resultSet.getString("topic_description");
            data[2] = resultSet.getString("topic_by");
            tableModel.addRow(data);        
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

I set up this table to retrieve the topic details which user select certain thread from main page. And I set up another table to store for the replies by users. Here is it :

    public void SetUpJTableComment() {
    DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
            .getModel();
    String[] data = new String[3];
    db.setUp("IT Innovation Project");
    String sql = "Select reply_content,reply_by from forumReplies WHERE reply_topic = "
            + topicId + "";
    ResultSet resultSet = null;
    resultSet = db.readRequest(sql);
    try {
        while (resultSet.next()) {
            data[0] = resultSet.getString("reply_content");
            data[1] = resultSet.getString("reply_by");
            tableModel1.addRow(data);
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

And this is how I set up the table :

    private JTable getJTableComment() {
    String header[] = { "Comment", "Reply By" };
    if (jTableComment == null) {
        jTableComment = new JTable() {
            public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
        };
    }
    DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
            .getModel();
    tableModel1.setColumnIdentifiers(header);

    jTableComment.getColumnModel().getColumn(0).setMinWidth(700);
    jTableComment.getColumnModel().getColumn(0).setMaxWidth(800);

    jTableComment.getColumnModel().getColumn(1).setMinWidth(97);
    jTableComment.getColumnModel().getColumn(1).setMaxWidth(100);

    jTableComment.getTableHeader().setFont(
            new Font("Dialog", Font.PLAIN, 20));
    jTableComment.getTableHeader().setForeground(Color.white);
    jTableComment.getTableHeader().setBackground(new Color(102, 102, 102));
    jTableComment.setRowHeight(50);
    jTableComment.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jTableComment.setFont(new Font("Dialog", Font.PLAIN, 18));
    return jTableComment;
}

It works perfectly with two separating tables. I wonder if there is some way to combine both of these tables into one table? And how can I customize the table to make it look less-liked a table because my current one is just .. solid-table and my teacher asked me to improve it but I have no idea to do so. And I tried to add button into the table but I realized that I cannot add it from the try statement because that is is retrieve data from database directly. Any guides? Thanks in advance.


Solution

  • You can use SQL join construct and have one table with more columns:

    select topic_title,topic_description,topic_by,
           reply_content,reply_by 
             from forumTopics join forumReplies 
               on (forumTopics.topic_id=forumReplies.topic_id)  WHERE topic_id = 1234
    

    then build the model from the five column result set as you are already doing.

    But surely if there is more than one reply to a forum topic, the topic part will be repeated in the table.

    To make a table not to look like a table, try JTreeTable from Swing Labs maybe, it allows to have tree-like subsections, exactly that is required. It is not part of the system library however, you will need to download it. Some source code on how to just JTreeTable can be found here.

    On how JTreeTable looks is Swing Labs, you can see in they web-startable demo. It also shows the code sample automatically.