Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you add radio buttons to ButtonGroup dynamically?  RSS feed

 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a HashMap to store names (keys) and IDs (values). I have set it up so that the radio buttons are added to the JPanel dynamically. That is, each time a name/id is added, a new radio button is automatically generated and added to the panel. Here is a snippet of the code I have thus far:

private void employeeButtons() {

buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(5,2,5,5));

empMap.put("Kyle", "1090");
empMap.put("Alex", "6490");
empMap.put("Mark", "9090");
empMap.put("Anne", "4390");

for (String n : empKeys())

buttonPanel.add(new JRadioButton(n));

ButtonGroup btnGroup = new ButtonGroup();

}

private Set<String> empKeys() {

Set<String> empName = empMap.keySet();

return empName;

}

The above code adds all of the radio buttons to the panel just fine. However, I need the buttons to be mutually exclusive. I would like to be able to not only add the buttons dynamically, but I need the buttons to be added to a ButtonGroup dynamically as well. That is, when I add a new hashmap entry the a new radio button is generated and added to the radiobutton group. I am not sure how to proceed or if what I am envisioning is even possible. Any advice, pointers, or guidance you all could give me will be greatly appreciated.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't want to create a new ButtonGroup for every button.

The ButtonGroup should already exist before you start iterating through the Hashmap. Then you just add the button to the ButtonGroup the same way you add the button to the panel.

Also, don't forget to use the "code" tags when you post code so the code is more readable.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Stein wrote:The above code adds all of the radio buttons to the panel just fine. However, I need the buttons to be mutually exclusive. I would like to be able to not only add the buttons dynamically, but I need the buttons to be added to a ButtonGroup dynamically as well. That is, when I add a new hashmap entry the a new radio button is generated and added to the radiobutton group.

Looks like a use case for JList to me.
 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the responses! I really do appreciate you guys taking the time to look into this. What a warm welcome!

Rob Camick, I followed your guidance and made my button group first. Then I used the for loop as before but this time I added the values to both the panel and the group. The code is as follows:

Not sure if this is the cleanest way to do things, but it worked! Thanks again guys!
 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I have the buttons added to the panel and button group, I need to add action listeners to each dynamically created button. With my current setup, I am having trouble trying to figure out how to add the action listeners to the buttons. My ultimate goal with this program is to have a user select an employee name (radio buttons) and then return the employee's ID. However, I am failing to see how to reference the buttons I created.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is the cleanest way to do things


I generally don't like trying to place all code in a single statement. It makes it difficult to debug and change. I prefer multiple statements. Yes it may be a little more typing, but ultimately I think the code is easier to understand at first glance and easier to maintain.

However, I am failing to see how to reference the buttons I created.


You add the ActionListener to the button when you create the button. Something like:



Of course you will need to modify the actionPerformed() method to access your Hashmap to retrieve the employee id based on the text of the radio button selected.
 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

You have helped me tremendously! I really do appreciate all of your guidance and suggestions. Thanks to your help, I have finished writing my program. I am very happy with its functionality. For the action performed section I went with the following code, piggy backing off of your example:


Again, thank you very much for your time, Rob.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!