0

I am making an applet and as part of my applet, I want this to happen: When the user presses "OK", the old components (some radio buttons) are removed, and a new JPanel is added, with a bunch of textfields.

However, I cannot figure out how to add a new component to the applet after it has started. I made the problem simpler by ignoring the removal part (Which I know how to do) and just adding a simple JLabel instead, but even that won't add!

Here is my code so far:

// imports omitted

public class Class extends Applet implements ActionListener 
{
  Button okButton;  
  CheckboxGroup radioGroup; 
  Checkbox radio1; 
  Checkbox radio2; 
  Checkbox radio3;
  JLabel j;

  public void init()  
  { 
    setLayout(new FlowLayout()); 
    okButton = new Button("OK"); 
    j = new JLabel("hello");
    radioGroup = new CheckboxGroup(); 
    radio1 = new Checkbox("Red", radioGroup,false); 
    radio2 = new Checkbox("Blue", radioGroup,true); 
    radio3 = new Checkbox("Green", radioGroup,false); 
    add(okButton); 
    add(radio1); 
    add(radio2); 
    add(radio3);
    okButton.addActionListener(this); 
  }

  public void repaint(Graphics g) 
  { 
    if (radio1.getState()) add(j); 
  }

  public void actionPerformed(ActionEvent evt)  
  { 
    if (evt.getSource() == okButton) repaint();
  } 
}

What am I doing wrong?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
anil
  • 1
  • 1
  • 1

5 Answers5

2

You shouldn't override the repaint method, and certainly not add a component in this method. Just remove the radio buttons from the applet (using its remove method) and add the label in the applet in your actionPerformed method, the same way you add them in the init method.

You might have to call validate after.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Add components and then call validate() of your container. In this case yourApplet.validate(). This will trigger repainting and rearranging of all elements.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

you could do something like

JFrame fr= new JFrame();                    // global variables
JPanel panelToBeAdded = new JPanel();
JPanel initialPanel = new JPanel();
JTextField fieldToBeAdded = new JTextField(); 
panelToBeAdded.setPreferredSize( new Dimension(400,400));
initialPanel.setPreferredSize( new Dimension(400,400));
initialPanel.setVisible(true);
fr.add(initialPanel);
fr.setVisible(true);
fr.pack();


public void actionPerformed(ActionEvent ae) {
    initialPanel.setVisible(false);
    //radiobuttons.setVisible(false);---> hide the radio buttons
    panelToBeAddedd.add(fieldToBeAddedd);
    panelToBeAddedd.setVisible(true);
    fr.add(panelToBeAddedd);

}

public void repaint( Graphics g ) {
  // do something
} 
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
0

What am I doing wrong?

Your repaint(Graphics) method is not the same method you are calling in your actionPerformed method.

Also, repaint is a pretty bad name for a method which is adding a new component.

 public void swapComponents() 
 { 
     if (radio1.getState()) {
        remove(radio1);
        remove(radio2);
        remove(radio3);
        add(j);
        validate();
     }
 }

 public void actionPerformed(ActionEvent evt)  
 { 
     if (evt.getSource() == okButton) {
        swapComponents();
     }
 }
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
0

When the user presses "OK", the old components (some radio buttons) are removed, and a new JPanel is added, with a bunch of textfields.

Use a CardLayout, as shown here. It is perfect for situations like this.

Game view High Scores view

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433