anil
anil

Reputation: 1

adding components in applet

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?

Upvotes: 0

Views: 5034

Answers (5)

Andrew Thompson
Andrew Thompson

Reputation: 168835

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

Upvotes: 0

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74800

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();
     }
 }

Upvotes: 0

Suhail Gupta
Suhail Gupta

Reputation: 23276

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
} 

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692023

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.

Upvotes: 2

AlexR
AlexR

Reputation: 115378

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

Upvotes: 0

Related Questions