-
Adding Components to Container
I am trying to add new components (in this case new buttons) to a
container (in this case a panel) after the container is already being
displayed on the screen. Unfortunately, these new buttons do not appear
until the window is resized. I'm not sure, but I don't think that this
is a repaint problem. Does anyone know how to add new components to the
display? Any input would be appreciated.
------------------------------------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class test extends Applet
{
Panel p = new Panel();
public void init() {
p.setLayout(new FlowLayout());
Button b = new Button("ADD");
p.add(b);
add(p);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
p.add(new Button ("I'm Here"));
}
});
}
}
-
Re: Adding Components to Container
Rocco Balsamo <rbalsamo@hillier.com> wrote in message
news:3921A175.3825BF7A@hillier.com...
> I am trying to add new components (in this case new buttons) to a
> container (in this case a panel) after the container is already being
> displayed on the screen. Unfortunately, these new buttons do not appear
> until the window is resized. I'm not sure, but I don't think that this
> is a repaint problem. Does anyone know how to add new components to the
> display? Any input would be appreciated.
>
Look into the validate() and invalidate() methods. I've never been able to
understand the documentation for these, but if you validate, or invalidate,
the component, or container, after you add the component it might do what
you want. Try it and see. And let us know what if anything works.
-
Re: Adding Components to Container
Yes! It works! In order to add a new component to a container after an event,
you must call the validate() method and then the doLayout() method on the
container. Thanks a lot, Paul.
Paul Clapham wrote:
> Rocco Balsamo <rbalsamo@hillier.com> wrote in message
> news:3921A175.3825BF7A@hillier.com...
> > I am trying to add new components (in this case new buttons) to a
> > container (in this case a panel) after the container is already being
> > displayed on the screen. Unfortunately, these new buttons do not appear
> > until the window is resized. I'm not sure, but I don't think that this
> > is a repaint problem. Does anyone know how to add new components to the
> > display? Any input would be appreciated.
> >
> Look into the validate() and invalidate() methods. I've never been able to
> understand the documentation for these, but if you validate, or invalidate,
> the component, or container, after you add the component it might do what
> you want. Try it and see. And let us know what if anything works.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks