This discussion is archived
8 Replies Latest reply: Dec 14, 2010 10:05 AM by morgalr RSS

The components placed in a JPanel do not load, until I resize the window.

843853 Newbie
Currently Being Moderated
I have a JPanel in which I have placed a hierarchy of Box objects. But they do not load when that panel is displayed. I have to click on the sides of the window as if I mean to resize it, and then the components show. How to show the components the first time that the JPanel loads?
Note: repaint() and revalidate() both do not work.

The JPanel itself replaces another JPanel that was previously in that JFrame. I delete that previous JPanel using setVisible(false) and removeAll() methods on that JPanel. Does that have anything to do with this problem?

class Coaster_Ride_Panel extends JPanel
{
     public Coaster_Ride_Panel(int w,int h)
    {
       //...
        
        setLocation(0,0);
        setPreferredSize(new Dimension(screenWidth,screenHeight));
        setSize(screenWidth,screenHeight);
        setLayout(new BorderLayout());
        
      
        
        results=new JTextArea(8,40);
        //...
        scrolls=new JScrollPane(results);
     
     
        generate_button=new JButton("Generate");
        generate_button.addActionListener(new QueryButtonListener());
        
        Box hbox=Box.createHorizontalBox();
        
        hbox.add(//...);
        hbox.add(//...);
      
            //
          
        Box b=Box.createVerticalBox();
        
        b.add(hbox);
        b.add(Box.createVerticalStrut(25));        
        b.add(//...);
        b.add(Box.createVerticalStrut(25));
        b.add(scrolls);
 
        
        
        add(b,BorderLayout.CENTER);
        
        repaint();
        revalidate();
        
        

           
    }
  • 1. Re: The components placed in a JPanel do not load, until I resize the window.
    796262 Newbie
    Currently Being Moderated
    If you want help, you'll have to provide an SSCCE that demonstrates the problem. But it sounds like you should be using a CardLayout.
  • 2. Re: The components placed in a JPanel do not load, until I resize the window.
    843853 Newbie
    Currently Being Moderated
    I have trimmed down my code to the essential problem. It is a text file of 10 kB now. Am I supposed to put the whole thing on the page now? Is that how you publish an SSCCE?
  • 3. Re: The components placed in a JPanel do not load, until I resize the window.
    796262 Newbie
    Currently Being Moderated
    icosahedronman wrote:
    I have trimmed down my code to the essential problem. It is a text file of 10 kB now. Am I supposed to put the whole thing on the page now? Is that how you publish an SSCCE?
    You could try, but I would bet that what you have isn't actually an SSCCE (the first 'S' stands for "short"). All you really need are the bare essentials: a JPanel, some components, and the act of removing those Components and replacing them with others. That's the gist of your problem, right?

    And have you looked into CardLayout at all? I bet you 10 Duke Stars (which are no longer being produced, so they are a hot commodity now) that that's the way you should be going.
  • 4. Re: The components placed in a JPanel do not load, until I resize the window.
    843853 Newbie
    Currently Being Moderated
    I am running into a different problem altogether when I attempt to use CardLayout, a problem much worse than the one I have right now. Okay, please look at the code below and tell me if there is a way to rectify the problem in my code. It is 3.4kB text file.
    If there is no way to improve on this code, I will work harder on the CardLayout thing. And the whole structure of my project will have to be changed.

    Okay. Problem: On first display, there is the white initial panel, which will never be shown again (used for user authentication) Then, on clicking the menu item, the coaster panel opens. But the button does not display until you click the side of the frame, as if you were going to resize that frame.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    
    public class Run_Project 
    {
         public static void main(String args[])
         {
              Toolkit kit=Toolkit.getDefaultToolkit();
              Dimension screenSize=kit.getScreenSize();
              int screenWidth=screenSize.width;
              int screenHeight=screenSize.height;
    
              WelcomeFrame frame=new WelcomeFrame(screenWidth,screenHeight);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
         }
    }
    
    class WelcomeFrame extends JFrame 
    {
         public WelcomeFrame(int w,int h)
         {
              screenWidth=w;
              screenHeight=h;
    
              setLocation(0,0);
              setSize(screenWidth,screenHeight);
    
              addMenuItems();
              addMenuItemListeners();
    
              tp_panel=new Initial_Panel(screenWidth,screenHeight);
    
              Box b=Box.createHorizontalBox();
              b.add(tp_panel);
    
              add(b);
    
              Poller p=new Poller(this);
              Thread t=new Thread(p);
              t.start();
         }     
    
         public void addMenuItems()
         {
              menubar=new JMenuBar();
              
              ride_menu=new JMenu("Rides");
              ride_menu.setEnabled(true);
              
              coaster_item=new JMenuItem("Roller Coasters");
              ride_menu.add(coaster_item);
              
              menubar.add(ride_menu);
              setJMenuBar(menubar);
         }
    
         public void addMenuItemListeners()
         {
              coaster_item.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                        System.out.println("ActionListener for coaster called...");
                             remove_initial_screen();
                             display_coaster=true;
                   }
              });
         }
    
         public void remove_initial_screen()
         {
              System.out.println("Remove Initial Screen called...");
              if(tp_panel!=null){
                   tp_panel.setVisible(false);
                   tp_panel.removeAll();
              }
         }
    
         public void run_frame()
         {
              if(display_coaster){
                   if(panel!=null){
                        panel.setVisible(false);
                        panel.removeAll();
                   }
                   panel=new Coaster_Ride_Panel(screenWidth,screenHeight);
                   this.add(panel,BorderLayout.CENTER);
                   panel.repaint();
                   display_coaster=false;
              }
         }
    
         private Initial_Panel tp_panel;
         private JMenu ride_menu;
         private JMenuItem coaster_item;
         private boolean display_coaster;
         private JPanel panel;
         private JMenuBar menubar;
         private int screenWidth,screenHeight;
    }
    
