0

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 thisenter image description here

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...

The original code

Chax
  • 1,041
  • 2
  • 14
  • 36
  • I'm not sure that your posted code and text is adequate to allow us to understand your problem enough to answer it. If you don't get a decent answer soon, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Oct 26 '14 at 21:48
  • 1
    Thanks for the comment, i'll try to explain better what i'm trying to acheive. – Chax Oct 26 '14 at 21:56
  • @HovercraftFullOfEels There you go. Is it better explained? – Chax Oct 26 '14 at 22:02
  • 2
    Chax, please have a look at the link I've provided in my first comment. Please comment back to me and let me know if you post your [MCVE](http://stackoverflow.com/help/mcve), and I'll take a look at it. Remember that this program needs to be short, very short, compilable, runnable, not require outside resources (unless they're easily obtainable from the internet) and shows for us your problem. Good luck. – Hovercraft Full Of Eels Oct 26 '14 at 22:03
  • @HovercraftFullOfEels There you go. A simplified code that have my problems in it. Plus it is compilable. – Chax Oct 26 '14 at 23:46

1 Answers1

4

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;

Layout

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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • What is the big difference between `GridBagLayout` and `Trees`? – Chax Oct 26 '14 at 23:44
  • @Chax A `JTree` is designed to do what you are trying to do, it's much easier to update and modify (add new elements) and allows you to even collapse and expand branches – MadProgrammer Oct 26 '14 at 23:45
  • Ok thanks. I'll have a look at that. Plus your answer is really good. It is well explained. – Chax Oct 26 '14 at 23:49
  • @Chax: as per usual. All his answers are like that. – Hovercraft Full Of Eels Oct 27 '14 at 01:17
  • 1
    @HovercraftFullOfEels Over the top, long winded, rants of little to no relevance, until you get to the example code at the end ;) – MadProgrammer Oct 27 '14 at 01:20
  • 1
    @MadProgrammer: Yeah, that's the truth, but I didn't have the heart to say that to your face. Sorry. – Hovercraft Full Of Eels Oct 27 '14 at 01:21
  • 1
    @HovercraftFullOfEels Yeah, I'm like that grandparent who you had to sit and listen to for hours on end, only because you knew you'd get a few bucks or sweets from them at the end ;) – MadProgrammer Oct 27 '14 at 01:23
  • @MadProgrammer Have you ever tought of being a teacher or something like that? – Chax Oct 27 '14 at 01:51
  • @MadProgrammer any idea on how to add checkbox to a `JTree`? All the solution i found are either inappropriate for my problem or over-hardcore for my skills... – Chax Oct 29 '14 at 02:25
  • Something like [this](http://stackoverflow.com/questions/21924846/checkbox-within-a-jxtreetable/21924930#21924930) for example, about as ease it will get...don't be scared to grow your mad skills – MadProgrammer Oct 29 '14 at 02:27
  • @MadProgrammer I'm not scared, I still a bit lost in the `JTree` ;) – Chax Oct 29 '14 at 02:29
  • `JTree` is among the most complex of components, so I'm not surprised, but if you can deal with it (and `JTable`), you'll be able to deal with just about anything – MadProgrammer Oct 29 '14 at 02:31
  • @Mad It's a good news...i guess :P – Chax Oct 29 '14 at 02:33