从内容面板中删除JTable

ple*_*ood 0 java swing jtable jframe

我正在使用JTable以图形方式显示我正在开发的应用程序的搜索结果.我希望能够在不再需要表时删除表,然后用新创建的表替换它.以下是我目前将表格添加到JFrame的方法:

    userLibrary = new CustomLibrary(users, LIBRARY_WIDTH, LIBRARY_HEIGHT);
    userLibrary.setOpaque(true);
    userLibrary.setBounds(LIBRARY_START_X, LIBRARY_START_Y, LIBRARY_WIDTH, LIBRARY_HEIGHT);
    getContentPane().add(userLibrary);
Run Code Online (Sandbox Code Playgroud)

我的自定义库(扩展了JPanel)执行以下操作:

public CustomLibrary(LinkedList<User> usernames, int width, int height) {
    CustomTable table = new CustomTable(userRows,columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(width, height));
    table.setFillsViewportHeight(true);
    table.setAutoCreateRowSorter(true);
    JScrollPane scrollPane = new JScrollPane(table);

    // Add the scroll pane to this panel.
    add(scrollPane);
}
Run Code Online (Sandbox Code Playgroud)

现在一切正常并显示我的表,但我无法弄清楚如何从我的内容窗格中完全删除表.我试过打电话

getContentPane().remove(userLibrary);
Run Code Online (Sandbox Code Playgroud)

但这似乎什么都不做.

所以我的一般问题是.一旦我已经创建并绘制了一个表,我如何从我的JFrame中完全删除它?

cam*_*ckr 6

我希望能够在不再需要表时删除表,然后用新创建的表替换它.

最简单的方法是替换JTable的TableModel:

table.setModel( yourNewlyCreatedTableModel );
Run Code Online (Sandbox Code Playgroud)

无需创建JTable或JScrollPane.