GUI help with menu

Hello Everyone:

I need some help with a GUI project.

What I want to do is when someone select the File Menu and chose Open a Jpanel will open.

I have tried to write the code which is called class labelListener implements ActionListener.

Here is the code I have so far

publicclass GUIMenuextends JFrame{

public GUIMenu(){

super("Menus");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Build first menu in the menu bar.

JMenuItem f1 =new JMenuItem("New");

f1.setMnemonic(KeyEvent.VK_N);

f1.getAccessibleContext().setAccessibleDescription (

"If this was working you could open a new file");

JMenuItem f2 =new JMenuItem("Open");

f2.setMnemonic(KeyEvent.VK_O);

f2.addActionListener(new labelListener());//loads/sets the listener

JMenuItem f3 =new JMenuItem("Exit");

//need help here

f3.setMnemonic(KeyEvent.VK_E);

f3.addActionListener(new exitListener());//loads/sets the listener

//Build second menu in the menu bar.

JMenuItem e1 =new JMenuItem("Copy");

e1.setMnemonic(KeyEvent.VK_C);

JMenuItem e2 =new JMenuItem("Cut");

e2.setMnemonic(KeyEvent.VK_X);

JMenuItem e3 =new JMenuItem("Paste");

e3.setMnemonic(KeyEvent.VK_P);

//Build third menu in the menu bar.

JMenuItem a1 =new JMenuItem("Close");

a1.setMnemonic(KeyEvent.VK_D);

a1.addActionListener(new exitListener());//loads/sets the listener

//Create the menu bar.

JMenuBar menubar =new JMenuBar();

//Build the first menu.

JMenu menu =new JMenu("File");

menu.add(f1);

menu.add(f2);

menu.addSeparator();

menu.add(f3);

menubar.add(menu);

//Build the Second menu.

JMenu menu2 =new JMenu("Edit");

menu2.add(e1);

menu2.add(e2);

menu2.add(e3);

menubar.add(menu2);

//Build the Third menu.

JMenu menu3 =new JMenu("Close the program");

menu3.add(a1);

menubar.add(menu3);

// prepare user interface

JTextArea edit =new JTextArea(8, 40);

JScrollPane scroll =new JScrollPane(edit);

BorderLayout bord =new BorderLayout();

setLayout(bord);

add("Center", scroll);

setJMenuBar(menubar);

pack();

setVisible(true);

}

//inner class Section used for exit on menu 3 for fun

class exitListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent arg0){

int x = JOptionPane.showOptionDialog(GUIMenu.this,"Exit Program?",

"Exit Request", JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE, null, null,

JOptionPane.NO_OPTION);

if (x == JOptionPane.YES_OPTION){

GUIMenu.this.dispose();

}//close the if statement

}//close public void actionPerformed

}//close the class exitListener

//this is the class I need help in

class labelListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent arg0){

String[] formats ={"Atom","RSS 0.92","RSS 1.0","RSS 2.0"};

JPanel JPanelPane;

JPanel x = JPanelPane =new JPanel();

JLabel formatLabel =new JLabel("Output formats:");

rootPane.add(formatLabel);

JComboBox formatBox =null;

for (int i = 0; i < formats.length; i++)

formatBox.addItem(formats[i]);

rootPane.add(formatBox);

add(rootPane);

setVisible(true);

{

GUIMenu.this.dispose();

}

}[/b]

}

publicstaticvoid main(String[] arguments){

GUIMenu frame =new GUIMenu();

}

Any help would be great

Thanks

red PS This is my first GUI...and I'm somewhat new to Java

Message was edited by:

Redheadashley

[6725 byte] By [Redheadashleya] at [2007-11-27 4:13:41]
# 1
> What I want to do is when someone select the File Menu and chose Open a Jpanel will open.you can remove/add/revalidate or set up a cardlayout etc, but what do you want to happen if 'open' is clicked twice?
Michael_Dunna at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...
# 2
I would like to open a Jpanel when they select File to New (Open) or even a call would be good(that is Hit the Alt + O and it opens the Jpanel) It'sa class assignment.thanksred
Redheadashleya at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...
# 3

what I'm getting at is you can remove the old component, then add the new JPanel

in its place - all will work well, but if you then click 'open' again, your program can

run into problems because the old component no longer exists.

do you want to open the panel in a separate window?

if so, perhaps a JOptionPane might be all you need.

Michael_Dunna at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...
# 4
Separate window would be great.Can you please tell me how to do that pleasethank for getting back to me so soonred
Redheadashleya at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...
# 5
see my comment in a different forum to same question, [url= http://forum.java.sun.com/thread.jspa?threadID=5172013&messageID=9662194#9662194]here[/url].
petes1234a at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...
# 6

for a JOptionPane, try something like this first. there are different optionPanes you can use.

Which is best depends on what you're trying to do

class labelListener implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

String[] formats = { "Atom", "RSS 0.92", "RSS 1.0", "RSS 2.0" };

JPanel JPanelPane = new JPanel(new GridLayout(2,1));

JLabel formatLabel = new JLabel("Output formats:");

JComboBox formatBox = new JComboBox(formats);

JPanelPane.add(formatLabel);

JPanelPane.add(formatBox);

JOptionPane.showMessageDialog(getContentPane(),JPanelPane);

}

}

Michael_Dunna at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...
# 7
thanks I will try thisred
Redheadashleya at 2007-7-12 9:19:58 > top of Java-index,Desktop,Core GUI APIs...