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