0

I'm trying to change content dynamically based on a JRadioButton selection... My simplified code looks something like this.

//import
public class Thing {
  //
  JPanel pnlMain, pnl1, pnl2, pnlRt, pnlLt;
  JRadioBtn btn1, btn2;
  //
  Thing () {
    //
    //initialize panels, add to them, etc.
    pnlMain.add(pnlLt);
    pnlMain.add(pnl1);
    pnlMain.add(pnlRt);
    //
    //Get it showing and stuff.
    //
    }
  //
  //One instance of this class connected to all radio buttons.
  class Evt implements ActionListener {
    public void actionImplemented (ActionEvent evt) {
      //
      pnlMain.remove(1);
      //
      if (evt.getActionCommand().equals("Radio 1"))
        pnlMain.add(pnl1);
      else pnlMain.add(pnl2);
      //
      pnlMain.validate();
      //
      }
    }
  //
  public static void main (String[] args) {
    new Thing();
    }
  //
  }

This lets me change panels, but i cannot change back to a panel i had previously selected... I don't understand why. Please help!!!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
creXALBO
  • 307
  • 1
  • 3
  • 13

2 Answers2

3

You should be using CardLayout instead, as this is exactly what that is for. Check out the tutorial here.

Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45
2

Use a proper layout manager. In this scenario, I recommend using CardLayout. This enables the developer to delegate the "complexity" of panel exchanging to the layout manager, which is how it should be.

mre
  • 43,520
  • 33
  • 120
  • 170