Adding panels to frame, but not showing when app is run

29,314

Solution 1

When you add components to a container you may need to invalidate the container hierarchy in order to get them to become visible...

The problems is as highlight when you set the frame visible BEFORE you've added anything to it...

public static void drawFrame(){
    // Create frame
    JFrame frame = new JFrame("Frame");
    // Set default close operation
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Set frame attributes

    // !! Don't do this here...
    //frame.setVisible(true);
    // ... IMHO, better to use pack...
    //frame.setSize(400, 300);
    frame.setResizable(false);
    // Set Layout
    frame.setLayout(new BorderLayout());
    // Add Components
    frame.add(drawMenuBar(), BorderLayout.NORTH);
    JPanel twinPane = new JPanel();
    frame.add(twinPane, BorderLayout.CENTER);
    twinPane.setLayout(new GridLayout(1, 2));
    twinPane.add(drawForm());
    twinPane.add(drawInfo());

    // !! Do it here instead...
    frame.pack();
    frame.setVisible(true);
} // Ends method drawFrame

Solution 2

You're setting the JFrame visible before you've added the components. Don't do that. Add the components, pack() your JFrame, and only then set it visible.

But regardless of any answers you might receive, your question has been asked many times before, including: Why shouldn't I call setVisible(true) before adding components?. Voting to close as a duplicate since the question is unlikely to help future visitors.

Share:
29,314

Related videos on Youtube

Joshua Martin
Author by

Joshua Martin

I have always had an interest in programming, and recently decided to get started by learning Java. I found a website that got me started: Java Made Easy However, the website's tutorials ended just short of showing me some of the essential concepts of Java that I needed to know. I therefor got a book at my local Chapters store, Beginning Programming with Java by Barry Burd, which got me well on my way to learning Java by showing all the basic concepts I needed to get started. Now, I have joined StackOverflow because I have found many answers to my questions here. Some of my other other hobbies include: photography, minecraft and watching movies

Updated on September 23, 2020

Comments

  • Joshua Martin
    Joshua Martin over 3 years

    I am crating an app with two panels in the frame along with a menu bar along the top. The menu bar shows up just fine, and any actions set so far work, but the other two panels never appear.

    I have tried retracing all the panels and lines that add them on to the frame and cannot find any errors.

    The first of the two panes that do not show, form in the drawForm() method, did show before I added some components, but since have not shown even when I remove the components again.

    Here is the class Frame:

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Frame {
        public static void drawFrame(){
            // Create frame
            JFrame frame = new JFrame("Frame");
            // Set default close operation
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // Set frame attributes
            frame.setVisible(true);
            frame.setSize(400, 300);
            frame.setResizable(false);
            // Set Layout
            frame.setLayout(new BorderLayout());
            // Add Components
            frame.add(drawMenuBar(), BorderLayout.NORTH);
            JPanel twinPane = new JPanel();
                frame.add(twinPane, BorderLayout.CENTER);
                twinPane.setLayout(new GridLayout(1, 2));
                twinPane.add(drawForm());
                twinPane.add(drawInfo());
        } // Ends method drawFrame
    
        public static JMenuBar drawMenuBar(){
            //Create menu structure
            JMenuBar menu = new JMenuBar();
                JMenu file = new JMenu("File");
                    JMenuItem clear = new JMenuItem("Clear");
                    JMenuItem calculate = new JMenuItem("calculate");
                    JMenuItem exit = new JMenuItem("Exit");
                JMenu help = new JMenu("Help");
                    JMenuItem about = new JMenuItem("About");
                    JMenuItem instructions = new JMenuItem("Instructions");
            //Add menu items to repective area of menu tree
            menu.add(file);
                file.add(clear);
                file.add(calculate);
                file.add(exit);
            menu.add(help);
                help.add(about);
                help.add(instructions);
            //Add ActionListener
            exit.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    MainApp app = new MainApp();
                    app.exit();
                }
    
            });
            //Set Return
            return menu;
        } // Ends method drawMenuBar
    
        public static JPanel drawForm(){
            //Set panel with layout and border
            JPanel form = new JPanel();
            form.setBorder(BorderFactory.createTitledBorder("Form"));
            form.setLayout(new BoxLayout(form, BoxLayout.PAGE_AXIS));
            //Create field and labels for form panel and add to form panel
            JLabel label1 = new JLabel("text1");
                form.add(label1);
            JTextField textfield1 = new JTextField(5);
                form.add(textfield1);
            JLabel label2 = new JLabel("text2");
                form.add(label2);
            JTextField textfield2 = new JTextField(5);
                form.add(textfield2);
            JButton calc = new JButton("calculate");
                form.add(calc);
            JTextField result = new JTextField(5);
                form.add(result);
            //Set return
            return form;
        } // Ends method drawForm
    
        public static JPanel drawInfo(){
            //Set Panel with layout and border
            JPanel info = new JPanel();
            info.setBorder(BorderFactory.createTitledBorder("Information"));
            //Set Return
            return info;
        } // Ends method drawInfo
    
    } // Ends class Frame
    

    The main method is in another class, but the class Frame creates the GUI. The frame along with the menu bar work perfectly, but everything after that does nothing.

    I appreciate any help, thank you

    Josh

    • Hovercraft Full Of Eels
      Hovercraft Full Of Eels over 10 years
      Regardless of our answers, your question has been asked many times before, including: Why shouldn't I call setVisible(true) before adding components?. Voting to close as a duplicate since the question is unlikely to help future visitors.
    • MadProgrammer
      MadProgrammer over 10 years
      @HovercraftFullOfEels Second...

Related