Absolute Layout Manager

Question:
Do you know of any LayoutManager that would allow me to lay out components exactly where I want them, or any other way of doing so?

Answer:
There is no standard AWT or Swing LayoutManager implementation that will perform absolute positioning of components. Instead, java.awt.Component, and therefore every subclass of Component, contains a setLocation method that allows a component to be positioned using coordinates that are interpreted relative to the component’s parent container. To position components absolutely without any interference from a layout manager, you have to set the layout manager of the container to null. You also have to set the sizes of your components explicitly, not relying on a layout manager to size your components for you automatically. The following code listing demonstrates how to do this. It places a button inside a frame at a randomlocation. Every time you press the button, it moves itself to a new random location within the frame.

import java.awt.*;import java.awt.event.*;public final class AbsoluteLayout {  public static int random(int min, int range) {    double dRange = range;    double result;    result = Math.rint(Math.random()*dRange);    return (int)(min + (int)result);  }  public static void main(String[] args) {    Button button;    Frame frame;    WindowListener exitListener;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {	Window window = e.getWindow();	window.setVisible(false);	window.dispose();	System.exit(0);      }    };    button = new Button("Press me");    // Explicitly size the button, otherwise it will be a mere point.    button.setSize(100, 25);    // Add an ActionListener that will randomly move the button    button.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {	Component source, parent;	Dimension sourceSize, parentSize;	source = (Component)e.getSource();	sourceSize = source.getSize();	parent = source.getParent();	parentSize = parent.getSize();	source.setLocation(random(0, parentSize.width - sourceSize.width),			   random(0, parentSize.height - sourceSize.height));      }    });    frame = new Frame();    // Don't forget to set the layout manager to null!  Otherwise it will    // interfere with your positioning and sizing.    frame.setLayout(null);    frame.add(button);    frame.addWindowListener(exitListener);    frame.setSize(400, 400);    // Dispatch an initial event to randomly place the button.    button.dispatchEvent(			 new ActionEvent(button,					 ActionEvent.ACTION_PERFORMED, null));    frame.setVisible(true);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a