2

I'm pretty nooby at layouts... for my application I want to put a panel in the bottom-left corner, with padding, but I'm not sure which layout to use.

I tried using a BorderLayout coupled with horizontal alignment in order to position the panel in the corner, but it did not produce the expected result. My code is as follows:

final JLabel label = new JLabel();
label.setBorder(new EmptyBorder(20, 20, 20, 20));
label.setHorizontalAlignment(JLabel.LEFT);
label.setPreferredSize(new Dimension(100,100));
label.setBackground(Color.WHITE);
label.setOpaque(true);
add(label, BorderLayout.SOUTH);

(the outer class extends JFrame)

The image below details what I want, and what I have. what I want vs what I have

Any tips to achieve this layout? It seems rather simple, but again, I'm new to layouts

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Joe Balin
  • 177
  • 2
  • 17
  • Is this frame only having a JLabel? I recommend GridBagLayout for people just getting into Swing. It's my personal favorite layout, but it's mostly preference. – KyleKW Nov 30 '16 at 15:51
  • Did you get it working with `GridBagLayout`? – ItamarG3 Nov 30 '16 at 16:14
  • I feel like I am kind of intimidated by GridBagLayout, so I actually went for the SpringLayout suggestion that someone posted. It seems to be working nicely, except something with the bottom padding was a little weird. I think SpringLayout will be easy to work with for adding additional features. The finished product will probably have a format similar to this image: http://puu.sh/szyi3/0a6da303f5.png – Joe Balin Nov 30 '16 at 16:19

3 Answers3

2

There are 2 ways I recommend:

  1. You can use GridBagLayout to achieve what you want. Check out the tutorial here. It is flexible enough to give you what you need and for any further add-on components.

  2. Use nested panels with different layouts. For example, a topPanel and a bottomPanel. By default (FlowLayout will be used for your bottomPanel) which will position the JLabel at bottom left hand corner.

user3437460
  • 17,253
  • 15
  • 58
  • 106
1

You can use a SpringLayout.

It's tricky, but it'll do the trick.

SpringLayout springLayout = new SpringLayout();
Container cont = getContentPane();
cont.setLayout(springLayout);

JLabel label = new JLabel("New label");
springLayout.putConstraint(SpringLayout.WEST, label, 20, SpringLayout.WEST, cont);
springLayout.putConstraint(SpringLayout.SOUTH, label, -10, SpringLayout.SOUTH, cont);
cont.add(lblNewLabel);

Spring Layout allows you to set the distance between the sides of components and the frame, and eventually set it's location in a dynamic way (the location will remain relative to the frame when you resize)

this is the output:

output

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • I am trying a SpringLayout right now. However, I'm having trouble getting the bottom-side padding to appear. I noticed you used a negative value, because we want the label to appear ABOVE the center (which is, relatively speaking, a negative y value from the bottom), right? For some reason, I had to triple the padding value before I could actually see any space between the label and the bottom of the screen. Any clue what could be causing that? – Joe Balin Nov 30 '16 at 16:17
  • @JoeBalin What do you mean? can you post a comment with a link to a picture showing what happens when you run the code? – ItamarG3 Nov 30 '16 at 16:19
  • Here is a screenshot showing the code snippet, as well as the a screenshot of results with the triple padding (-60) verses normal (-20). http://puu.sh/szyve/fe138e15e4.png – Joe Balin Nov 30 '16 at 16:24
  • @JoeBalin Ah! Well, you used `this` as an anchor (the object to which we set a relative distance). `this` is a JFrame and refers to the entire window. `cont` refers only to the insides of the frame, as in, not including the `JFrame` borders – ItamarG3 Nov 30 '16 at 16:26
  • Ooh! I thought it would work the same... guess I was wrong! I changed the constraint to use the container as a reference instead, and now it works with the normal padding amount. Thanks! – Joe Balin Nov 30 '16 at 16:32
0

You can Use null Layout, to customise your JFrame. with

 yourFrame.setLayout(null) 

you can

 setLocation(x,y);
 setsize(width,height);
Helvijs
  • 160
  • 13
  • 1
    I would not recommend using setLocation(x,y) to position your objects in Swing. – KyleKW Nov 30 '16 at 15:52
  • I'm slightly new in Java as well, so could you please explain me, why you would not recommend using setLocation(x,y) ? @KyleKW . – Helvijs Nov 30 '16 at 15:54
  • 1
    Layout managers are often used due to them handling complications with changes of screen size and changing the size of your frames/panels. Using a null layout can cause you to run into issues where you will most likely have to change these positions if those variables change. Take a look at this link [here](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing). The top answer will give you more detail. – KyleKW Nov 30 '16 at 15:57
  • @Helvijs, I believe it is essentially "hacking" a layout - as in, hard-coding the exact positions of areas. It's fine in some cases but it's not good practice because of issues that KyleKW mentioned. I appreciate the reply, though! – Joe Balin Nov 30 '16 at 16:00
  • 1
    @KyleKW I understand what you mean and thank you for replying. I just wanted to mention that usually, I do not set location to specific number. For example, if I want to place JTextField in middle of the screen for X and Y coordinates i will use ((width /2), (height / 2)). In this case, buttons will follow the size of, screen as well. But Size off components will stay the same. Anyway, thank you for helping. In future, I will try to avoid null layout according to software complexity ! – Helvijs Dec 02 '16 at 13:06