How to place a JButton at a desired location in a JFrame using java

Hey, I want to put a Jbutton on a particular coordinate in a JFrame. I put setBounds for the JPanel (which I placed on the JFrame) and also setBounds for the JButton. However, they dont seem to function as expected.

My Output:

How to place a JButton at a desired location in a JFrame using java

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 can place the JButton at coordinate (0,0) ?

Many Thanks

-------------Problems Reply------------

Following line should be called before you add your component


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.

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 pnlButton = new JPanel();
JButton btnAddFlight = new JButton("Add Flight");

public Control() {

//JFrame layout

//JPanel layout

//Adding to JFrame

// postioning

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 );

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 ...

I have figured it out lol. for the button do .setBounds(0, 0, 220, 30) The .setBounds layout is like this (int x, int y, int width, int height)

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.

Category:java Views:0 Time:2010-07-07

Related post

  • How to add a dynamically created node(tag) at the desired location? 2010-01-02

    Suppose I have two DIVs on my page. <body> <div id="d1"></div> <div id="d2"></div> </body> I want to create a new DIV dynamically using JavaScript and place it between d1 and d2. If I use the appendChild method the

  • How to place JButtons at a certain coordinate in a JFrame 2010-07-03

    I want to know how to place JButtons at a particular coordinate in the JFrame. All day I have seen layouts. This does not suit my purpose. I would prefer something like setBounds. Rumour has it that it does not work but setLocation does. I tried it b

  • Show route between current and desired location on iPhone MapView 2011-12-22

    I want to show a route on a MKMapView between the current location and a desired location as an annotation. What is the best way to do this? --------------Solutions------------- ///in .h add delegate MKMapViewDelegate ///in .m file - (void)locationMa

  • How to set a position of a popup to a desired location when clicked on link using jquery 2012-03-01

    when i click on a link a pop up is displayed but not a desired location my question is how to set it at a particular location.. my code is function showPopupforAssignSubject(id) { $(document).ready(function() { $("#assign_subjects_"+id).jqpopup_open(

  • Excel copy or selection not showing selected cells and i am unable to insert it desired location 2012-05-12

    i am using Microsoft Excel 2003 in which i am facing problem in copy/cut. after selecting the cells it is not showing selected sells to paste with flickering border and i am unable to insert it desired location. It showing the flickering border for a

  • Desktop olders don't open to desired locations and sizes on desktop.__ 2012-08-03

    Desktop Folders Don't open to desired locations and sizes on desktop. My desktop folders all open to the same size; I used to get them to open to a size & location on the desktop customized to each individual folder, so how do I get back the cust

  • Desired Location for Files 2014-04-15

    How can I set the desired location for my transfered files from WXP to W7 with WET? The files have dumped into the OS?C drive. There is hardly any data in the Data Drive D. --------------Solutions------------- Hi, These should help : (References to V

  • Internet Explorer will not stay at the desired location 2014-11-25

    Hi, How do you force Internet Explorer to stay at the selected location... I prefer than it be at the center of the screen, but it keeps going back to the upper left corner.... I know that if you open Internet Explorer and drag it to a position, that

  • I'm tired of JButtons, how can I make a nicer GUI in java? 2010-04-11

    So far I've only built "small" graphical applications, using swing and JComponents as I learned at school. Yet I can't bear ugly JButtons anymore. I've tried to play with the different JButton methods, like changing colors, putting icons etc. but I'm

  • Have mstest output my log file at a desired location 2012-03-06

    I have test that runs in mstest, everything works fine and it gives me a log file. But the log file is outputted in the TestResults folder in the project. Is there a setting I can change in mstest, which upon changing lets me output the log file to a

  • When is it desired to not implement toString() in Java? 2009-07-21

    A lead developer on my project has taken to referring to the project's toString() implementations as "pure cruft" and is looking to remove them from the code base. I've said that doing so would mean that any clients wishing to display the objects wou

  • how to show mapkit blue dot(user's location) at desired location? 2009-10-22

    i am using map kit.i want to know that how we can get current user location.the mapkit shows blue dot at current user location.i want to do something like this.the user location is showed on map.i have button which on clicking again get the current u

  • add text to UITextField at desired location 2009-12-18

    I am quite new to iphone development. I have a situation here. I have some labels which can be dragged across the screen. When any of these labels are dragged to some textfield and released over a textfields UIlabel test is assigned to that text fiel

  • How to get a desired substring form a String in java or jsf? 2010-03-01

    I am developing an application using JSF in Eclipse IDE with Derby as database. I have a feature to upload files to the database. But the file name is getting stored as "C:\Documents and Settings\Angeline\Desktop\test.txt" instead of "test.txt". How

  • Placing blue dot in mapKit with a desired location 2010-05-11

    For demonstration purpose, i need to simulate the user location in a Mapkit view. It seems that it is possible with an undocumented API to place the blue dot anywhere on the map view. Unfortunately, i don't know witch undocumented API to use ? Any he

  • How to place stored procedure in desired location? 2010-09-12

    On SQL Server 2008, how can I place my stored procedures in "Stored Procedures" folder of my DB? When I declare it this way: CREATE PROCEDURE mySchema.myProc It goes to: MYSERVER\System Databases\Master\Programmability\Stored procedures folder. How t

  • Copy a file and paste it to the desired location 2011-03-17

    I want to copy a file from say /mnt/sdcard/Pictures and paste to the location /mnt/sdcard/Pictures/myselection.. how to code it?? --------------Solutions------------- Use Java I/O. Do not hardcode /mnt/sdcard as a path, as that is invalid on some And

  • Save Export CSV to desired location - PHP 2011-04-11

    I have a code like this. header("Content-type: application/"); header("Content-Disposition: attachment; filename=\"$filename\""); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); header("Pragma: pu

  • DBRequest#connectionDidFinishLoading: error moving temp file to desired location: The operation couldn't be completed. (Cocoa error 4.) 2011-09-09

    i am using Dropbox api in my project and i get this error when i go deep into some directory and then download a file from that folder to iPhone. --------------Solutions------------- I don't think you're handling this issue correctly. The reason remo

Copyright (C), All Rights Reserved.

processed in 0.108 (s). 11 q(s)