Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-05-11
8
Medium Priority
?
194 Views
Last Modified: 2012-05-05
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,
0
Comment
Question by:k41d3n
  • 6
  • 2
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13977041
GridLayout checkbox = new GridLayout(0, 2);  // 2 columns, as much rows as needed
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13977049
Then adding the checkboxes just as you do know will give you the wanted layout
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13977073
>> 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:k41d3n
ID: 13977363
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
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 13977395
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13977427
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
 
LVL 2

Author Comment

by:k41d3n
ID: 13977435
Awesome.

Thank you zz, ytmnd.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13977496
:)) Thanks for accepting.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question