View Single Post
  #3 (permalink)  
Old 11-11-2007, 10:53 PM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The default layout manager for Applet is FlowLayout - this from it being an extension of the Panel class.
System.out.println(new Applet().getLayout().getClass().getName());
When you add components to a FlowLayout it may not be able to show them at their preferred size (which is what it tries to do). So your added components may be offscreen or collapsed.
What to do?
An elegant solution is to change the model in your View and Manipulation class instances so you don't have to swap/change components.
Code:
v.setModel(m); manic.setModel(m);
If this is not possible/desirable then try removing the components from the applet, adding the new ones and validating the layout. Calling validate on a Container tells it we have changed something in one or more of its children that will affect the layout and it needs to do a new layout for proper display. In the JComponent (Swing vs. AWT) class we have a similar method revalidate.
Code:
public void mouseClicked(MouseEvent e) { if(clicked==false) { x=e.getX(); y=e.getY(); m=new Model(x,y,xborder,yborder); v=new View(m); sim=new Simulation(m,v); manic=new Manipulation(m); removeAll(); add(v); add(manic); validate(); clicked=true; } }
Another option is to set the layout for the applet to BorderLayout. You could add your view to the center section and your button component to the south section using the proper constraints (see BorderLayout api). And you would still call removeAll and validate in the event code.
Reply With Quote