Forums Register Login

Java GUI and Placement

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
[Screenshot.jpg]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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.



+Pie Number of slices to send: Send
See the Swing chapter of the Java Tutorial - it has an section devoted to the various layout managers.
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1131 times.
Similar Threads
how to encrypt data into an image
Pixels to Feet Calculation
Draging componets like JButtons during runtime in JFrame
Swings Problem!!!!
Unable to implement detection of click on a JButton using Swing and AWT
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:24:13.