0

I'm not sure how I can go about replacing a JButton with a JRadioButton when the JButton is clicked. So say if I had Jpanel set as a gridlayout which lays out 4 rows (3 JLabels and a JButton). So when the user clicks the JButton, that JButton will be replaced with a JRadioButton.

Would I use a CardLayout for this purpose, or is there a simple way in replacing that JButton?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Douglas Grealis
  • 599
  • 2
  • 11
  • 25
  • not there isn't some issue with that, but have to add JComponents with numbering by holding its ZOrder, then to call revalidate and repaint to its container – mKorbel Apr 10 '13 at 11:40
  • *"Would I use a `CardLayout` for this purpose,"* Sure, it can go inside the `GridBagLayout`. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 10 '13 at 11:41

1 Answers1

-1

What you could do is remove the button from the pannle and add a radio button in its place

Example code

  static JButton button1 = new JButton("Test");
  static JPanel panel1 = new JPanel():
  public static void main(String[] args{
  panel1.add(button1);
  }
  public void ActionListener(ActionEvent e){
  if(e.equals("Test"){
  panel1.remove(button1);
  panel1.add(radiobutton1);
  }

from here you can add your radieo button

fftk4323
  • 130
  • 1
  • 12
  • Thanks alot for the reply. So in my case I could add the radioButton at position 3 (4th row JButton) to the JPanel. I was thinking too much into it. Thanks again – Douglas Grealis Apr 10 '13 at 11:55
  • This method is unnecessary and buggy. Especially if the radio button is larger than the button. Combined with poor formatting and forgetting to inform the GUI to update the layout, -1. Tip to the OP - *Use a `CardLayout`.* – Andrew Thompson Apr 10 '13 at 23:21
  • this code is only for getting an idea of how you can swape out buttons. i had no idea about cardlayouts when i wrote this. if you want to say my idea is bad and propse a tip, give some code so the guy asking the question and people reading this have an idea on how to do what you say – fftk4323 Apr 11 '13 at 01:50