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:
"
+ lb.toString());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…