Adding Components to Container


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Adding Components to Container

  1. #1
    Rocco Balsamo Guest

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



  2. #2
    Paul Clapham Guest

    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.




  3. #3
    Rocco Balsamo Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center