This month, a Java tip. If you are an MFC programmer, don't panic! There will be more MFC tips, but more and more of us are using Java and so we will have some Java stuff too. Let me know what you think.

Disabling Layout Managers

Java's AWT uses layout managers to control the position of controls. This is important because on different platforms, you don't know all the correct sizes you'd usually use to position items.

As an example, when you add a button to an applet, you might use code like this:

     add(gobtn,"CENTER");

to put the button in the center.

Sometimes, this is not convienient. For example, what if you have an applet with only one button. The applet itself should look like a button. With the layout manager, it will try to leave some border space, and otherwise change the layout making it difficult to get the button to completely fill the applet. What you need is a way to turn off the layout manager.

Luckily, this is easy to do with setLayout. Just pass a null parameter and you are in business. So for example:

public void init() {
  Rectangle sz=bounds();
  // Don't use a layout manager
  setLayout(null);
  // make the next button, add it, and make it large
  btn=new Button("Next");
  add(btn);
  btn.resize(sz.width,sz.height);
  }

If you are interested in Java or other Web development issues, why not get a FREE subscription to Web Techniques magazine? This is where my Java column appears and is published by the same company that publishes Dr. Dobb's Journal. Qualified subscribers get it for free. This is a regular paper magazine you get in the mail, not an e-zine. Why not subscribe now?