Re: applet resizing



jay wrote:

> Sub: applet resizing

Generally you do not resize applets, though there are ways
you can allow your end user to resize an applet.

In this case, resizing (or indeed 'sizing') anything is
a mistake.

Any help/suggestions would be greatly appreciated.

I am building an applet that uses panels extensively and switches
panels in the center of the applet as buttons are pressed.

It sounds like you need a CardLayout in the CENTER of your BorderLayout.

But some corrections to, and comments on, the source
you posted..

<sscce>
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

// why is this called 'panel' when it is not a Panel or JPanel?
// class names should start with FirtsLetterOfEachWordUpperCase
public class panel extends JApplet
{
//  private int height = 200;//applet size
//  private int width = 200;
// set in the HTML

  Panel top = new Panel();
  Panel center = new Panel();
  Panel center1 = new Panel();
  Panel center2 = new Panel();

  public JButton test1 = new JButton ("Test 1");
  public JButton test2 = new JButton ("Test 2");
  public JButton test3 = new JButton ("Test 3");

  public void init()
  {
    JRootPane rootPane = this.getRootPane();

    // why are you invoking this?
    //rootPane.putClientProperty("defeatSystemEventQueueCheck",
      //Boolean.TRUE);

    setLayout (new BorderLayout());
    add (BorderLayout.NORTH, top);
    add (BorderLayout.CENTER, center);

    // an ActionListener would respond to MouseEvents
    // as well as keyboard activity..
    test1.addMouseListener(new TestMouseListener());

    top.add(test1);

    center1.add(test2);
    center2.add(test3);

    center.add(center1);

    // not necessary
    // center1.setVisible(true);

    // this has lots of problems when used with applets
    //resize(width, height);

    // important!
    validate();
  }

  public class TestMouseListener extends MouseAdapter {
    public void mouseClicked(MouseEvent evt) {
      if ((evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
        center.removeAll();
        center.add(center2);

        // important!
        validate();
      }
    }
  }
}
</sscce>

HTH

--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
.