Java AWT.Panel action event help

Hi,
    I have a Panel which I declared as an instance variable and can be accessed by the event handler. How do I change the components within the Panel on a click of a button? I already have my class and the actionPerformed(ActionEvent event) method written. The problem is when I want to add a Label, for instance, within the Panel on a click of the button, it doesn't work. What is an approach to accomplish this? thanks!

private Panel myPanel = new Panel(new FlowLayout());
private myButton = new Button();
...
myButton.addActionListener(this);
...
public void actionPerformed(ActionEvent event) {
   if(event.getSource() == myButton){
      myPanel.add(new Label("A new label has been created"));
   }
}
   
azcalv408Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
gkishorejiConnect With a Mentor Commented:
I tried this for ur question... its working fine for me...
check if u r missing something

import java.awt.*;
import java.awt.event.*;

public class Test extends Frame implements ActionListener
{
      Panel p = new Panel();
      Button b = new Button("Add");
      public Test()
      {
            b.addActionListener(this);
            add(b);
            add(p);
            setLayout(new FlowLayout());
            setVisible(true);
      }
      
      public static void main(String[] args)
      {
            new Test();
      }
      
      public void actionPerformed(ActionEvent e)
      {
            if(e.getSource().equals(b))
            {
                  p.add(new Label("New Label"));
                  p.setBackground(Color.blue);
                  validate();
            }
      }
}
0
 
gkishorejiCommented:
the label will not be visible until u validate the contents...
validate the panel after adding the component.

public void actionPerformed(ActionEvent event) {
   if(event.getSource() == myButton){
      myPanel.add(new Label("A new label has been created"));
      myPanel.validate();
   }
}
0
 
gkishorejiCommented:
sorry.. u need to validate the entire frame..

public void actionPerformed(ActionEvent event) {
   if(event.getSource() == myButton){
      myPanel.add(new Label("A new label has been created"));
      validate();
   }
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
azcalv408Author Commented:
hi gkishoreji ,
  thanks for helping me, but it still doesn't work :(
  i even tried
myPanel.setBackground(Color.blue);      to change its color (nothing changed)
validate();
0
 
gkishorejiCommented:
ohh. i didnt notice...

u have a problem here

event.getSource() == myButton

u must compare objects using equals method

event.getSource().equals(myButton)
0
 
azcalv408Author Commented:
that's not it either...
0
 
gkishorejiCommented:
it should work fine.. check whether u have added the panel to the Frame or the outer container..
0
 
azcalv408Author Commented:
this is in the main class, myButton is also defined up top
public myClass extends Frame{  
      myButton.addActionListener(this);
      add(myPanel);
      setVisible(true);
}
does that look right?
0
 
azcalv408Author Commented:
public class,  sorry
0
 
azcalv408Author Commented:
I think I know the problem now (sorry for not clarifying earlier):
I tried to change it after setting background in the main class like
myPanel.setBackground(Color.red);
myPanel.add(new Label("New Label");

then I want to change the existing panel on click of button....is there a way to do that?
0
 
gkishorejiCommented:
>> then I want to change the existing panel on click of button

i dont understand this
0
 
azcalv408Author Commented:
nevermind that, I got it now. thanks a bunch!
0
 
azcalv408Author Commented:
sorry...just wondering if you know how to clear the entire panel out? like remove all labels and start over with the same panel. thanks!
0
All Courses

From novice to tech pro — start learning today.