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.setViewportVi ew(getJPan eCheckBox( ));
}
return jScrollPane3;
}
private JPanel getJPaneCheckBox() {
JPanel jPanelCheckBox = new JPanel();
GridLayout checkbox = new GridLayout(1, 2);
jPanelCheckBox.setLayout(c heckbox);
try {
Set methods = GetTestMethods
.findMethods(DeviceIdentif ication.cl ass);
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(elemen t);
System.out.println(newChec kBox.getNa me());
jPanelCheckBox.add(newChec kBox);
}
} 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,
/**
* This method initializes jScrollPane3
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane3() {
if (jScrollPane3 == null) {
jScrollPane3 = new JScrollPane();
jScrollPane3.setViewportVi
}
return jScrollPane3;
}
private JPanel getJPaneCheckBox() {
JPanel jPanelCheckBox = new JPanel();
GridLayout checkbox = new GridLayout(1, 2);
jPanelCheckBox.setLayout(c
try {
Set methods = GetTestMethods
.findMethods(DeviceIdentif
System.out.println(methods
//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(elemen
System.out.println(newChec
jPanelCheckBox.add(newChec
}
} 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,
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(c heckbox);
...
insidePanel.add(jPanelChec kBox); // <<< added
return insidePanel; // <<< changed
Then you should have:
JPanel insidePanel = new JPanel(new FlowLayout(FlowLayout.LEFT
... // all the code you have right now:
JPanel jPanelCheckBox = new JPanel();
GridLayout checkbox = new GridLayout(0, 2);
jPanelCheckBox.setLayout(c
...
insidePanel.add(jPanelChec
return insidePanel; // <<< changed
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(c heckbox);
try {
Set methods = GetTestMethods
.findMethods(DeviceIdentif ication.cl ass);
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(elem ent);
checkBoxLabel.setText(elem ent);
newCheckBox.setName(elemen t);
System.out.println(newChec kBox.getNa me());
jPanelCheckBox.add(newChec kBox);
jPanelCheckBox.add(checkBo xLabel);
}
} catch (Exception e) {
atelog.error(e);
e.printStackTrace();
}
insidePanel.add(jPanelChec kBox);
return insidePanel;
}
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(c
try {
Set methods = GetTestMethods
.findMethods(DeviceIdentif
System.out.println(methods
//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(elem
checkBoxLabel.setText(elem
newCheckBox.setName(elemen
System.out.println(newChec
jPanelCheckBox.add(newChec
jPanelCheckBox.add(checkBo
}
} catch (Exception e) {
atelog.error(e);
e.printStackTrace();
}
insidePanel.add(jPanelChec
return insidePanel;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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") );
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") );
ASKER
Awesome.
Thank you zz, ytmnd.
Thank you zz, ytmnd.
:)) Thanks for accepting.