0

I've been trying to dynamically add/remove radio buttons to/from a JPanel and RadioGroup but it doesn't seem to work after initialisation. The JPanel layout is set to Flow Layout. I have the following method:

private void addButton(){
   JRadioButton jRadioButton = new JRadioButton("1");
   jPanel1.add(jRadioButton);
}

If I call this after initComponents in my JFrame Form constructor, it successfully adds a button to the JPanel. If I use a JButton on the form which calls the same method on click, nothing seems to happen. Why isn't it adding more buttons with each click? I've also been trying to remove radio buttons after initialisation and that doesn't seem to work either. I must be missing something very simple?

Steve W
  • 1,108
  • 3
  • 13
  • 35
  • When a question is marked as a duplicate I wish people would at least link to an answer that provides a Swing solution. The "accepted" answer indicates you should use validate(). That is an AWT method. Yes, it will work most of the time. But this is a Swing question and in Swing `revalidate()` was added and should be used. – camickr Feb 17 '21 at 15:08
  • 1
    Thanks for the note. I found that validate worked but I've changed to revalidate now based on your comment. BTW, I did try to find a solution before posting but had no luck. Searching on StackOverflow is never that easy. I always google then select the StackOverflow results. – Steve W Feb 17 '21 at 15:16

0 Answers0