getSelectedRow方法保持返回第0行

时间:2012-12-23 13:16:55

标签: swing

我尝试从jTable中选择一行并将值存储到topicId中。这是我的代码。

    jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
            if (jTable.getSelectedRow() >= 0 && jTable.getValueAt(jTable.getSelectedRow(), 0) != null) {
                    topicId = (Integer)jTable.getValueAt(jTable.getSelectedRow(), 0);}

            System.out.println(topicId);
                eForumTopics topics = new eForumTopics(topicId);
                topics.retrieveThread();
                getJFrame().dispose();
                eForumThreadContent myWindow = new eForumThreadContent(topicId);
                myWindow.getJFrame().setVisible(true);
            }
        });
    }

以下是滚动窗格的代码。

    private JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
        jScrollPane = new JScrollPane();
        jScrollPane.setBounds(new Rectangle(75, 220, 800, 450));
        jScrollPane.setViewportView(getJTable());
    }
    return jScrollPane;
}

以下是jTable的代码。

    private JTable getJTable() {
    if (jTable == null) {
        Vector columnNames = new Vector(); // Vector class allows dynamic
                                            // array of objects
        Vector data = new Vector();
        try {
            DBController db = new DBController();
            db.setUp("IT Innovation Project");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String dsn = "IT Innovation Project";
            String s = "jdbc:odbc:" + dsn;
            Connection con = DriverManager.getConnection(s, "", "");
            String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_category = '"+category+"'";
            java.sql.Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columns = metaData.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(metaData.getColumnName(i));
            }
            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(resultSet.getObject(i)); 
                }
                data.addElement(row);
            }
            resultSet.close();
            ((Connection) statement).close();
        } catch (Exception e) {
            System.out.println(e);
        }
        jTable = new JTable(data, columnNames);
        TableColumn column;
        for (int i = 0; i < jTable.getColumnCount(); i++) {
            column = jTable.getColumnModel().getColumn(i);
            if (i == 1) {
                column.setPreferredWidth(400); // second column is bigger
            }else {
                column.setPreferredWidth(200);
            }
        }
        String header[] = { "Title", "Description", "Posted by" };

        for (int i = 0; i < jTable.getColumnCount(); i++) {
            TableColumn column1 = jTable.getTableHeader().getColumnModel()
                    .getColumn(i);

            column1.setHeaderValue(header[i]);
        }
        jTable.getTableHeader().setFont( new Font( "Dialog" , Font.PLAIN, 20 ));
        jTable.getTableHeader().setForeground(Color.white);
        jTable.getTableHeader().setBackground(new Color(102, 102, 102));
        jTable.setEnabled(false);
        jTable.setRowHeight(100);
        jTable.getRowHeight();
        jTable.setFont( new Font( "Dialog" , Font.PLAIN, 18 ));
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

我不确定我的桌子或滚动窗格是否有问题。每当我在jTable中选择任何行时,例如4或5或6,结果就会让我返回第0行。有人可以帮忙吗?提前谢谢。

1 个答案:

答案 0 :(得分:4)

添加鼠标侦听器以将所选行存储在字段中没有意义。该表知道其选定的行,而且,可以在不使用鼠标的情况下更改选择。

如果您想在每次选择更改时执行某些操作(例如加载所选行的详细信息),请添加选择侦听器:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            int viewRow = table.getSelectedRow();
            if (viewRow >= 0) {
                int modelRow = table.convertRowIndexToModel();
                Integer topicId = tableModel.getTopicIdAtRow(modelRow);
                // todo load the details for topicId
            }
        }
    }
});