Widgets added to Frame are invisible

Help
2007-08-16
2013-05-23
  • Jesse Aldridge
    Jesse Aldridge
    2007-08-16

    I don't understand why the following code is working.

    What's supposed to happen is when I click the button a new label is added to the window.  The problem is that the labels remain invisible until I manually resize the window.

    I've tried adding calls to repaint to both the container and the window, but neither of them seem to work.

    ---------------------

    import buoy.event.CommandEvent;
    import buoy.event.WindowClosingEvent;
    import buoy.widget.BButton;
    import buoy.widget.BFrame;
    import buoy.widget.BLabel;
    import buoy.widget.ColumnContainer;

    public class BuoyTest {
       
        public static void main( String[] args ) {
            container = new ColumnContainer();
                           
            BButton button = new BButton( "Go" );   
            button.addEventLink( CommandEvent.class, new Object() {
                void processEvent() {
                    container.add( new BLabel( "Testing" ));
                    window.pack();
                }
            });
           
            container.add( button );   
           
            setup_window( container );
        }
       
        static void setup_window( ColumnContainer container ) {       
            window = new BFrame();   
           
            window.setContent( container );
           
            window.pack();
            window.setVisible(true); 
               
            window.addEventLink( WindowClosingEvent.class, new Object() {
                void processEvent()
                {
                  System.exit(0);
                }
              });
        }

        static ColumnContainer container;
        static BFrame window;
    }

     
    • Jesse Aldridge
      Jesse Aldridge
      2007-08-16

      ...er, that was supposed to say, "I don't understand why the following code is NOT working."

      Also, I found that I could get it to work by adding the line "window.setVisible(true);" after  the first "window.pack();" line.  Not sure why thought.  It seems like something needs to get repainted, but calling repaint doesn't seem to work for me.

       
    • Peter Eastman
      Peter Eastman
      2007-08-16

      Your program works fine for me (once I added the missing import statement for CommandEvent).  I click the button, the window gets larger, and a new label appears in it.  What platform and Java version are you using?  I'm using Java 1.5 on Mac OS X.

      Peter

       
    • Jesse Aldridge
      Jesse Aldridge
      2007-08-16

      I was using Java 1.5.0_11 on Windows XP.
      I just tried switching to Java 1.6 and when I do that, it works.
      It still doesn't work when I switch back to 1.5.

      Guess it's a 1.5 + Win XP bug?

       
    • Jesse Aldridge
      Jesse Aldridge
      2007-08-16

      Well I just ran into a similar problem.  This is with Java 1.6.  Still using Win XP. 
      The first test is working fine, but this one is having the same problem.  The Labels and the ScrollBar are invisible until I resize the window.

      It's not really a big deal though, as adding "window.setVisible(true)" after the line "container.add( new BLabel( "Testing" ) )" seems to serve as an easy work-around.

      -------------

      import java.awt.Dimension;

      import buoy.event.CommandEvent;
      import buoy.event.WindowClosingEvent;
      import buoy.widget.BButton;
      import buoy.widget.BFrame;
      import buoy.widget.BLabel;
      import buoy.widget.BScrollPane;
      import buoy.widget.ColumnContainer;
      import buoy.widget.WidgetContainer;

      public class BuoyTest2 {
         
          public static void main( String[] args ) {
              container = new ColumnContainer();
                             
              BButton button = new BButton( "Go" );   
              button.addEventLink( CommandEvent.class, new Object() {
                  void processEvent() {
                      container.add( new BLabel( "Testing" ) );
                  }
              });
             
              container.add( button );   
             
              setup_window( container );
          }
         
          static void setup_window( WidgetContainer container ) {       
              window = new BFrame();   
             
              //Make it scrollable
              BScrollPane scroll_pane = new BScrollPane();       
              scroll_pane.setContent( container );
              scroll_pane.setPreferredViewSize( new Dimension( 100,100 ) );
             
              window.setContent( scroll_pane );
             
              window.pack();
              window.setVisible(true); 
                 
              window.addEventLink( WindowClosingEvent.class, new Object() {
                  void processEvent()
                  {
                    System.exit(0);
                  }
                });
          }

          static ColumnContainer container;
          static BFrame window;
      }

       
    • Peter Eastman
      Peter Eastman
      2007-08-17

      I can certainly tell you why the second version doesn't work: you took out the call to pack() after adding the BLabel.  When you add a widget to a container, that doesn't automatically cause the container to redo its layout, so the new widget will probably have size (0,0) or something like that.  You need to call layoutChildren() or pack() so that it will know to redo its layout

      Peter

       
      • Jesse Aldridge
        Jesse Aldridge
        2007-08-17

        Oh, ok.  That fixed it.  Thanks.