0

I am trying to use a nested JPanel, which I can then reuse in different parts of my application, eg navigation bar at the top of the page. I am having trouble setting the orientation of the items, eg I want the button to be above the textfield.

If I create them individually and add them directly to the JPanel they come out one on top of the other as inteneded, as below:

    final JFrame frame = new JFrame("Nested Layout Example");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      final JPanel gui = new JPanel(new BorderLayout(5,5));
                    gui.setBorder( new TitledBorder("BorderLayout(5,5)") );

                    JButton button = new JButton("Button");
                    JButton button1 = new JButton("Button1");
                    gui.add(button, BorderLayout.NORTH);
                    gui.add(button1, BorderLayout.SOUTH);

 frame.setContentPane(gui);

                frame.pack();

                frame.setLocationRelativeTo(null);
                try {
                    // 1.6+
                    frame.setLocationByPlatform(true);
                    frame.setMinimumSize(frame.getSize());
                } catch(Throwable ignoreAndContinue) {
                }

                frame.setVisible(true);

However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side, as below:

 final JFrame frame = new JFrame("Nested Layout Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JPanel gui = new JPanel(new BorderLayout(5,5));
                gui.setBorder( new TitledBorder("BorderLayout(5,5)") );

                JPanel container = new JPanel();
                JButton button = new JButton("Button");
                JButton button1 = new JButton("Button1");
                container.add(button, BorderLayout.NORTH);
                container.add(button1, BorderLayout.SOUTH);

                gui.add(container);

               frame.setContentPane(gui);

                frame.pack();

                frame.setLocationRelativeTo(null);
                try {
                    // 1.6+
                    frame.setLocationByPlatform(true);
                    frame.setMinimumSize(frame.getSize());
                } catch(Throwable ignoreAndContinue) {
                }

                frame.setVisible(true);

I have tried setting the componenetOrientation,

container.setComponentOrientation(ComponentOrientation.);

but there is no option for vertical

  • 2
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Oct 27 '14 at 11:38

2 Answers2

1

I have tried setting the componenetOrientation

Please note the problem has nothing to do with component orientation: it's a layout manager problem as explained below.

However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side

Here:

JPanel container = new JPanel();
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);

The default layout manager for panels is FlowLayout and it will ignore BorderLayout constraints. You'll have to set BorderLayout as layout manager not only to gui panel but to container panel as well.

JPanel container = new JPanel(new BorderLayout());
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
dic19
  • 17,821
  • 6
  • 40
  • 69
0

Try setting a layout for your JPanel. There are many Layouts available in the package java.awt. Some of them are BorderLayout, GridBagLayout, CardLayout, FlowLayout, GridLayout.

I have just added One line to your code, and now, it puts the buttons in the way you want:

        final JFrame frame = new JFrame("Nested Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel gui = new JPanel(new BorderLayout(5,5));
        gui.setBorder( new TitledBorder("BorderLayout(5,5)") );

        JPanel container = new JPanel();

        container.setLayout(new GridLayout(2,1)); // This is the line that I have added.

        JButton button = new JButton("Button");
        JButton button1 = new JButton("Button1");
        container.add(button, BorderLayout.NORTH);
        container.add(button1, BorderLayout.SOUTH);

        gui.add(container);

        frame.setContentPane(gui);

        frame.pack();

        frame.setLocationRelativeTo(null);
        try {
              // 1.6+
              frame.setLocationByPlatform(true);
              frame.setMinimumSize(frame.getSize());
        } catch(Throwable ignoreAndContinue) {
        }

        frame.setVisible(true);

If you want to add more buttons, you can edit that line. The first Parameter of the constructor method of GridLayout is the number of vertical columns, and the second is the number of horizontal columns.

dryairship
  • 6,022
  • 4
  • 28
  • 54