    class Initial_Panel extends JPanel
    {
         public Initial_Panel(int w,int h)
         {
              screenWidth=w;
              screenHeight=h;
              setSize(2*(screenWidth/3),screenHeight);
              setBackground(Color.WHITE);
         }
         private int screenWidth,screenHeight;
    }
    
    class Coaster_Ride_Panel extends JPanel
    {
         public Coaster_Ride_Panel(int w,int h)
         {
              screenWidth=w;
              screenHeight=h;
              setBackground(new Color(220,220,12));
    
              setLocation(0,0);
              setPreferredSize(new Dimension(screenWidth,screenHeight));
              setSize(screenWidth,screenHeight);
              setLayout(new BorderLayout());
    
              generate_button=new JButton("Generate");
    
              Box b=Box.createVerticalBox();
              b.add(Box.createVerticalStrut(25));        
              b.add(generate_button);
              b.add(Box.createVerticalStrut(25));
    
              add(b,BorderLayout.CENTER);
              repaint();
              revalidate();
         }
    
         private int screenWidth,screenHeight;
         private JButton generate_button;
    
    }
    
    //Periodically checks the booleans to see which panel needs to be displayed
    class Poller implements Runnable
    {
         public Poller(WelcomeFrame f)
         {
              frame=f;
         }
         
         public void run()
         {
              try{
                   for(;;){
                        Thread.sleep(300);
                        frame.run_frame();
                        frame.repaint();
                        frame.setEnabled(true);
                   }
              }
              catch(Exception e){e.printStackTrace();}
         }
         private WelcomeFrame frame;
    }
  • 5. Re: The components placed in a JPanel do not load, until I resize the window.
    796262 Newbie
    Currently Being Moderated
    If you want to post an [SSCCE |http://sscce.org] (which should be one file, as small as possible), I'm sure somebody would be happy to look at your code.

    But I can save you a few hour and tell you right now, CardLayout is the way to go.

    Edit- Also, it looks like you're modifying your GUI on a Thread that's not the EDT. Don't do that.

    Check out [SwingUtilities.invokeLater()|http://download-llnw.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29] . Or better yet, it looks like you want to be using a Swing Timer instead of Threading.
  • 6. Re: The components placed in a JPanel do not load, until I resize the window.
    843853 Newbie
    Currently Being Moderated
    Guess what, the problem is solved if I call frame.pack() on that Poller class. Just thought that I would tell.
    Thanks for your time. I will keep CardLayout in mind the next time I have to do something like this. :-)
  • 7. Re: The components placed in a JPanel do not load, until I resize the window.
    796262 Newbie
    Currently Being Moderated
    icosahedronman wrote:
    Guess what, the problem is solved if I call frame.pack() on that Poller class. Just thought that I would tell.
    Thanks for your time. I will keep CardLayout in mind the next time I have to do something like this. :-)
    Well, I guess it's good you got your problem fixed. But I promise that your unfixed threading problems are going to give you some issues down the road. They are going to be hard to figure out and impossible to debug. You've been warned, and you're going to kick yourself for not fixing them sooner.

    Much luck.

    And just to re-re-reiterate, if you want to remove some components from a JPanel and add some other in their place, CardLayout is the way to go.
  • 8. Re: The components placed in a JPanel do not load, until I resize the window.
    morgalr Explorer
    Currently Being Moderated
    I don't see anyplace you call pack on your display object. Call pack() after you add your components and you should be fine.