Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding a radio button to a sequential group using a loop  RSS feed

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I am attempting to add an array of radio buttons to a sequential group using a for next loop. I can get the group to work by hard coding each array instance but, when I try it using the loop, it doesn't work.

Here is the working method:

private void rBtnLayout(JRadioButton[] rBtns){

layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(rBtns[0])
.add(rBtns[1])
.add(rBtns[2])
.add(rBtns[3]))
);

layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(rBtns[0])
.add(rBtns[1])
.add(rBtns[2])
.add(rBtns[3]))
)
);

pack();
}

I have tried numerous variations of the following without any luck:

private void rBtnLayout(JRadioButton[] rBtns){

for (int i = 0; i < numBtns; i++){
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(rBtns[i]))
);

layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(rBtns[i]))
)
);
}
pack();
}

Has anyone else tried this sort of thing? Any luck? Thanks.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to be trying to hand-code the group layout. Why? There are so many more user-friendly layouts out there including GridLayout which is perfect for setting out JRadioButtons in a row. My recommendation is that you try this instead.

for instance:


[ March 06, 2008: Message edited by: pete stein ]
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I may try that approach in the future. Right now I have too much invested in the GroupLayout approach.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!