Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the padding between in the JPanel still using a flow layout?

Here's the portion of my java application GUI that I have a question about.

enter image description here

What this GUI consists is a blue JPanel(container) with default FlowLayout as LayoutManager that contains a Box which contains two JPanels(to remove the horizontal spacing or i could have used setHgaps to zero for that matter instead of a Box) that each contains a JLabel.

Here's my code for creating that part of the GUI.

  private void setupSouth() {

    final JPanel southPanel = new JPanel();
    southPanel.setBackground(Color.BLUE);

    final JPanel innerPanel1 = new JPanel();
    innerPanel1.setBackground(Color.ORANGE);
    innerPanel1.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
    innerPanel1.add(new JLabel("Good"));

    final JPanel innerPanel2 = new JPanel();
    innerPanel2.setBackground(Color.RED);
    innerPanel2.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
    innerPanel2.add(new JLabel("Luck!"));

   final Box southBox = new Box(BoxLayout.LINE_AXIS);
  southBox.add(innerPanel1);
  southBox.add(innerPanel2);

    myFrame.add(southPanel, BorderLayout.SOUTH);
}

My question is how would i get rid the vertical padding between the outer JPanel(the blue one) and the Box?

I know this is padding because i read on Difference between margin and padding? that "padding = space around (inside) the element from text to border."

This wouldn't work because this has to due with gaps(space) between components.- How to remove JPanel padding in MigLayout?

I tried this but it didn't work either. JPanel Padding in Java

like image 425
committedandroider Avatar asked Dec 07 '14 22:12

committedandroider


1 Answers

You can just set the gaps in the FlowLayout, i.e.

FlowLayout layout = (FlowLayout)southPanel.getLayout();
layout.setVgap(0);

The default FlowLayout has a 5-unit horizontal and vertical gap. Horizontal doesn't matter in this case as the BorderLayout is stretching the panel horizontally.

Or simple initialize the panel with a new FlowLayout. It'll be the same result.

new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

Edit:

"I tried that, didn't work.."

Works for me...

enter image description here     enter image description here

     Setting the gap ↑              Not setting the gap ↑

import java.awt.*;
import javax.swing.*;

public class Test {

    public void init() {
        final JPanel southPanel = new JPanel();
        FlowLayout layout = (FlowLayout)southPanel.getLayout();
        layout.setVgap(0);
        southPanel.setBackground(Color.BLUE);

        final JPanel innerPanel1 = new JPanel();
        innerPanel1.setBackground(Color.ORANGE);
        innerPanel1.add(new JLabel("Good"));

        final JPanel innerPanel2 = new JPanel();
        innerPanel2.setBackground(Color.RED);
        innerPanel2.add(new JLabel("Luck!"));

        final Box southBox = new Box(BoxLayout.LINE_AXIS);
        southBox.add(innerPanel1);
        southBox.add(innerPanel2);

        southPanel.add(southBox);   // <=== You're also missing this

        JFrame myFrame = new JFrame();
        JPanel center = new JPanel();
        center.setBackground(Color.yellow);
        myFrame.add(center);
        myFrame.add(southPanel, BorderLayout.SOUTH);
        myFrame.setSize(150, 100);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLocationByPlatform(true);
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new Test().init();
            }
        });
    }
}

Note: Always post a runnable example (as I have done) for better help. You say it doesn't work, but it always works for me, so how would we know what you're doing wrong without some code that will run and demonstrate the problem?

like image 147
Paul Samsotha Avatar answered Oct 12 '22 23:10

Paul Samsotha