JFrames - Why doesn't setLocation work for window components?


In my JFrame code below,

I am trying to place my JButton at x,y co-ordinates inside the frame, but even though the size is smaller than the JFrame, the JButton becomes the size of my JFrame and is not at the location in the JFrame I specify! Why?

Is setLocation not what I think in the JFrame? why is the JButton so huge?
Thx
import javax.*;
import javax.swing.*;


public class serverTester extends JFrame {
	
	JButton createClientButton;
	
	
	public serverTester() {
		
		this.setSize(600, 300);
		this.setLocation(200,200);
		
		createClientButton = new JButton();
		
		createClientButton.setLocation(200, 100);
		createClientButton.setSize(100,100);
		createClientButton.setText("create");
		add(createClientButton);
		
		this.setVisible(true);
		this.toFront();
	}

}

Open in new window

LVL 1
beavoidAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
setLocation() is the method which specifues the location of top winodw
on youyr screen . The lements are governened bu the layouts, etc. and cannot be plcaes using setLocation
0
for_yanCommented:
setLocation can be used when you are placing your JFrame on the screen
It cannot be used to locate the JButton inside JFrame.
You should use layouts - such as FloawLayoout, BorderLayout, GrodLayour, ect
to lay out compomnents in your frame
0
for_yanCommented:
There fore your fisrt setLocation  operator
this.setLocation(200,200);

will work and place your JFrame


the second one will not work:
createClientButton.setLocation(200, 100);

0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

for_yanCommented:


look at layouts on this page:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

It is not common to specify the loaction of the button on the frmae in this way with x and y;
normally you would manage your whole layout of componenets and think of them
in termas of BorderLayout, FlowLayout, GridLayout, soemtimes more sphisiticates GridBagLayout.

though probably with soem less used layouts like SpringLayout, GroupLayout - you could
achieve what you want (NetBeans iuses GroupLayout to do automatic design of the elements by IDE designer)
 - but it is prbably not the best thing to do when you do it manually - withouyt designer,
 easepcially ltaking into account what happens with resizing etc.


So beeter still to stick with more conventional layouts.
0
for_yanCommented:
Maybe is you set Layout of you root compoenent to null, your
setLocation for JButton would still work?  frankly I never tried
0
for_yanCommented:
this works, though not redommended:

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


public class serverTester extends JFrame {

	JButton createClientButton;


	public serverTester() {

		this.setSize(600, 300);
		this.setLocation(200,200);

        Container c = this.getContentPane();
        c.setLayout(null);

		createClientButton = new JButton();

		createClientButton.setLocation(200, 100);
		createClientButton.setSize(100,100);
		createClientButton.setText("create");
		c.add(createClientButton);

		this.setVisible(true);
		this.toFront();
	}

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

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
0
for_yanCommented:
Obviously normally default layout overrides setLocation for components.
As soon as you set null as default layout of the top component
it will honor setLocation method

Anyway, all waht I said before is true - it is normal to use setLocation ofr placing the top window on the screen
and lay out comnponents using the layouts
0
beavoidAuthor Commented:
Excellent

Thanks

not recommended because?
0
for_yanCommented:
One reason which is obvious for me - just take my code and try to resize it - it does not resize well

I'm sure when you try more to work with it you'll see any other reasonas - you'll need to manage youself many things which itlauyout does for you automatically
0
for_yanCommented:
Another obviosu reason - you'll need to calculate your slef how they are placed relative to each other,
make it more or less beatifyul, and for te applicatiin use it is normally deoes not matter
if you place it at x 200 or 250, what matters that they are laioout nicely rekative to each other, etc.
So common layouts manage the patterns ofg good layouts for you - that is my underasatnding
0
HonorGodSoftware EngineerCommented:
btw, the technique that I stumbled upon recently was to use the setLocationRelativeTo( null ) to center the application frame in the middle of the screen...

http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationRelativeTo%28java.awt.Component%29
0
beavoidAuthor Commented:
Perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.