I'm trying to remove an object from my GUI and i've done everything I can think of. I thought I had the problem licked by calling validate() but it didn't work. What i'm trying to do is retrieve my object called button from a vector, then set the visibility of the object to false to hide my object. I know this posting is similar to something I posted before but this visibility stuff is really weird. I'm really stuck because it seems to make sense to me what i'm doing.
Anyway, here's an abbrieviated version of my program (the problem lies in the actionPerformed method
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();
JPanel container = new JPanel();
Button newCircle;
Button newSquare;
Button newShape;
public CommandMain()
{ setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
circleButton.addActionListener(this);
squareButton.addActionListener(this);
undoButton.addActionListener(this);
exitButton.addActionListener(this);
this.container.add(circleButton);
this.container.add(squareButton);
this.container.add(undoButton);
this.container.add(exitButton);
setContentPane(container);
setVisible(true);
}
public static void main(String args[]) throws IOException
{ CommandMain window = new CommandMain(); }
public void actionPerformed(ActionEvent picked)
{ Button objectToHide;
Object choice = picked.getSource();
if(choice == circleButton)
{ newShape = new Button("Circle"
;
Button newCircle = newShape.getShape();
this.container.add(newCircle);
newShape.execute();
objectList.add(newShape); setContentPane(container);
repaint(); // Redraw the GUI because it changed
}
else if(choice == undoButton)
{ if(objectList.size() > 0) { System.out.println("UNDO Pressed"
;
objectToHide = (Button)objectList.elementAt(objectList.size()-1);
objectToHide.hideShape(); // <== PROBLEM HERE: OBJECT DOESN'T DISAPPEAR
objectList.removeElementAt(objectList.size()-1);
validate();
setContentPane(container);
// repaint();
} // End if(objectList.size() > 0)
} // End undoButton
}
}
================ BUTTON.JAVA
public class Button extends JPanel
{ String objectShape;
Button()
{ }
Button(String shape)
{ this.objectShape = shape; }
public void execute()
{ setVisible(true); }
public void hideShape() //<=== SOMETHING SCREWY?
{ setVisible(false); }
public Button getShape()
{ // SNIP }
Anyway, here's an abbrieviated version of my program (the problem lies in the actionPerformed method
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();
JPanel container = new JPanel();
Button newCircle;
Button newSquare;
Button newShape;
public CommandMain()
{ setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
circleButton.addActionListener(this);
squareButton.addActionListener(this);
undoButton.addActionListener(this);
exitButton.addActionListener(this);
this.container.add(circleButton);
this.container.add(squareButton);
this.container.add(undoButton);
this.container.add(exitButton);
setContentPane(container);
setVisible(true);
}
public static void main(String args[]) throws IOException
{ CommandMain window = new CommandMain(); }
public void actionPerformed(ActionEvent picked)
{ Button objectToHide;
Object choice = picked.getSource();
if(choice == circleButton)
{ newShape = new Button("Circle"
Button newCircle = newShape.getShape();
this.container.add(newCircle);
newShape.execute();
objectList.add(newShape); setContentPane(container);
repaint(); // Redraw the GUI because it changed
}
else if(choice == undoButton)
{ if(objectList.size() > 0) { System.out.println("UNDO Pressed"
objectToHide = (Button)objectList.elementAt(objectList.size()-1);
objectToHide.hideShape(); // <== PROBLEM HERE: OBJECT DOESN'T DISAPPEAR
objectList.removeElementAt(objectList.size()-1);
validate();
setContentPane(container);
// repaint();
} // End if(objectList.size() > 0)
} // End undoButton
}
}
================ BUTTON.JAVA
public class Button extends JPanel
{ String objectShape;
Button()
{ }
Button(String shape)
{ this.objectShape = shape; }
public void execute()
{ setVisible(true); }
public void hideShape() //<=== SOMETHING SCREWY?
{ setVisible(false); }
public Button getShape()
{ // SNIP }