help with GUI assign a keystroke

I'm trying to figure out how to assign a keystroke and a click to a menu item.

Example If a user click on a menu and selects theClose the program

Menu and choseClose[/b]. I want the program to open (//inner class Section used for exit on menu 3 for fun) ie "Exit Program".

here is my code so far.

publicclass GUIMenuextends JFrame{

public GUIMenu(){

super("FeedBar 2");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Build third menu in the menu bar.

JMenuItem a1 =new JMenuItem("Close");

a1.setMnemonic(KeyEvent.VK_D);

//Create the menu bar.

JMenuBar menubar =new JMenuBar();

//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

publicstaticvoid main(String[] arguments){

GUIMenu frame =new GUIMenu();

}

}

Thanks

SandyR

[3245 byte] By [SandyReda] at [2007-11-27 3:22:56]
# 1

I see where you create the MenuItem (a1) .. I see where you add the MenuItem into the Menu .. and I see where you add the Menu into the JMenuBar.. But I *don't* see where you add an action listener to a1. Maybe I'm missing it, but have you done that? A MenuItem won't do anything unless it has some objects listening for it.

You would need to add an a1.addActionListener( <action listener in here> )

JJCoolBa at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for the help...Sorry for this but this is very new to me. Infact this is my 4 projectSo I need to all a new class... class okListener implements ActionListener(a1.addActionListener;)SandyRMessage was edited by: SandyRed
SandyReda at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...
# 3

You shouldn't need to code out a new action listener.. You already have one in your code.

class exitListener implements ActionListener {

public void 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

You simply need to register an instance of that class as a listener to the menu item.

a1.addActionListener(new exitListener());

JJCoolBa at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks..you gave me the clue but I just did not see it.sandyR
SandyReda at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...
# 5

I have one more question to ask...

I want to add another listener which would open another window.

How do I do that. I have looked for it in my Java book, but I can not find any info on it.

I would like to place on JMenuItem f2

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);

[b]f2.addActionListener(Don't know what is next);[/b]

Project name would be FormatFrame2.java

Thanks

SandyR

Message was edited by:

SandyRed

Message was edited by:

SandyRed

SandyReda at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...
# 6

To do that, you'll probably have to program out your own ActionListener.. It would be *similar* to the exitListener that you have above, except the code inside of it would be responsible for opening up a new Frame and I'd probably rename the class from exitListener to openListener (or some other such applicable name).

Either way, it'll at least look similar to the ActionListener you already have -- just the guts will be different. Then you'll need to (again) add the class as a listener to the menu item like you did with the first one.

JJCoolBa at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...
# 7

Taking your advice I did this but it is not working when I select File menu Open.

here is my code

public class GUIMenu extends JFrame {

public GUIMenu() {

super("FeedBar 2");

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");

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 exitListener implements ActionListener {

public void 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

class labelListener implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

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

JComboBox formatBox = new JComboBox();

JPanel pane = new JPanel();

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

pane.add(formatLabel);

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

formatBox.addItem(formats[i]);

pane.add(formatBox);

add(pane);

setVisible(true);

}

}

public static void main(String[] arguments) {

GUIMenu frame = new GUIMenu();

}

}

SandyR

SandyReda at 2007-7-12 8:25:41 > top of Java-index,Java Essentials,New To Java...