So I have a JTable inside a JScrollPane inside a frame that works. What I want to do is to change the table that is being showed to another table that displays some other information and has more/less columns/rows. How do I do this?
I have tried stuff like putting another table in the ScrollPane but that doesn’t work and if I re-put the ScrollPane in the frame it disappears.
EDIT:
I think this is all the code that has to do with this problem. I simply call the funktions to change the view.
Object[][] userData = new Object[50][6];
userTable = new JTable(userData, new String[] { "Namn", "Adress",
"Telefon", "Personnummer", "PIN", "Antal cyklar" }) {
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
userTable.setSelectionMode(0);
userTable.getTableHeader().setReorderingAllowed(false);
Object[][] bikeData = new Object[50][7];
bikeTable = new JTable(bikeData, new String[] { "Ägare", "Streckkod",
"Färg", "Märke", "Ram-nummer", "Senast hämtad", "Senast lämnad" }) {
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
bikeTable.setSelectionMode(0);
bikeTable.getTableHeader().setReorderingAllowed(false);
JScrollPane tablePane = new JScrollPane(bikeTable);
frame.add(tablePane);
frame.setVisible(true);
}
public void displayUsers(){
tablePane.setViewportView(userTable);
}
public void displayBikes(){
tablePane.setViewportView(bikeTable);
}
Have you tried this:
This should replace the component displayed in the scroll pane. Of course you can always change the model of your JTable and its columns but that would not be my preference.
EDIT: Here is a snippet demonstrating this: