bruh bruh - 11 months ago 55
Java Question

GridLayout doesn't work

here's my code:

setBorder(BorderFactory.createTitledBorder("Candidates for President"));

for(int row=0; row<PresidentTable.tblNatPresident.getRowCount(); row++){
setLayout(new GridLayout(row, 2));
String name=PresidentTable.tblNatPresident.getValueAt(row, 0).toString();

JLabel lblName=new JLabel(name);
JRadioButton radioVote=new JRadioButton();

add(lblName); add(radioVote);

but the one that will appear looks like this:

enter image description here

and i'm trying to achieve like this:

name | radiobutton

name | radiobutton

and the number of rows will depend on how many rows the jtables was. and only two columns.

i don't really know i mean did i violate something in the codes? or is there something that i should put for it to properly work? please help thank you so much :)

Answer Source

For starters you need to move your setLayout before the loop as it is just overriding it each iteration for no reason, therefore you need to change the y value to the amount of rows. Secondly change swap the values within the grid layout so it looks something like this...

setLayout(new GridLayout(2, PresidentTable.tblNatPresident.getRowCount()));