The code has a JPanel with an inner JPanel that displays awt drawing. Upon mouseclick the inner JPanel is to be replaced by one of its polymorphic siblings. This code isn't replacing the jPanel.
class ContentPanel extends JPanel {
private GraphicPanel graphicPanel;
public ContentPanel(GraphicPanel graphicPanel) {
this.graphicPanel = graphicPanel;
add(this.graphicPanel);
public void setGraphicPanel(GraphicPanel graphicPanel) {
this.graphicPanel = graphicPanel;
// invalidate();
// revalidate();
// repaint();
}
Setting the graphicPanel to a polymorphic relative doesn't cause any errors, it just isn't painting the new graphicPanel. Using cardLayout is not preferred, there must be a cleaner way. How to proceed?
in setGraphicPanel, you need to remove the current graphicPanel and add the new one. THEN call revalidate.
something like this:
public void setGraphicPanel(GraphicPanel graphicPanel) {
this.removeAll();
this.graphicPanel = graphicPanel;
this.add(graphicPanel);
this.revalidate();
}
Although CardLayout was designed to do just this thing. Are you sure you don't want to use CardLayout?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With