I'm working with GridBagLayout
and I can't figure why the JCheckBox
don't align. I've tried to set alignement as a JCheckBox
parameter(i.e checkBox.setAlignementX(LEFT_ALIGNEMENT)
). It did'nt worked.
I first tried using BorderLayout
, but it turned out even worst
What I'm trying to acheive is this
I want the threee main categories(Physique, Psychologique and Social) to be set on the complete left, and i want the sub-categories to be set just a bit offset. That way you'll know it's a sub-categorie(according to the lines i've drawn).
There uis three JPanel
, one for each category.
This is not the original code but it somewhat reproduce the problem i have.
public class Window extends JFrame {
private JButton button = new JButton("Generer");
private JCheckBox physique = new JCheckBox("Traits Physiques");
private JCheckBox psychologic = new JCheckBox("Traits Psychologiques");
private JCheckBox mass = new JCheckBox("Masse");
private JCheckBox quality = new JCheckBox("Qualité");
private JTextArea text = new JTextArea("");
private JScrollPane scroller = new JScrollPane(text);
private JPanel panel = new JPanel();
public Window() {
setLayout(new BorderLayout());
setTitle("Personnage");
setSize(500, 350);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
text.setEditable(false);
arrange();
getContentPane().add(panel, BorderLayout.WEST);
getContentPane().add(scroller, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);
setVisible(true);
}
/**
* Arrange all the layout
*/
private void arrange() {
JPanel phPanel = new JPanel();
JPanel psPanel = new JPanel();
GridBagConstraints gbcPanel, gbcPh, gbcPs;
gbcPanel = gbcPh = gbcPs = new GridBagConstraints();
panel.setLayout(new GridBagLayout());
gbcPanel.gridx = 0;
gbcPanel.gridy = 0;
gbcPanel.gridheight = 1;
gbcPanel.gridwidth = 1;
panel.add(phPanel, gbcPanel);
gbcPanel.gridy = 1;
panel.add(psPanel, gbcPanel);
//---------------------------
gbcPh.gridx = 0;
gbcPh.gridy = 0;
gbcPh.gridheight = 1;
gbcPh.gridwidth = 2;
phPanel.add(physique, gbcPh);
gbcPh.gridx = 1;
gbcPh.gridy = 1;
gbcPh.gridwidth = 1;
phPanel.add(mass, gbcPh);
//--------------------------
gbcPs.gridx = 0;
gbcPs.gridy = 0;
gbcPs.gridheight = 1;
gbcPs.gridwidth = 2;
psPanel.add(psychologic, gbcPs);
gbcPs.gridx = 1;
gbcPs.gridy = 1;
gbcPs.gridwidth = 1;
psPanel.add(quality, gbcPs);
//------------------------
}
}
I want the main category(i.e physique and psychologic) to be align totally left and the sub category(i.e mass and quality) to be a bit offset. I can't do that, i don't know how, i've read multiple tutorial etc...
Honestly, setAlignmentX
probably isn't going to do much for your, setHorizontalAlignment
would be a better choice, but having said that, there are other things you can do with the GridBagConstraints
To start with, you could use the anchor
and weightx
properties...
phyConstraint.anchor = GridBagConstraints.WEST;
phyConstraint.weightx = 1;
The anchor will determine the position within the given cell the component will want to align to, this defaults to CENTER
. The weightx
property describes the amount of space that will given to, based on the remaining space left over after the layout has been calculated, in this case, we're asking for all the remaining space.
Next, you can use the insets
properties to determine the amount of internal spacing or margin a cell will have, this will affect the position the component when it's laid out...
phyConstraint.insets = new Insets(0, 20, 0, 0);
Finally, for the optionConstraint
, we can also use the fill
property, which will determine how a component will size depending on the amount of available space within there cell, in this case, you'll want the component to expand to fill all the available space, for example...
optionConstraint.weightx = 1;
optionConstraint.fill = GridBagConstraints.HORIZONTAL;
Have a look at How to Use GridBagLayout for more details.
I'd also consider having a look at How to Use Trees, which might save you some trouble...