Reputation: 161
Can someone give me some tips on how to dynamically generate nested radio buttons ? I never worked with radio buttons in JAVA, so before I start maybe I'll find some useful tips around here. I read the information from a database and using that I want to create some radio buttons, with drop down as well.
Something like this:
When I press My Savings Account a new drop down panel will appear with the set of the products that account has. Also I want my response for these radio buttons choice to be stored.
Upvotes: 0
Views: 4200
Reputation: 3439
Here Simple Exercise of adding button into the Swing and yes this example uses RadioButton.
Here check it out this.
for (int i = 0; i < 10; i++) {
final JRadioButton button1 = new JRadioButton("Kishan"+i);
jPanel1.add(button1);
buttonGroup1.add(button1);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.err.println("Action Performed..************");
System.out.println("This is action text.."+button1.getText());
}
});
}
Upvotes: 0
Reputation: 1435
I do not recommend you to use nesting. It is good practice to use UI where components are aligned with each other as possible, and nesting would create vertical alignment for every level of nested components. Also in combination with radio buttons it requires large area of empty user interface!
Instead of nesting you could place each level of radio buttons under previous level of radio buttons. Selecting radio button in one level would enable and update radio buttons in level under. Something like this, but imagine more radio button groups:
Also you could replace radio buttons with combo boxes. That would save additional space and make UI even simpler.
If you really want radio button nesting, i would use GridBagLayout. Every radio button would be in one row and filling multiple columns. Every level of radio buttons would start on next column:
Upvotes: 1