Reputation: 951
I have problem with JPanels.I got 1 main panel(GridBagLayout) and there are 4 panels in. I don't want to see this *** gap in the middle anymore , wrr! When I use fill both on panels it works but there is one problem more panels on left cant be resized horizontal. So i seted for thoose panels on left fill vertical, and for thoose on right fill both. It works fine as the left side is not resizing but I still have this gap in the middle and when I resize the frame this gap changes size from small to big ;/
screen: http://i44.tinypic.com/smr42a.png
EDIT: Are u able to fix it when all panels are in some Jpanel(i am using netbeans design mode and it forced to add main Panel)
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame {
public static void main(String[] args) {
final JFrame f = new JFrame();
JPanel p0 = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
p1.setBorder(BorderFactory.createTitledBorder("panel 1"));
p2.setBorder(BorderFactory.createTitledBorder("panel 2"));
p3.setBorder(BorderFactory.createTitledBorder("panel 3"));
p4.setBorder(BorderFactory.createTitledBorder("panel 4"));
p1.setBackground(Color.RED);
p2.setBackground(Color.GREEN);
p3.setBackground(Color.BLUE);
p4.setBackground(Color.CYAN);
f.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.ipadx = 50;
p0.add(p1,c);
c.gridy = 1;
c.fill = GridBagConstraints.VERTICAL;
c.weighty = 1;
p0.add(p2,c);
c.gridx = 1;
c.gridy = 0;
c.ipadx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0;
c.weightx = 1;
p0.add(p3,c);
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.gridy = 1;
p0.add(p4,c);
f.add(p0);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
Upvotes: 0
Views: 2848
Reputation: 1
Set the frame layout to "absolute", then you can move the tabbed pane all the way to the top, that way, it overlaps the frame, and therefore cannot be seen anymore. If you want to put a panel at the top, but freaking hate the tabs, you can set the layout of the tabbed panels to "absolute" and have a panel placed above it, to hide the tabs. It works for me.
Upvotes: 0
Reputation: 10994
Try next example. It hasn't any gap/insets:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame {
public static void main(String[] args) {
final JFrame f = new JFrame();
JPanel p0 = new JPanel(new GridBagLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
p1.setBorder(BorderFactory.createTitledBorder("panel 1"));
p2.setBorder(BorderFactory.createTitledBorder("panel 2"));
p3.setBorder(BorderFactory.createTitledBorder("panel 3"));
p4.setBorder(BorderFactory.createTitledBorder("panel 4"));
p1.setBackground(Color.RED);
p2.setBackground(Color.GREEN);
p3.setBackground(Color.BLUE);
p4.setBackground(Color.CYAN);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.ipadx = 50;
p0.add(p1, c);
c.gridy = 1;
c.fill = GridBagConstraints.VERTICAL;
c.weighty = 1;
p0.add(p2, c);
c.gridx = 1;
c.gridy = 0;
c.ipadx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0;
c.weightx = 1;
p0.add(p3, c);
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.gridy = 1;
p0.add(p4, c);
f.getContentPane().add(p0,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
and it looks like:
panel1
has fixed size.
panel2
resizes only vertically.
panel3
resizes only horizontally.
panel4
resizes vertically and horizontally.
EDIT: Don't set LayoutManager
to contentPane, use default for that(BorderLayout
). Also set GridBagLaout
to your p0
, and all will be work. And I recommend you to read about LayoutManager
's.
Upvotes: 1