It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes GroupLayout radio button alignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GroupLayout radio button alignment" Watch "GroupLayout radio button alignment" New topic
Author

GroupLayout radio button alignment

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Hello,

I am trying to create a simple frame with rows of radio buttons using GroupLayout. I have it working except I cannot get the rows of buttons to align along the left hand side. I have tried using the GroupLayout.LEADING argument to no avail.

What I would like to see should look like this:

A B
1 2 3 4 5 6
RED BLUE GREEN

What I am actually getting looks like this:

A B
1 2 3 4 5 6
RED BLUE GREEN

Is anyone familiar with GroupLayout? It seems like each of these rows should belong to one parallel group. Here is my code:

public void setup() {

layout.setAutocreateGaps(true);
layout.setAutocreateContainerGaps(true);

for (int i = 0; i < numRows; i++) {
numBtns[i] = values[i].length;
rBtns = new JRadioButton[numRows][numBtns[i]];

for (int j = 0; j < numBtns[i]; j++) {


rBtns[i][j] = new JRadioButton(values[i][j]);
rBtns[i][j].setSelected(false);
rBtns[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.equals("Selected")){;
System.out.println("Selected");
}
}
});

switch (i){
case 0: SeqGroup0.add(rBtns[i][j]);
ParaGroup0.add(rBtns[i][j]);
case 1: SeqGroup1.add(rBtns[i][j]);
ParaGroup1.add(rBtns[i][j]);
case 2: SeqGroup2.add(rBtns[i][j]);
ParaGroup2.add(rBtns[i][j]);
}
}
}

getContentPane().setLayout(layout);

layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(SeqGroup0)
.add(SeqGroup1)
.add(SeqGroup2)
);

layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.BASELINE)
.add(layout.createSequentialGroup()
.add(ParaGroup0)
.add(ParaGroup1)
.add(ParaGroup2)
)
);

pack();
}

Thanks.
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Sorry. I haven't got the hang of the use of HTML tags in postings yet. I will use periods to represent spaces.

What I would like to see is this:

A B
1 2 3 4 5
RED BLUE GREEN

What I am actually seeing is this:

A B
.....1 2 3 4 5
...............RED BLUE GREEN

TIA
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Got it! I forgot to add break statements to my switch construct:

 
Consider Paul's rocket mass heater.
 
subject: GroupLayout radio button alignment
 
Similar Threads
Java grid colouring in game
Simple: Turning A Program Into An Applet
help me create Go! game please
Swings
JMenu Problem