0

I am trying to add a second JPanel to my window, which uses BoxLayout. For some reason everything beyond my overridden JPanel refuses to appear.

Here's the code:

 public void initialize()
  {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Polygon Viewer");
    frame.setContentPane(makeGUI(frame));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,700);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
  }
  public JPanel makeGUI(final JFrame frame)
  {
    JPanel gui = new JPanel();
    gui.setLayout(new BoxLayout(gui,BoxLayout.PAGE_AXIS));

    class GraphPaint extends JPanel
    {
      public void paintComponent(Graphics g)
      {
        // Lots of graphics stuff
      }
    }

    GraphPaint mainG = new GraphPaint();
    mainG.setMinimumSize(new Dimension(600,600));
    mainG.setMaximumSize(new Dimension(600,600));
    mainG.setPreferredSize(new Dimension(600,600));
    gui.add(mainG);

    // Everything beyond here refuses to show up in the window

    JPanel lowerBar = new JPanel();
    lowerBar.setLayout(new BoxLayout(lowerBar,BoxLayout.LINE_AXIS));
    lowerBar.setMinimumSize(new Dimension(600,100));
    lowerBar.setPreferredSize(new Dimension(600,100));
    lowerBar.setBackground(Color.RED);
    gui.add(lowerBar);

    JPanel data = new JPanel();
    data.setLayout(new BoxLayout(data,BoxLayout.PAGE_AXIS));

    JLabel area = new JLabel("Area: <insert area here>");
    data.add(area);

    JLabel perimeter = new JLabel("Perimeter: " + shape.perimeter());
    data.add(perimeter);

    return gui;
  }

I must have messed up the BoxLayout setup, or can BoxLayout not contain other JPanels using BoxLayout?

Thrfoot
  • 125
  • 2
  • 7

1 Answers1

2

You never add the data panel to the gui.

Also, make sure you are calling super.paintComponent(g) (you've commented out that part of the code so I can't tell if you're doing it or not, but that may cause you problems if you don't)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Don't worry, I'm calling it. Also, shouldn't `lowerBar` show up as a red box at the bottom of the window? When I run the code it doesn't. Thanks for catching the `data` though. Also, `data` is meant to go into `lowerBar`. – Thrfoot Nov 22 '12 at 04:00
  • Actually, I just ran the code with `data` fixed, and it worked. I still don't understand why the red background is not appearing though. It only shows up above and below the two `JLabel`s. Is the `BoxLayout` resizing `lowerBar` or is it something else? – Thrfoot Nov 22 '12 at 04:02
  • `BoxLayout` lays components out in the order you add them, since your adding `lowerBar` first, it appears first – MadProgrammer Nov 22 '12 at 04:21