-1

我尝试从 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 行。有人可以帮忙吗?提前致谢。

4

1 回答 1

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
            }
        }
    }
}); 
于 2012-12-23T13:32:35.597 回答