12 Replies - 306 Views - Last Post: 13 February 2013 - 09:47 PM Rate Topic: -----

#1 neo112  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-November 12

Problem with component placement in JFrame

Posted 13 February 2013 - 08:30 AM

Hi,

I am having a hard time trying to change the location of Swing components in my JFrame. I've tried using setBounds(x,y, width, height) and setLocation (x,y) methods but they seems to have no effect on the placement of buttons, labels, textfields and so on. Could someone point me in the right direction and say what I am missing?

Here's my test code:

import javax.swing.*;


public class Guitemp {
	JFrame f = new JFrame("Main Window");
	JPanel p = new JPanel();
	JButton b1 = new JButton("Testbutton");
	JLabel l1 = new JLabel("Label");

	public Guitemp() {
		gui();
	}

	public void gui() {
	
			f.setSize(400,400);
			f.setVisible(true);
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			p.add(b1);
			b1.setBounds(50, 50, 35, 25); // this should move the button to 50 px right+down from top left corner
			//and make its size 35x25 px but it does nothing. the button stay in one place.
			
			//b1.setLocation(50, 50); this doesnt work either
			
			l1.setBounds(100, 100, 30, 30); // doesnt change size nor placement
			p.add(l1);
			f.add(p);
	}
	
	public static void main (String [] args) {
		new Guitemp();
	}
 
}




Is This A Good Question/Topic? 0
  • +

Replies To: Problem with component placement in JFrame

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 09:42 AM

JPanel is using it's default LayoutManager - FlowLayout. This will automatically change the size and location of your components when your GUI requests that the panel should be laid out. You can try using panel.setLayout(null), but this is discouraged here because LayoutManagers take care of a lot of things for you (such as changing the locations and sizes of components when the frame is resized, etc.) and rewriting that code yourself is not usually a good solution.

Usually when you have a GUI in mind, you can find a combination of LayoutManagers that does the job without ever needing to manually set the location of a component. Of course, for testing purposes, just set your layout to null, see what it can and can't do and then try some LayoutManagers to see what they do to your components. Resize the frame for each test.
Was This Post Helpful? 2
  • +
  • -

#3 neo112  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-November 12

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 11:00 AM

Thanks for your reply, I changed panel layout to null and I'm now able to move all the components to where ever I want. Thats good for learning.

There's another question however, is it possible to make the frame stay in one size? I don't want the user to click+drag and change the size of the window.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 863
  • Posts: 2,652
  • Joined: 29-July 11

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 11:31 AM

setSize(300,300) and setResizable(false).
Was This Post Helpful? 1
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 863
  • Posts: 2,652
  • Joined: 29-July 11

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 11:45 AM

As for keeping the window in one spot, I assume you mean to prevent someone from grabbing the titlebar and dragging the window?

I suppose you could set it undecorated. You could probably set up a swing timer that calls setLocation(x,y) every half second or so. Moving the frame is OS functionality, so I have no idea how successful you c an be with that.
Was This Post Helpful? 0
  • +
  • -

#6 neo112  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-November 12

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 11:59 AM

I was trying to keep the user from changing the default size by dragging the window size because my components don't automatically readjust to those changes.

Also, there seems to be some kind of a glitch, when I run the program the Frame is empty and the components only get loaded into it once I have resized the frame by dragging it larger with mouse. What could be the cause of this ?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 12:32 PM

Quote

I was trying to keep the user from changing the default size by dragging the window size because my components don't automatically readjust to those changes.
That's because you're not using a layout manager ;) Layout manages exist precisely to take all the work out of manually resizing/repositioning components when the window size changes. Your users might not take kindly being prevented from resizing the window - i wouldn't. Moral of the story - learn how to use layout managers ;)
Was This Post Helpful? 1
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 863
  • Posts: 2,652
  • Joined: 29-July 11

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 12:46 PM

View Postneo112, on 13 February 2013 - 06:59 PM, said:

Also, there seems to be some kind of a glitch, when I run the program the Frame is empty and the components only get loaded into it once I have resized the frame by dragging it larger with mouse. What could be the cause of this ?


Post your mot recent code. It doesn't happen for me with your first posted code.
Was This Post Helpful? 0
  • +
  • -

#9 neo112  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-November 12

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 01:02 PM

But the reason I want the window to remain in a selected size is because it doesn't hold much information, just name, age, job, gender etc fields and it would look funny if users drag it to full resolution, just a 400x300 window will suffice.

In reality I don't need the window to remain the same size all the time because I figured I can use frame.getHeight()/Width() and managing pixels is not a problem for me.

As for the components not loading before resizing, it's pretty random, around 50% of the time I run the program everything starts as it should, else a click+drag is needed to start to see the components. How could I use just a random Layout to verify that the problem gets resolved when a Layout is used? atm i use panel.setLayout(null), but I have no real idea how to try out different layouts.
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 863
  • Posts: 2,652
  • Joined: 29-July 11

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 01:44 PM

Eh, besides some thread safety issues with adding to the frame on a thread other than the edt after setvisible is called, I don't technically see any problems with the code. Make setVisible() the last call in the constructor. Maybe that will help. All swing components should be created and manipulated only on the event dispatch thread, so you will need to learn that, too. :) I amm nsure there are some swing tutorials in the tutorials section regarding it.
Was This Post Helpful? 1
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 02:30 PM

Quote

just a 400x300 window will suffice.

What are you going to do if

a. those dimensions are large enough to cause signficant scrolling of the display?
b. those dimensions are so tiny in the display that the windows becomes difficult to see and enter text?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 03:17 PM

View Postneo112, on 13 February 2013 - 03:02 PM, said:

But the reason I want the window to remain in a selected size is because it doesn't hold much information, just name, age, job, gender etc fields and it would look funny if users drag it to full resolution, just a 400x300 window will suffice.

Quote

Moral of the story - learn how to use layout managers

And set your Frame .setResizable(false);
Was This Post Helpful? 0
  • +
  • -

#13 neo112  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-November 12

Re: Problem with component placement in JFrame

Posted 13 February 2013 - 09:47 PM

View Postfarrell2k, on 13 February 2013 - 01:44 PM, said:

Make setVisible() the last call in the constructor.


thanks, it worked! :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1