查询更改时,java刷新JTable

时间:2015-07-09 15:58:39

标签: java sql swing jtable h2

我正在构建一个SQL DB GUI。我遇到的问题是当用户双击SHOW TABLES中的表时,它不会更新显示新表结果的表。

这是我的代码:

JTabbedPane tabbedPane = new JTabbedPane();
Statement stat = Gui.getStat();
private JPanel panel;
private String query;

public static void main(String[] args) {
    showTables();
}


public void showTables() throws SQLException{
        ResultSet showTablesResult = null;
        query = "abc";
        try {
            showTablesResult = stat.executeQuery("SHOW TABLES");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error connecting to databse");
        }
        displayResultSet(showTablesResult);
    }


    public void displayResultSet(ResultSet rs) throws SQLException{

        JTable table = new JTable(buildTableModel(rs)){
            private static final long serialVersionUID = 1L;

            @Override
                public boolean isCellEditable(int row, int column) {
                   return false;
                }
        };

        panel.add(new JScrollPane(table));


        table.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount() == 2){
                    if(query == "abc"){
                        //get value in selected cell
                        String selectedData = null;
                        int[] selectedRow = table.getSelectedRows();
                        int[] selectedColumns = table.getSelectedColumns();

                        for (int i = 0; i < selectedRow.length; i++) {
                          for (int j = 0; j < selectedColumns.length; j++) {
                            selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
                          }
                        }

                        try {
                            viewTable(selectedData);
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }

                    }
                }
            }
        });

    }



    public void viewTable(String tName) throws SQLException{
        ResultSet showTablesResult = null;
        query = "123";
        try {
            showTablesResult = stat.executeQuery("SELECT * FROM " +tName);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error connecting to databse");
        }
        displayResultSet(showTablesResult);
    }



    public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        ResultSetMetaData metaData = rs.getMetaData();

        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }


        return new DefaultTableModel(data, columnNames);

    }

我该如何解决这个问题呢?我认为可能有一个功能可以重新绘制或刷新表格或其他东西,但我找不到它是什么。

谢谢:)

2 个答案:

答案 0 :(得分:0)

panel.add(new JScrollPane(table));

默认情况下,组件的大小为(0,0),因此无需绘制任何内容。

当您从可见的GUI添加(或删除)组件时,您需要调用布局管理器,以便在组件上为组件指定大小/位置。基本代码是:

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

但是,当使用逻辑“刷新”表时,更容易做到:

table.setModel( your updated TableModel );

无需每次都重新创建JScrollPane和JTable。

答案 1 :(得分:0)

正如camickr所说,每次都没有必要重新创建JTable和TableModel

我会这样做:

从viewTable(...)中删除displayResultSet(...)方法,只需将其替换为以下内容:

Vector<Vector<Object>> newDataVector = //fill with new data
defaultTableModel.setDataVector(newDataVector, columnIdentifiers);
defaultTableModel.fireTableDataChanged();