Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GUI and Placement  RSS feed

 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I open a new JFrame and I want to put a swing component (let's say a JButton) at a specific location inside this JFrame.
I don't want this location to be exactly in the center\north\south\etc, and I don't want to be restricted by any Layout form.
For example, suppose I want that the upper-left corner of the JButton will be placed in pixel-location (50,30) and the JButton pixel size is 25x65. How can I do it with Java?
 
Henry Wong
author
Sheriff
Posts: 22847
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:Suppose I open a new JFrame and I want to put a swing component (let's say a JButton) at a specific location inside this JFrame.
I don't want this location to be exactly in the center\north\south\etc, and I don't want to be restricted by any Layout form.
For example, suppose I want that the upper-left corner of the JButton will be placed in pixel-location (50,30) and the JButton pixel size is 25x65. How can I do it with Java?


Layout managers are actually optional -- you don't have to use them. Of course, without them, you'll have to explicitly set the bounds for all the components in the container.

Henry
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If so, can you give a small code example that demonstrates how to do it?
what method is used in order to locate the component in the appropriate place?

For example, I wrote the following code:



What I get is a JButton that covers the entire JFrame. I want the upper-left corner of the JButton to be located in (10,10) and the size of the JButton is 50x50. How can I correct my code?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I advise always to use a layout. But if for some reason (which one?) you have decided against that, read http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf is right: always use a layout.
If you want it to be in the middle, try border layout with blank panels added to PAGE_START PAGE_END LINE_START and LINE_END and put your button in CENTER. There are doubtless many other ways to do that.

Moving discussion to our GUIs forum
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to how my question is simply add the line:


and then use the method setBounds(int x, int y, int height, int width) for every component.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is the answer to how to make an inflexible GUI. A null layout will look a right mess if you resize the display.
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers!

If you recommend so strongly on working with Layouts, how would you write a class based on layouts that will create a frame just as the one attached to this post?
Screenshot.jpg
[Thumbnail for Screenshot.jpg]
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start out with a horizontal BoxLayout containing three panels. You can see the three panels there; the first contains the two check boxes, the second contains the X and Y entries, and the third contains the three buttons. The three buttons look like a vertical FlowLayout, and the other two... I don't know, I haven't done this for a while but you could try several options and see what works best for them.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I would start out with a horizontal BoxLayout containing three panels. You can see the three panels there; the first contains the two check boxes, the second contains the X and Y entries, and the third contains the three buttons. The three buttons look like a vertical FlowLayout, and the other two... I don't know, I haven't done this for a while but you could try several options and see what works best for them.


This will absolutely work. Also for the first 2 panels in the BoxLayout containing the check boxes and the text fields, they can be achieved using the GridBagLayout inside the BoxLayout.

So a BoxLayout for the JFrame with 3 JPanels inside. The first 2 panels will have the GridBagLayout and the third will use a FlowLayout. By modifying a couple parameters here and there you can get it to look just like this.

Layout managers are very powerful and the preffered way to build a GUI in Swing as they handle a lot of issues such as screen and window resizing for you.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a usability perspective, I would move the buttons into the bottom row, which is where one would expect them. The 4 input elements can be a 2x2 GridLayout - why would the checkboxes be closer together than the input fields? That doesn't make sense IMO, and is less visually pleasing (which is of course debatable).
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
Can you give me a code example for doing that?

I'm not familiar with dividing the frame to three areas, and work with different manager layout in each area.
A code example will be extremely helpful for me.



 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the Swing chapter of the Java Tutorial - it has an section devoted to the various layout managers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!