How to place a JButton at a desired location in a JFrame using Java?
Asked Answered
L

7

23

I want to put a JButton at a particular coordinate in a JFrame. I used setBounds() for the JPanel (which I placed on the JFrame) and also setBounds() for the JButton. However, they don't seem to function as expected.

My Output:

alt text

This is my code:

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Control extends JFrame {

    // JPanel
    JPanel pnlButton = new JPanel();
    // Buttons
    JButton btnAddFlight = new JButton("Add Flight");

    public Control() {
        // FlightInfo setbounds
        btnAddFlight.setBounds(60, 400, 220, 30);

        // JPanel bounds
        pnlButton.setBounds(800, 800, 200, 100);

        // Adding to JFrame
        pnlButton.add(btnAddFlight);
        add(pnlButton);

        // JFrame properties
        setSize(400, 400);
        setBackground(Color.BLACK);
        setTitle("Air Traffic Control");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Control();
    }
}

How do I place the JButton at coordinate (0, 0)?

Lifeblood answered 7/7, 2010 at 14:24 Comment(0)
E
23

Following line should be called before you add your component

pnlButton.setLayout(null);

Above will set your content panel to use absolute layout. This means you'd always have to set your component's bounds explicitly by using setBounds method.

In general I wouldn't recommend using absolute layout.

Emmieemmit answered 7/7, 2010 at 14:29 Comment(0)
W
4

Use child.setLocation(0, 0) on the button, and parent.setLayout(null). Instead of using setBounds(...) on the JFrame to size it, consider using just setSize(...) and letting the OS position the frame.

//JPanel
JPanel pnlButton = new JPanel();
//Buttons
JButton btnAddFlight = new JButton("Add Flight");

public Control() {

    //JFrame layout
    this.setLayout(null);

    //JPanel layout
    pnlButton.setLayout(null);

    //Adding to JFrame
    pnlButton.add(btnAddFlight);
    add(pnlButton);

    // postioning
    pnlButton.setLocation(0,0);
Wafer answered 7/7, 2010 at 14:28 Comment(2)
The Java tutorial recommends setBounds. setLocation just delegates to setBounds anyway.Lawn
@Mark_Peters True, though I was (perhaps wrongly) assuming that in asking to set the position, like most apps, the JButton was to be sized automatically. setLocation does invoke setBounds, but it uses the components current width and height, letting it size naturally based on contents. Typical, but you're right, not always what is wanted.Wafer
F
2

I have figured it out. For the button do .setBounds(0, 0, 220, 30) The .setBounds layout has following parameters - (int x, int y, int width, int height)

Fluorspar answered 21/4, 2013 at 22:15 Comment(0)
N
2

Define somewhere the consts :

private static final int BUTTON_LOCATION_X = 300;  // location x 
private static final int BUTTON_LOCATION_Y = 50;   // location y 
private static final int BUTTON_SIZE_X = 140;      // size height
private static final int BUTTON_SIZE_Y = 50;       // size width

and then below :

                JButton startButton = new JButton("Click Me To Start!");
                // startButton.setBounds(300, 50,140, 50 );
                startButton.setBounds(BUTTON_LOCATION_X
                                    , BUTTON_LOCATION_Y,
                                      BUTTON_SIZE_X, 
                                      BUTTON_SIZE_Y );
                contentPane.add(startButton);

where contentPane is the Container object that holds the entire frame :

 JFrame frame = new JFrame("Some name goes here");
 Container contentPane = frame.getContentPane();

I hope this helps , works great for me ...

Neptune answered 10/8, 2013 at 7:34 Comment(0)
C
2

You should set layout first by syntax pnlButton.setLayout(), and then choose the most suitable layout which u want. Ex: pnlButton.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));. And then, take that JButton into JPanel.

Cence answered 23/7, 2018 at 11:10 Comment(0)
C
1

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.

I created the following GUI.

Air Traffic Control

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

I used a JFrame. I did not extend a JFrame. This is referred to as composition over inheritance. The only reason you should extend a Swing component, or any Java class, is if you intend to override one or more of the class methods.

A JFrame has a default BorderLayout. I placed the JPanel in the center of the JFrame.

I created a JPanel to place in the JFrame. I used a FlowLayout to place the JButton in the center of the top of the JPanel. If you want the JButton to be somewhere else, you can adjust the values of the empty border to place the JButton where you want.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AirTrafficControl implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new AirTrafficControl());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Air Traffic Control");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
        panel.setPreferredSize(new Dimension(400, 400));
        
        JButton btnAddFlight = new JButton("Add Flight");
        panel.add(btnAddFlight);
        
        return panel;
    }

}
Constitutionally answered 13/4, 2022 at 19:7 Comment(0)
R
0

First, remember your JPanel size height and size width, then observe: JButton coordinates is (xo, yo, x length , y length). If your window is 800x600, you just need to write:

JButton.setBounds(0, 500, 100, 100);

You just need to use a coordinate gap to represent the button, and know where the window ends and where the window begins.

Raimund answered 26/10, 2013 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.