4

Here is my problem

enter image description here

How can I get rid of the gap from the inner rectangle box (it is actually the border of the middle JPanel)?

The outter rectangle is a extends JCompoment. Inside it contains three JPanels. Each of them use GridLayout. I even try to setHgap to a negative value in the big rectangle, but it doesn't change anything.

Edit: Sorry the question is not clear. I do want the border, but I don't want the gap between the inner border and outer border. If there is no gap in between, the whole rectangle will represent a class diagram class.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bili
  • 610
  • 2
  • 9
  • 20

4 Answers4

7

You may mean this way:

enter image description here

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

public class GridBadFrame {

    private JFrame frame;
    private JPanel pnlCenter;
    private JPanel pnl1;
    private JPanel pnl2;
    private JPanel pnl3;

    public GridBadFrame() {
        pnl1 = new JPanel();
        pnl1.setBackground(Color.red);
        pnl2 = new JPanel();
        pnl2.setBackground(Color.blue);
        pnl3 = new JPanel();
        pnl3.setBackground(Color.red);
        JLabel lblWest = new JLabel();
        lblWest.setPreferredSize(new Dimension(50, 150));
        JLabel lblEast = new JLabel();
        lblEast.setPreferredSize(new Dimension(50, 150));
        JLabel lblNorth = new JLabel();
        lblNorth.setPreferredSize(new Dimension(600, 50));
        JLabel lblSouth = new JLabel();
        lblSouth.setPreferredSize(new Dimension(600, 50));
        pnlCenter = new JPanel();
        pnlCenter.setBackground(Color.black);
        pnlCenter.setLayout(new java.awt.GridLayout(3, 0, 10, 10));
        pnlCenter.setPreferredSize(new Dimension(600, 400));
        pnlCenter.add(pnl1);
        pnlCenter.add(pnl2);
        pnlCenter.add(pnl3);
        frame = new JFrame();
        frame.add(pnlCenter, BorderLayout.CENTER);
        frame.add(lblNorth, BorderLayout.NORTH);
        frame.add(lblSouth, BorderLayout.SOUTH);
        frame.add(lblWest, BorderLayout.WEST);
        frame.add(lblEast, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                GridBadFrame gridBadFrame = new GridBadFrame();
            }
        });
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Yes! That is exactly what I want. Oh, I see what you done there! Haha that is pretty neat trick. You delibrately left a gap between the panels so that the background of the central panel will show as the divider. Simple yet does the job that I want. cheers – bili Aug 23 '11 at 14:35
2

it is actually the border of the middle JPanel

If it is the border, remove that (setBorder(null)).

Thomas
  • 87,414
  • 12
  • 119
  • 157
1

I think if you try setBorderPainted(false) it may get rid of the unwanted border.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
  • 1
    isn't the space the issue in this question? While you may choose to not paint the border, the component will still take up space, only that it will not be visible to you while its empty! – gotomanners Aug 23 '11 at 13:14
0

The issue is probably to do with the preferred height and width set by the GridLayout on the JPanel.

Excerpt from the documentation:

The preferred width of a grid layout is the largest preferred width of any of the components in the container times the number of columns, plus the horizontal padding times the number of columns plus one, plus the left and right insets of the target container.

The preferred height of a grid layout is the largest preferred height of any of the components in the container times the number of rows, plus the vertical padding times the number of rows plus one, plus the top and bottom insets of the target container.

gotomanners
  • 7,808
  • 1
  • 24
  • 39