Search code examples
javaswingborder-layout

How to get the west and east of a BorderLayout attached?


This is how a panel with a BorderLayout usually looks :

enter image description here

I dont want to put anything in the center, so the east and west are separated with a space. How to get them attached? If there isn't a way, what other Layout should I use instead? Thank you


Solution

  • You can simply add graphical elements in the desired sections and ignore the others:

    add(element1, BorderLayout.WEST);
    add(element2, BorderLayout.EAST);
    

    If you want a space in the center, add it explicitly:

    add(new JLabel(" "), BorderLayout.CENTER);