Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Agree with @MadProgrammer. You should put the codes--frame.pack(); frame.setVisible(true)-- on bottom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top