Removing a JTable from Frame
I have a program that is displaying a list of students - which are enrolled in the selected course - in a JTable. How can I remove the JTable before updating it when another course is selected? As it is, when the selected course is changed, the new JTable with students for that course is simply appended below t开发者_如何学编程he previous one.
If I understood your question well, you should remove the table from the container that holds it. For example if you have something like this in your code:
JTable table = new JTable();
// Here is a container. It can be a JScrollPane, JPanel or something else.
JScrollPane scroll = new JScrollPane();
// Add the table to the container.
scroll.getViewport().add( table );
To remove your table from the container, call:
scroll.getViewport().remove( table );
Now you can add another component to the container and it will be the only one visible.
精彩评论