Calculating number of items in a list and building checkboxes off of that

Ok, as observed in my last question (Thanks to everyone for that), I have this code:

/**
     * This method initializes jScrollPane3
     *
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane3() {
        if (jScrollPane3 == null) {
            jScrollPane3 = new JScrollPane();
            jScrollPane3.setViewportView(getJPaneCheckBox());
        }
        return jScrollPane3;
    }

   private JPanel getJPaneCheckBox() {
        JPanel jPanelCheckBox = new JPanel();  
        GridLayout checkbox = new GridLayout(1, 2);
        jPanelCheckBox.setLayout(checkbox);
        try {
            Set methods = GetTestMethods
            .findMethods(DeviceIdentification.class);
            System.out.println(methods.size());
            //these variables are defined private elsewhere in the class
            //For a set or list
            for (Iterator it = methods.iterator(); it.hasNext();) {
                Object listElement = it.next();
                String element = listElement.toString();
                System.out.println(element);
                JCheckBox newCheckBox = new JCheckBox();
                newCheckBox.setName(element);
                System.out.println(newCheckBox.getName());
                jPanelCheckBox.add(newCheckBox);
            }
        } catch (Exception e) {
            atelog.error(e);
            e.printStackTrace();
        }
        return(jPanelCheckBox);
    }


This prints the check boxes out like this:


X                                                                      X


in the middle of my pane.

The issue now is this:

I like how they are side by side, and not just one list down.

The issue now, is that I only have 2 checkboxes being built right now, but in the future I could have dozens. I'd like it to start at the top of the pane and not the middle and build the checkboxes with labels in this respect:

X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel

etc;

If there is an odd number it would look like this:

X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                               X theLabel
X theLabel                              


How would I program this logically? My knowledge of building layouts is so limited, any guidance you have would help me 10 fold.

Thank you,
LVL 2
k41d3nAsked:
Who is Participating?
 
zzynxSoftware engineerCommented:
You don't need a JCheckBox AND a JLabel.
Just use JCheckBoxes (which have labels of themselves)

GridLayout checkbox = new GridLayout(0, 2);

and

                JCheckBox newCheckBox = new JCheckBox(element);
                newCheckBox.setName(element);
                System.out.println(newCheckBox.getName());
                jPanelCheckBox.add(newCheckBox);
0
 
zzynxSoftware engineerCommented:
GridLayout checkbox = new GridLayout(0, 2);  // 2 columns, as much rows as needed
0
 
zzynxSoftware engineerCommented:
Then adding the checkboxes just as you do know will give you the wanted layout
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
zzynxSoftware engineerCommented:
>> I'd like it to start at the top of the pane and not the middle

Then you should have:

JPanel insidePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));  // <<<< added
... // all the code you have right now:
JPanel jPanelCheckBox = new JPanel();  
GridLayout checkbox = new GridLayout(0, 2);
jPanelCheckBox.setLayout(checkbox);
...

insidePanel.add(jPanelCheckBox);      // <<< added
return insidePanel;                              // <<< changed


0
 
k41d3nAuthor Commented:
Alright, I'm getting there, Your help is immeasurable.

The issue I have now is this:

when the gui loads it looks like this:

X                          testLabel X                                 testLabel

essentially, a checkbox a huge space then the label, then an immediate checkbox a huge space and the label

I'd like it to be more concise.

X theLabel          X theLabel

Here is my code, adjusted for your changes:

   private JPanel getJPaneCheckBox() {
        JPanel insidePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));  
        JPanel jPanelCheckBox = new JPanel();  
        GridLayout checkbox = new GridLayout(0, 4);
        jPanelCheckBox.setLayout(checkbox);
        try {
            Set methods = GetTestMethods
            .findMethods(DeviceIdentification.class);
            System.out.println(methods.size());
            //these variables are defined private elsewhere in the class
            //For a set or list
            for (Iterator it = methods.iterator(); it.hasNext();) {
                Object listElement = it.next();
                String element = listElement.toString();
                System.out.println(element);
                JCheckBox newCheckBox = new JCheckBox();
                JLabel checkBoxLabel = new JLabel();
                checkBoxLabel.setName(element);
                checkBoxLabel.setText(element);
                newCheckBox.setName(element);
                System.out.println(newCheckBox.getName());
                jPanelCheckBox.add(newCheckBox);
                jPanelCheckBox.add(checkBoxLabel);
            }
        } catch (Exception e) {
            atelog.error(e);
            e.printStackTrace();
        }
        insidePanel.add(jPanelCheckBox);
        return insidePanel;  
    }

0
 
zzynxSoftware engineerCommented:
If in some GUI you see

   x  Magazine
   x  Internet
   x  Other


Then that's simply the result of

   panel.add( new JCheckBox("Magazine") );
   panel.add( new JCheckBox("Internet") );
   panel.add( new JCheckBox("Other") );
0
 
k41d3nAuthor Commented:
Awesome.

Thank you zz, ytmnd.
0
 
zzynxSoftware engineerCommented:
:)) Thanks for accepting.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.