I'm just going through a small book on Java programming and the goal is to press a button to show a shape (ie: circle) and then to press a button to hide that same shape.

My idea was to simply add it in but change the visibility to true and false. Now, I get it to show perfectly but I can't get it to hide (ie: setVisiblity to false) for some reason. The problem lies in the actionPerformed method of the CommandMain class. I'm at a loss as to why I can't simply change the visibility of the object to true and false.

I've include the relevant code below
Thanks for all your help in advance.


================= COMMANDMAIN.JAVA =================
public class CommandMain extends JFrame implements ActionListener
{ JButton circleButton = new JButton("Make Circle");
JButton squareButton = new JButton("Make Square");
JButton undoButton = new JButton("Undo Last");
JButton exitButton = new JButton("Exit");
private static Vector objectList = new Vector(); // Stores objects in order
JPanel container = new JPanel();
Button newCircle; // Used to add a Circle object to the vector
Button newSquare;
Button newShape;
Button objectToHide;

// CONSTRUCTOR
// PURPOSE: Used to set up all the buttons on our Panel
public CommandMain()
{ setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Add interactive ability to buttons
circleButton.addActionListener(this);
squareButton.addActionListener(this);
undoButton.addActionListener(this);
exitButton.addActionListener(this);

// Add them so we can see them on the panel
this.container.add(circleButton);
this.container.add(squareButton);
this.container.add(undoButton);
this.container.add(exitButton);
setContentPane(container);
setVisible(true);
}

// PURPOSE: Used to create an instance of itself so we can see the frame
public static void main(String args[]) throws IOException
{ CommandMain window = new CommandMain(); }

// PURPOSE: Used to perform actions based on the button picked by the user
public void actionPerformed(ActionEvent picked)
{ Object choice = picked.getSource();
if(choice == circleButton) // CIRCLE WAS SELECTED
{ newShape = new Button("Circle");
Button newCircle = newShape.getShape();
this.container.add(newCircle); // Add it to my container
newShape.execute();
objectList.add(newShape); // Add the object to my Vector
setContentPane(container);
repaint(); // Redraw the GUI because it changed
}
else if(choice == undoButton) // UNDO WAS SELECTED
{ if(objectList.size() > 0) // Check to ensure we don't overstep the boundaries
{ System.out.println("UNDO Pressed");
objectToHide = (Button)objectList.elementAt(objectList.size()-1);
objectToHide.hideShape(); // <-- PROBLEM: NOT HIDING MY SHAPE

objectList.removeElementAt(objectList.size()-1);
setContentPane(container);
repaint();
} // End if(objectList.size() > 0)
} // End undoButton
else if(choice == exitButton) // EXIT WAS SELECTED
{ System.out.println("Goodbye: Application exiting");
dispose();
System.exit(0);
}
else // No idea what button was selected
{ System.out.println("ERROR: Invalid option"); }
}
}

========================== BUTTON.JAVA ==================
import java.awt.*;
import javax.swing.*;

public class Button extends JPanel
{ String objectShape;

Button()
{ }

Button(String shape)
{ this.objectShape = shape; }

public void execute()
{ setVisible(true); }

public void hideShape()
{ System.out.println("hideShape called");
super.setVisible(false);
}

public Button getShape()
{ if(this.objectShape.equals("Circle"))
{ System.out.println("Making a circle");
Circle newCircle = new Circle();
return newCircle;
}

else if(this.objectShape.equals("Square"))
{ System.out.println("Making a square");
Square newSquare = new Square();
return newSquare;
}
return null;
}
}