Search code examples
javauser-interfacejlabeljtextfieldjradiobutton

GUI changes with RadioButton Selection


Per the title, I'm trying to get my GUI change with RadioButton selection. More specifically, the JLabels and JTextfields. Essentially, I want to look something like this: http://i40.tinypic.com/262sec7.png, where the first three labels and fields are always the same, and the last two fields and labels are different.

I know it needs to occur via the ActionListeners for the RadioButtons. Should everything from the Labels down be created in the ActionListeners? Then cleared and created again based on the RadioButton selection?


Solution

  • You're looking for CardLayout.

    CardLayout is a LayoutManager which allows you add multiple components and choose which one is shown using CardLayout.show(Container parent, String name). Only one component at a time will be visible.

    There's a tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

    In your case, you would add a different JPanel with all the controls you want for each radio button. Then, the radio button ActionListener would simply call CardLayout.show( ... ) to show the appropriate set of controls.