单击按钮后如何将JTable添加到JPanel?

时间:2017-04-09 00:12:19

标签: java swing jbutton

如何在用户点击按钮后将JTable添加到JPanel?单击按钮后,JLabel将添加到JPanel,而不是JTable。

        @Override
        public void actionPerformed(ActionEvent e) {

            ResultSet resultSet = query.getStudentDetails(operateOnSelectedStudent());
            JTable table = null;
            try {
                while (resultSet.next()) {
                    try {
                        table = new JTable(buildTableModel(resultSet));
                        panel.add(table, BorderLayout.SOUTH);
                        panel.add(new JLabel("HELLO"));
                        panel.revalidate();
                        JOptionPane.showMessageDialog(null, new JScrollPane(table));
                        JScrollPane tableContainer = new JScrollPane(table);

                        panel.add(tableContainer, BorderLayout.CENTER);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

1 个答案:

答案 0 :(得分:0)

我能够解决问题。我在代码中添加了以下行来解决问题。

panel.add(tableContainer);
panel.revalidate();
panel.repaint();