0

I am trying to create a toolbar that will go at the top of all the pages in my java swing application.

I am creating a JPanel with a series of individual JPanels (containers) inside it. Each JPanel (container) has a north and south component or just a north component, set up using a GridLayout.

My problem is, I want a small gap between the north and south component, but I can't see how to do it and have not been able to find any help on the internet.

Below is a working example for the code for one of the containers:

public static void CustomersGui(){

        final JFrame frame = new JFrame("Nested Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel container1 = new JPanel();
        container1.setLayout(new GridLayout(2,1));
        JButton buttonDiary = new JButton("Diary");
        buttonDiary.setPreferredSize(new Dimension(140, 25));
        JButton buttonCars = new JButton("Cars");
        buttonCars.setPreferredSize(new Dimension(140, 25));
        container1.add(buttonDiary, BorderLayout.NORTH);
        container1.add(buttonCars, BorderLayout.SOUTH);


        frame.setContentPane(container1);

        frame.pack();


        frame.setVisible(true);
    }
  • 1
    Note that the `GridLayout` has a constructor that additionally allows you to specify a "hgap" and "vgap": The size of a gap between the components, horizontally and vertically. Alternatively, assigning an `BorderFactory.createEmptyBorder(1,2,3,4)` to the *inner* component is one possible way of adding empty spaces (but should only be used if there is no appropriate layout option to achieve the desired goal) – Marco13 Oct 28 '14 at 16:01
  • 2
    Please have a look at [providing whitespace, in a Swing GUI](http://stackoverflow.com/q/17874717/1057230). Hopefully this will help :-) – nIcE cOw Oct 28 '14 at 16:55
  • Moreover, you can use [Border](http://docs.oracle.com/javase/tutorial/uiswing/components/border.html), with any Swing component :-) – nIcE cOw Oct 28 '14 at 17:10

1 Answers1

5

I am trying to create a toolbar that will go at the top of all the pages in my java swing application.

Why don't just use JToolBar? See How to use Tool Bars

I am creating a JPanel with a series of individual JPanels (containers) inside it. Each JPanel (container) has a north and south component or just a north component, set up using a GridBagLayout.

Based on your code none of these statements is true: you are adding buttons (not panels) and you are using GridLayout not GridBagLayout:

JPanel container1 = new JPanel();
container1.setLayout(new GridLayout(2,1)); // GridLayout
JButton buttonDiary = new JButton("Diary"); // button here
buttonDiary.setPreferredSize(new Dimension(140, 25));
JButton buttonCars = new JButton("Cars"); // another button here

Besides you are using BorderLayout constraints that will be totally ignored by either GridLayout or GridBagLayout:

container1.add(buttonDiary, BorderLayout.NORTH);
container1.add(buttonCars, BorderLayout.SOUTH);

You should have a look to the whole Laying Out Components Within a Container lesson to learn about layout managers and how do all of them work.

In addition

As wisely pointed out by @nIcEcOw, since Java 1.4 BorderLayout the use of new constants is highly encouraged:

PAGE_START
PAGE_END
LINE_START
LINE_END
CENTER

From How to Use BorderLayout tutorial (bold text mine):

Before JDK release 1.4, the preferred names for the various areas were different, ranging from points of the compass (for example, BorderLayout.NORTH for the top area) to wordier versions of the constants we use in our examples. The constants our examples use are preferred because they are standard and enable programs to adjust to languages that have different orientations.

Update

Well now that your question has been edited to say that you actually use GridLayout it's easy to answer by saying that you can specify horizontal and vertical gaps between components either by using class' constructor: or setHgap() and setVgap() methods:

JPanel container1 = new JPanel(new GridLayout(2, 1, 8, 8));

// Or

GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
gridLayout.setColumns(1);
gridLayout.setHgap(8);
gridLayout.setVgap(8);    
JPanel container1 = new JPanel(gridLayout);

Don't forget to remove BorderLayout constraints when you add buttons to container1 panel, because those will be ignored:

container1.add(buttonDiary);
container1.add(buttonCars);

You might want to take a look to this topic as well: Providing white space in a Swing GUI

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    Please try to use `BorderLayout.PAGE_START` and `BorderLayout.PAGE_END` for `BorderLayout.NORTH` and `BorderLayout.SOUTH` respectively. Java is actively, encouraging programmers to write the new names instead of the previous one's since JDK 1.4 :-) – nIcE cOw Oct 28 '14 at 17:08
  • @dic19 if you read the question properly you will see it says this is a working example of the code for one of the containers. The code was edited to comply with MCVE principles –  Oct 28 '14 at 21:56
  • 1
    Thanks for pointing that out @nIcEcOw Bad habits are hard to leave behind. I've just edited my answer to include your comment :D – dic19 Oct 28 '14 at 22:10
  • Emmm... It's Minimal and Complete, but not Verifiable: it doesn't reproduce your problem. At least the problem stated in the question: *Leave small space between buttons when using GridBagLayout*. @jim – dic19 Oct 28 '14 at 22:25
  • @dic19, you are right, I have now edited to say GridLayout. You were incorrect in your first criticism but correct in your second. –  Oct 29 '14 at 01:56
  • Please see my update. Hope you can solve your problem now :) @jim – dic19 Oct 29 '14 at 11:26