-2

I am new to Java Swing, and I'm not very familiar with different layout managers.

I would like to create a frame where there are 3 panels with one menu bar on top as shown here:

enter image description here

I had tried the border layout, but it only works if there ain't no menu bar. What layout manager should I use?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 2
    This [visual guide](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) is always a good start to learn Java Swing and see what could be done. – AxelH Aug 30 '17 at 13:49
  • `BorderLayout` does not preclude using `JMenuBar`, for [example](https://stackoverflow.com/a/38215252/230513). – trashgod Aug 30 '17 at 15:06
  • The menubar is part of the frame. Read the section from the Swing tutorial on [How to Use Menus](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) for working demo code to get you started. – camickr Aug 30 '17 at 21:21
  • Okay, I'll try every suggestions. Tnx – Anonymouse24 Aug 31 '17 at 01:03

3 Answers3

0

You should use GridLayout you can specify amount of rows and columns and add elements to it for example if you say you want 2 rows and 3 columns and start to add elements to it first 3 will go to row number one and second 3 will go to row number two you basically creating a table and fill the cells from left to right from up to down (by default) so in your case you will need 3 rows and one column anyway here is an example that will help you

public class GridLayoutTut {
private JFrame mainFrame;

public static void main(String[] args) {
    GridLayoutTut swingLayoutDemo = new GridLayoutTut();
    swingLayoutDemo.showStuff();
}

private void showStuff() {
    JMenuBar menuBar = new JMenuBar();
    JMenu menu;
    menu = new JMenu("My menu");
    menuBar.add(menu);

    mainFrame = new JFrame("Java SWING Examples");
    mainFrame.setSize(400, 400);
    JPanel comp = new JPanel();
    comp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Pane 1"));
    JPanel comp2 = new JPanel();
    comp2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Pane 2"));
    JPanel comp3 = new JPanel();
    comp3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Pane 3"));

    mainFrame.setLayout(new GridLayout(3, 1)); // 3 rows 1 column
    mainFrame.add(comp);
    mainFrame.add(comp2);
    mainFrame.add(comp3);
    mainFrame.add(menuBar);
    mainFrame.setJMenuBar(menuBar);
    mainFrame.setVisible(true);
}

}

urag
  • 1,228
  • 9
  • 28
0

I would recommend creating a custom LayoutManager.

Here is a basic one that does just what you asked for:

import java.awt.*;
import java.util.ArrayList;

public class MyLayoutManager implements LayoutManager {
    private final int TOOLBAR_HEIGHT = 30;
    private final int WINDOW_WIDTH = 800;
    private final int WINDOW_HEIGHT = 600;

    //  CONSTRAINTS  //
    public static final String TOOLBAR = "TOOLBAR";
    public static final String BODY = "BODY";

    private ArrayList<Component> toolbarComponents = new ArrayList<>();
    private ArrayList<Component> bodyComponents = new ArrayList<>();

    @Override   // Called when JFrame.add() is used  //
    public void addLayoutComponent(String constraints, Component component) {

        switch(constraints) {
            case "TOOLBAR": toolbarComponents.add(component);   break;
            case "BODY":    bodyComponents.add(component);      break;
        }
    }


    @Override   //  Sets the bounds for each component  //
    public void layoutContainer(Container parent) {
        //  Toolbar  //
        int currentWidth = 0;
        for(Component c : toolbarComponents) {
            int width = c.getPreferredSize().width;
            c.setBounds(currentWidth, 0, width, TOOLBAR_HEIGHT);
            currentWidth += width;
        }

        //  Body  //
        int currentHeight = TOOLBAR_HEIGHT;
        for(Component c : bodyComponents) {
            int height = (parent.getHeight()-TOOLBAR_HEIGHT)/bodyComponents.size();
            c.setBounds(0, currentHeight, parent.getWidth(), height);
            currentHeight += height;
        }
    }


    @Override
    public Dimension minimumLayoutSize(Container parent) {
        return preferredLayoutSize(parent);
    }


    @Override   //  Called when JFrame.pack() is called  //
    public Dimension preferredLayoutSize(Container parent) {
        return new Dimension(WINDOW_WIDTH,WINDOW_HEIGHT);
    }


    @Override
    public void removeLayoutComponent(Component comp) {}

}

Then Implement it like so:

    JFrame frame = new JFrame("Interface");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new MyLayoutManager());

    frame.add(new JButton("Menu"), MyLayoutManager.TOOLBAR);
    frame.add(new JButton("File"), MyLayoutManager.TOOLBAR);
    frame.add(new JButton("Settings"), MyLayoutManager.TOOLBAR);

    JTextArea area1 = new JTextArea();
    area1.setBorder(new TitledBorder("title 1"));

    JTextArea area2 = new JTextArea();
    area2.setBorder(new TitledBorder("title 2"));

    frame.add(area1, MyLayoutManager.BODY);
    frame.add(area2, MyLayoutManager.BODY);

    frame.pack();

    frame.setVisible(true);
micro_man
  • 740
  • 7
  • 11
0

The JFrame class has a setJMenuBar(JMenuBar) method, so that the JMenuBar will automatically appear at the top of the JFrame.

For layout of your panels I would recommend the GridBagLayout. By adding your 3 panels with different GridBagConstraints you can tweak their sizes/positions within the JFrame. For the meaning of these you need to read the javadoc of GridBagConstraints.

    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu1 = new JMenu("Menu1");
    JMenu menu2 = new JMenu("Menu2");
    JMenu menu3 = new JMenu("Menu3");
    menuBar.add(menu1);
    menuBar.add(menu2);
    menuBar.add(menu3);
    frame.add(menuBar);
    frame.setJMenuBar(menuBar);

    JPanel panel1 = new JPanel();
    panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Panel 1"));
    JPanel panel2 = new JPanel();
    panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Panel 2"));
    JPanel panel3 = new JPanel();
    panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Panel 3"));

    frame.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.gridy = 0;
    gbc.weighty = 0.1;
    frame.add(panel1, gbc);
    gbc.gridy = 1;
    gbc.weighty = 0.3;
    frame.add(panel2, gbc);
    gbc.gridy = 2;
    gbc.weighty = 0.2;
    frame.add(panel3, gbc);

    frame.setSize(400, 400);
    frame.setVisible(true);
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49