Its pretty basic UI, but I cannot setup the JCheckBox
buttons so that they are placed immediately after one another (vertically) without any spacing. How would I reduce the spacing seen below?
JPanel debugDrawPanel = new JPanel(new GridLayout(0,1));
JPanel eastPanel = new JPanel(new GridLayout(1,0));
JTabbedPane tab = new JTabbedPane();
click = new ClickPanel(this);
setSettings(new Settings());
for (Setting setting: getSettings().getAll()){
JCheckBox checkBox = new JCheckBox(setting.name);
checkBox.setName(setting.name);
checkBox.addItemListener(new CheckBoxItemListener(this));
debugDrawPanel.add(checkBox);
}
tab.addTab("Object Parameters", click);
tab.addTab("Debug Draw", debugDrawPanel);
It appears that the minimum vertical size is being set by the content of another tab. One way to get around that is to put the GridLayout
in the PAGE_START
of a BorderLayout
before putting the panel with border layout into the tabbed pane.
GridLayout
has an orange BG.BorderLayout
has a yellow BG.import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class TopAlignedComponents {
private JComponent ui = null;
TopAlignedComponents() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JTabbedPane tb = new JTabbedPane();
ui.add(tb);
Image spacer = new BufferedImage(300, 100, BufferedImage.TYPE_INT_RGB);
tb.addTab("Spacer", new JLabel(new ImageIcon(spacer)));
String[] labels = {"Shapes", "Joints", "AABBs"};
JPanel checkPanel = new JPanel(new GridLayout(0, 1, 4, 4));
checkPanel.setBackground(Color.ORANGE);
for (String label : labels) {
checkPanel.add(new JCheckBox(label));
}
JPanel checkConstrain = new JPanel(new BorderLayout());
checkConstrain.setBackground(Color.YELLOW);
checkConstrain.add(checkPanel, BorderLayout.PAGE_START);
tb.addTab("Check", checkConstrain);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
TopAlignedComponents o = new TopAlignedComponents();
JFrame f = new JFrame("Top Aligned Components");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
If i remember correctly, its because of your layout!
GridLayout divides your windowsize into equal parts, so i think you should either unset your windows size and use pack() or you could switch to a different layout.
( I assume your window's size is or minimum-size is set somewhere )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With