A Panel with an Etched Border on a Panel with GridBagLayout.
- Code: Select all
public PanelWithBorder() {
initialize();
}
private void initialize() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
add(getPnl(), gbc);
}
protected JPanel getPnl() {
if (pnl == null) {
pnl = new JPanel();
pnl.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
pnl.setName("pnl");
}
return pnl;
}
I put this Panel on the contentpane of a JFrame with Gridbaglayout.
- Code: Select all
/**
* Create the frame.
*/
public FrameForPanelWithBorder() {
initialize();
}
private void initialize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
contentPane.setLayout(gridBagLayout);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
contentPane.add(getPanelWithBorder(), gbc);
}
protected PanelWithBorder getPanelWithBorder() {
if (panelWithBorder == null) {
panelWithBorder = new PanelWithBorder();
panelWithBorder.setName("panelWithBorder");
}
return panelWithBorder;
}
The design page shows the border of the panel, however there
is a gap to the right and the bottom.
If I start the Frame as an application there is no gap. That's O.K.
If I remove the dummy column and row of the "PanelWithBorder" the design page shows no gap.
- Code: Select all
// gridBagLayout.columnWidths = new int[]{0, 0};
// gridBagLayout.rowHeights = new int[]{0, 0};
// gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
// gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.columnWidths = new int[]{0};
gridBagLayout.rowHeights = new int[]{0};
gridBagLayout.columnWeights = new double[]{1.0};
gridBagLayout.rowWeights = new double[]{1.0};
Do you've got an idea?
Kind regards
Chris