Solved

Java AWT.Panel action event help

Posted on 2006-11-23
13
330 Views
Last Modified: 2008-03-10
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"));
   }
}
   
0
Comment
Question by:azcalv408
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:gkishoreji
ID: 18005804
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
 
LVL 5

Expert Comment

by:gkishoreji
ID: 18005808
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
 

Author Comment

by:azcalv408
ID: 18005863
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:gkishoreji
ID: 18005901
ohh. i didnt notice...

u have a problem here

event.getSource() == myButton

u must compare objects using equals method

event.getSource().equals(myButton)
0
 

Author Comment

by:azcalv408
ID: 18005914
that's not it either...
0
 
LVL 5

Expert Comment

by:gkishoreji
ID: 18005917
it should work fine.. check whether u have added the panel to the Frame or the outer container..
0
 

Author Comment

by:azcalv408
ID: 18005921
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
 

Author Comment

by:azcalv408
ID: 18005924
public class,  sorry
0
 
LVL 5

Accepted Solution

by:
gkishoreji earned 300 total points
ID: 18005941
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
 

Author Comment

by:azcalv408
ID: 18005959
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
 
LVL 5

Expert Comment

by:gkishoreji
ID: 18006010
>> then I want to change the existing panel on click of button

i dont understand this
0
 

Author Comment

by:azcalv408
ID: 18006113
nevermind that, I got it now. thanks a bunch!
0
 

Author Comment

by:azcalv408
ID: 18006125
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 31
servlet example 11 76
maven disable workspace resolution 1 78
restrict decimal places for double datatype 10 49
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question