TCLUG Development Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG-DEVEL:175] Absolute positioning in Java AWT?





Bob Tanner wrote:

> Quoting Perry Hoekstra (dutchman@uswest.net):
> > > I have a basic graphic and I want to put a TextField in the upper-right
> > > corner of the graphic.
> > >
> >
> > the code would look like this:
> >
> > g.drawImage(t_textField, 0, 600, Color.WHITE, this)
> >
> > where t_textField is an instance of the TextField,
> > 0, 600 is the absolute position,
> > Color.WHITE is the background
> > this refers to the Panel, Applet, whatever that implements ImageObserver

Apologize profusely.  Had a bit of a brain fart.  One should never code off the
top of their head.

Okay, here we go:

For buttons, widgets, etc., they all extend from Component.  Component has a
method called setBounds(int x, int y, int width, int height).  The definition
from the source file is:

 /**
  * Moves and resizes this component. The new location of the top-left
  * corner is specified by <code>x</code> and <code>y</code>, and the
  * new size is specified by <code>width</code> and <code>height</code>.
  * @param <code>x</code> The new <i>x</i>-coordinate of this component.
  * @param <code>y</code> The new <i>y</i>-coordinate of this component.
  * @param <code>width</code> The new <code>width</code> of this component.
  * @param <code>height</code> The new <code>height</code> of this
  * component.
  * @see java.awt.Component#getBounds
  * @see java.awt.Component#setLocation(int, int)
  * @see java.awt.Component#setLocation(java.awt.Point)
  * @see java.awt.Component#setSize(int, int)
  * @see java.awt.Component#setSize(java.awt.Dimension)
  * @JDK1.1
  */

It would be used in such a way:

private java.awt.TextArea getEnterTextArea() {
 if (enterTextArea == null) {
  try {
   enterTextArea = new java.awt.TextArea();
   enterTextArea.setName("EnterTextArea");
   enterTextArea.setBounds(12, 10, 403, 172);
  } catch (java.lang.Throwable anException) {
   handleException(anException);
  }
 }
 return enterTextArea;
}

I attached a small applet that uses a null layout manager and two widgets that
are using absolute positioning.  Give a holler if this is not what you are
looking for.

--
Perry Hoekstra

---
"I don't see much sense in that," said Rabbit.
"No," said Pooh humbly, "there isn't. But there was going to be when I began
it. It's just that something happened to it along the way."

Test.java