I have an application that allows users to select an option and based on the user selection a JPanel is removed from the Component, the new JPanel is added and the component is revalidated
see code:
if (c != null) {
contentPane.remove(c);
}
c = new AddBookInterface(theLibrary);
contentPane.add(c);
contentPane.revalidate();
break;
c is a Component
I have several JPanels that the user can switch between and the switch works properly. However, when I add this JPanel upon user selection the JPanels that are added afterward do not load properly. What is causing this?
public class RemoveBookInterface extends JPanel {
private Library theLibrary;
public RemoveBookInterface(Library theLibrary) {
this.theLibrary = theLibrary;
setSize(400, 400);
setLayout(new BorderLayout());
setVisible(true);
removeBook(theLibrary);
}
public void removeBook(Library theLibrary) {
// prompt user for book id of book to remove
Long ID = Long
.parseLong(JOptionPane
.showInputDialog("Enter the library book ID for the book you want to remove"));
try {
// get library book info and store it to display in message
LibraryBook lb = theLibrary.getInventory().findLibraryBook(ID);
// remove book
theLibrary.removeBook(ID);
// display message indicating book was removed
JOptionPane
.showMessageDialog(
null,
"The following library book was removed:\n"
+ lb.toString());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
Better approach is to shift to CardLayout. but if you want to stick to your approach, then try to add this after your line
Or you might have forgotten to schedule a job for your event dispatcher thread.
A sample program to help your cause :
Hope that might help in some sorts.
Regards.