6

How can I place an object in a specific location (x,y) on a JFrame?

0

4 Answers 4

10

Here find the Absolute Positioning Tutorials. Please do read carefully, as to why this approach is discouraged over using LayoutManagers

To add say a JButton to your JPanel, you can use this :

JButton button = new JButton("Click Me");
button.setBounds(5, 5, 50, 30);
panel.add(button);

Here try this example program :

import java.awt.*;
import javax.swing.*;

public class AbsoluteLayoutExample
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Absolute Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);

        JLabel label = new JLabel(
            "This JPanel uses Absolute Positioning"
                                    , JLabel.CENTER);
        label.setSize(300, 30);
        label.setLocation(5, 5);

        JButton button = new JButton("USELESS");
        button.setSize(100, 30);
        button.setLocation(95, 45);

        contentPane.add(label);
        contentPane.add(button);

        frame.setContentPane(contentPane);
        frame.setSize(310, 125);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new AbsoluteLayoutExample().displayGUI();
            }
        });
    }
}

Absolute Positioning Output

6
  • what is this "pane"? can I use JPanel instead? Jun 9, 2012 at 16:33
  • @user1441845 : DO watch this latest edit, how you can use it with JPanel :-) I forgot to tell you before, that you have to write panel.setLayout(null), though I had mentioned that in this code example now :-)
    – nIcE cOw
    Jun 9, 2012 at 16:51
  • for AbsoluteLayout is required to use Insets, not sure if Bounds are correct way +1
    – mKorbel
    Jun 9, 2012 at 17:04
  • @mKorbel : IMHO, setBounds() is used in place of setSize()/setLocation() as a shortcut, instead of using them, we can use setBounds(), since the 1st argument is for X-Axis, 2nd for Y-Axis, 3rd for WIDTH and 4th for HEIGHT.
    – nIcE cOw
    Jun 9, 2012 at 17:09
  • 1
    @nIcE cOw as a shortcut :-) :-) :-) :-) :-)
    – mKorbel
    Jun 9, 2012 at 17:10
3

Try these 2... in combination with each other...

setLocation() and setBounds()

Its even better to use GroupLayout, developed by NetBeans team in 2005. WindowsBuilder Pro is a good tool for Building Gui in java

3
  • +1, wish I could grasp GroupLayout, no doubt one of THE BEST Layout Managers around
    – nIcE cOw
    Jun 9, 2012 at 17:19
  • 1
    @nIcEcOw: tinkering in the NetBeans GUI editor and examining the generated code is one way to experiment with GroupLayout.
    – trashgod
    Jun 9, 2012 at 22:36
  • @trashgod : Interesting idea, drag/drop on the IDE, to experiment and play with the code, the usual way. That's one wonderful piece of advice, Thanks :-)
    – nIcE cOw
    Jun 10, 2012 at 2:48
2

Check out this absolute layout code sample:

Absolute Layout demo

1
  • +1, I had seen this tutorial somewhere else, just cann't remember where :-)
    – nIcE cOw
    Jun 9, 2012 at 17:13
2

In the class inheriting the frame:

setLayout(null); 

In your component:

setLocation(x,y);
2
  • Concerns: 1) you need to set the layout of the JFrame's contentPane, not the JFrame itself. 2) If the container's layout is null, then the coder is totally responsible for both the location and the size of the components that are added. 3) You should add a caveat about avoiding use of null layout and absolute positioning. Jun 9, 2012 at 17:21
  • @HovercraftFullOfEels : BULL'S EYE! I missed that JFrame part, what you caught so easily :-)
    – nIcE cOw
    Jun 9, 2012 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.