tags:

views:

73

answers:

2

I'm trying to modify a GUI. It is hosting a GLCanvas displaying JOGL content.

Here is the code to set it up:

private void setupWindow() {
    this.frame = new JFrame(WINDOW_TITLE);
    frame.setSize(width, height);
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(listener);
    menu.add(exitItem);
    frame.setJMenuBar(menuBar);
}

Currently, the canvas takes up the entire space in the window, aside from the menu bar. I'd like to make space for other controls in the window, like buttons and list boxes. How can I do it?

I tried inserting the following, but it didn't work:

private void setupWindow() {
    this.frame = new JFrame(WINDOW_TITLE);
    frame.setSize(width, height);

    // ** inserted the following:
    JPanel canvasPanel = new JPanel(new BorderLayout());
    canvasPanel.add(canvas);
    canvasPanel.setSize(30, 40);
    canvasPanel.setVisible(true);
    // **

    frame.add(canvasPanel);
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(listener);
    menu.add(exitItem);
    frame.setJMenuBar(menuBar);
}

This doesn't modify the appearance of the window at all.

What should I be doing here? I'm not too familiar with Java GUIs.

Update: Changing the constructor's argument from BorderLayout to FlowLayout causes the GLCanvas to disappear.

A: 

I suggest checking out this guide to layout managers - it will give you enough info to see what you should try.

Anon
+1  A: 

Per Anon's answer, you really need to better understand the layout managers. Your question is too open ended.

In case this helps though:

    JPanel canvasPanel = new JPanel(new BorderLayout());
    frame.add(canvasPanel, BorderLayout.CENTER);

    canvasPanel.add(canvas);


    Box leftBtnsBox = Box.createVerticalBox();
    frame.add(leftBtns, BorderLayout.WEST);

    leftBtns.add(new JButton("Button 1"));
    leftBtns.add(new JButton("Button 2"));

This code sets a BordeLayout explicitly, but I think the default layout manager for panels (and content panes) is BorderLayout. This code will put two buttons to the top left of the canvas panel arranged vertically. Because you are using these layout managers, your second code example's setSize had no effect.

kaliatech
1) I think it is better to set layouts explicitly since AFAIR Sun changed the default layout of top level containers from FlowLayout to BorderLayout some time around 1.4. 2) The default layout for JPanels at the moment is FlowLayout. 3) Besides using a Box for the JButtons, the OP might also consider a JToolBar.
Andrew Thompson