JMenuBar Question

Hello there, i'm very new to this so if this is a silly question then don't treat me too harshly. I am trying to design a GUI and i'm having problems implementing the Menu (adding Load and Quit functions to the GUI), I think I have all the code that I need but the items just aren't displaying.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass GUI

implements ActionListener

{

private JFrame frame;

public GUI()

{

makeFrame();

}

privatevoid makeFrame()

{

frame =new JFrame("GUI");

Container contentPane = frame.getContentPane();

frame.setSize(250,250);

frame.setLocation(100,100);

frame.pack();

frame.setVisible(true);

}

publicvoid actionPerformed(ActionEvent event)

{

System.out.println("Item: " + event.getActionCommand());

}

privatevoid makeMenuBar(JFrame frame)

{

JMenuBar menubar =new JMenuBar();

frame.setJMenuBar(menubar);

JMenu fileMenu =new JMenu("File");

menubar.add(fileMenu);

JMenuItem openItem =new JMenuItem("Open");

openItem.addActionListener(this);

fileMenu.add(openItem);

JMenuItem quitItem =new JMenuItem("Quit");

quitItem.addActionListener(this);

fileMenu.add(quitItem);

}

}

Any tips or corrections would be much appreciated, especially if someone could also explain how the JFileChooser could be added to the Load function.

Thanks,

cjr87.

[2668 byte] By [cjr87a] at [2007-11-26 22:25:25]
# 1
You're not calling makeMenuBar() :o)I would also advise that you get into the habit of using Action implementations instead of configuring menu items manually with strings and listeners - it will make your life a lot easier in the long run.
itchyscratchya at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
Ah I see, seems very obvious now you point it out lol, thanks.I'm also having a problem now that the actual window will not resize, i've tried using the setSize but it doesn't seem to have any effect at all. Is this another no brainer as well?
cjr87a at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...
# 3
You mean it won't resize with the mouse, or setSize() has no effect?Don't forget that pack() implicitly sets the size of the frame, so if you call setSize() before that then it will be steamrollered.
itchyscratchya at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...
# 4
setSize was having no effect on it, but once I removed the pack() it resized it to normal. Only problem with that is that as soon as I did, the menu disappeared until I put the pack() back in.I'm sure i'm missing something really basic here but I can't think what :S
cjr87a at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...
# 5
pack() also does a few other things including validating the component hierarchy. I suggest you always call pack(). If you want to set the size explicitly, do so after your pack() call.
itchyscratchya at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...
# 6
That sounds fair enough. Once I set the size after the pack(), the menu bar doesn't show until i actually drag the window out bigger or smaller then it will work, strange.Thanks again for the help.
cjr87a at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...
# 7

Sounds like you're calling makeMenuBar() after you call pack().

You need to call it beforehand. Any time you add() or remove() from the component hierarchy, you need to validate() (as is done implicitly by pack() or indeed by the user resizing the frame) before it will be displayed on the screen.

itchyscratchya at 2007-7-10 11:26:18 > top of Java-index,Desktop,Core GUI APIs...