I want to have this part in my application which allows user to add more options (in form of JradioButton). So by default I give some options to user in JradioButton and if they add more options (on the other part of the application); my Jframe should automatically add the options via Setup_Equipment_Frame method (shown Below) in which it gets array of strings that are basically the options that user added. I face some difficulties with it.
the code:
public void Setup_Equipment_frame(String a[]) //String_Array of Newly added options
{
//creating default options
JRadioButton option1 = new JRadioButton("Visual Stadio");
JRadioButton option2 = new JRadioButton("Netbeans");
JRadioButton option3 = new JRadioButton("Eclipse");
//Creating the button group
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
group.add(option3);
//setting the frame layout
setLayout(new FlowLayout());
for(int i=0; i<=a.length-1;i++) //loop for how many new options are added
{
if(a[i] != null) //if the array's current item is not null
{
JRadioButton NewButton1= new JRadioButton(""+a[i]); //Add the Button
add(NewButton1);
}
}
//adding the default options
add(option1);
add(option2);
add(option3);
pack();
}
Now it actually works. I add the radio button however since the name of the added buttons are all "NewButton1", I can't have any control over them and I have access to only last created JRadioButton and the default ones. I don't know how many new options user might add.
My problem is how to automatically create JRadioButton with different names.
I apologies in advance if my question or code is confusing. I am not that experienced.
thanks
Update
thanks for the answers, with your help I solved the problem simply by adding an array of JradioButtons
For those who might face the same problem the code that works for me is as follow:
Solved:
public void Setup_Equipment_frame(String a[])
{
int number_of_options=1;//number of new options
for(int i=0; i<=a.length-1;i++)
{
if(a[i] != null){
number_of_options++;
}
}
JRadioButton []v=new JRadioButton[number_of_options];
setLayout(new FlowLayout());
for(int z=0; z<=number_of_options-1;z++)
{if(a[z] != null){
{
v[z]=new JRadioButton(a[z]);
add(v[z]);
}
}
}
}
Thanks Alot