1

I tried to create a forum in java swing. Currently I have different categories. The topics are differentiate by topicId. I try to do like when I click on certain row within certain categories, then the app will redirect to certain thread content filtering by topicId.

For example, I have a thread. The thread is in Disscussion category and topidId is 5. When I clicked on Discussion section on forum main page, it will redirect me to a table which filtered by category in database. Then when I click on the thread which is in row 3 of the jTable, it will display me the data of topicId 5 in database.

I know there is some way to do this by using getSelectedRow method in jTable. So here are my codes :

    jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    int viewRow = jTable.getSelectedRow();
                    if (viewRow >= 0) {
                        topicId = jTable.convertRowIndexToModel(viewRow);
                        System.out.println(topicId);
    }
                }
            }
        });

    jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {                      
    eForumTopics topics = new eForumTopics(topicId);
                topics.retrieveThread();
                getJFrame().dispose();
                eForumThreadContent myWindow = new eForumThreadContent(topicId);
                myWindow.getJFrame().setVisible(true);
            }
        });

Here is my retrieveThread method :

    public boolean retrieveThread(){
    boolean success = false;
    ResultSet rs = null;
    DBController db = new DBController();
    db.setUp("IT Innovation Project");
    String dbQuery = "SELECT topic_title,topic_description,topic_by FROM forumTopics WHERE topic_id = " + topicId
            + "";
    rs = db.readRequest(dbQuery);
    db.terminate();
    return success;
}

}

I got an Id column at column 0 of jTable. So whenever user clicked on certain row, the Id of that row will store into topicId and execute the following. However, this app keep returning me row 0 even when I clicked on different rows. So is there any other ways to display topic views of a forum in java swing? I mean if not using the getSelectedRow method to get the topicId. Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
It newbie
  • 9
  • 4
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 24 '12 at 07:31
  • 1
    hmm ... same person as @GabrielHeng (http://stackoverflow.com/users/1856496/gabrielheng) with new account or new person in same class? Anyway: please learn java naming conventions and stick to them. – kleopatra Dec 24 '12 at 12:29
  • Please post from a single account; you can request a [merge](http://meta.stackexchange.com/a/73801/163188). – trashgod Dec 24 '12 at 17:21

1 Answers1

2

Instead of a MouseListener on the JTable, you can determine the selected rows(s) as shown in How to Use Tables. A ListSelectionListener, illustrated here and here, is a good way to react to a selection change.

As your threads sound hierarchical in nature, also consider an Outline view, shown here. It uses the same selection listener, shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045