Link to home
Start Free TrialLog in
Avatar of k41d3n
k41d3n

asked on

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,
Avatar of zzynx
zzynx
Flag of Belgium image

GridLayout checkbox = new GridLayout(0, 2);  // 2 columns, as much rows as needed
Then adding the checkboxes just as you do know will give you the wanted layout
>> 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


Avatar of k41d3n
k41d3n

ASKER

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;  
    }

ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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") );
Avatar of k41d3n

ASKER

Awesome.

Thank you zz, ytmnd.
:)) Thanks for accepting.