Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2006

Tip: Looking for answers? Try searching our database.

menuKeyTyped and menuKeyReleased methods do not work.

Thread view: 
niceguy16 - 10 May 2006 03:44 GMT
Hi All,

The code below has 3 menu items.  If I highlight a menu item and press
the Enter key, it will run

System.out.println("press");

3 times.

My first question is why three times but not one?
My next question is why it not run System.out.println("type");?

Thank a lot.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MenuEnterKey extends JFrame implements MenuKeyListener,
ItemListener{
JLabel l=new JLabel();
JLabel a=new JLabel("Enter Loan Amount");
JLabel pay=new JLabel("");
ButtonGroup group0 = new ButtonGroup( );
JRadioButtonMenuItem year7 = new JRadioButtonMenuItem("7 Years at
5.35%");
JRadioButtonMenuItem year15 = new JRadioButtonMenuItem("15 Years at
5.5%");
JRadioButtonMenuItem year30 = new JRadioButtonMenuItem("30 Years at
5.75%",true);
Container pane;
public MenuEnterKey(){
super("Loan Payment");
setSize(300, 200);
setLocation(200, 200);
JMenuItem quitItem = new JMenuItem("Exit");
year7.addMenuKeyListener(this);
year15.addMenuKeyListener(this);
year30.addMenuKeyListener(this);
year7.addItemListener(this);
year15.addItemListener(this);
year30.addItemListener(this);
// create the term menu
JMenu menu1 = new JMenu("Select a Term");
menu1.setMnemonic('S');
year7.setMnemonic('7');
year15.setMnemonic('1');
year30.setMnemonic('3');
group0.add(year7);
menu1.add(year7);
group0.add(year15);
menu1.add(year15);
group0.add(year30);
menu1.add(year30);
menu1.addSeparator( );
menu1.add(quitItem);
// create a menu bar and use it in this JFrame
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu1);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
FlowLayout flo = new FlowLayout(FlowLayout.LEFT,10,20);
BorderLayout brd = new BorderLayout(5,5);
pane = getContentPane();
p1.setLayout(brd);
p2.setLayout(flo);
pane.setLayout(flo);
pane.add(l);
pane.add(p2);
pane.add(p1);
setJMenuBar(menuBar);
}

public void menuKeyTyped(MenuKeyEvent e){
        System.out.println("type");
    }

public void menuKeyReleased(MenuKeyEvent e){
        System.out.println("release");
   }

public void menuKeyPressed(MenuKeyEvent e){
        int key = e.getKeyCode();
        if (key!=e.VK_ENTER) {
        /*if (e.getSource() == year7){
                    l.setText("Selected Term is 7 years.");
            }
            if (year15.isSelected()){
                    l.setText("Selected Term is 15 years.");
            }
            if (year30.isSelected()){
                    l.setText("Selected Term is 30 years.");
    }*/
            e.consume();
        }
        else
            System.out.println("press");
   }

public void itemStateChanged(ItemEvent e) {
    if (e.getSource() == year7){
            l.setText("Selected Term is 7 years.");
    }
    if (e.getSource() == year15){
            l.setText("Selected Term is 15 years.");
    }
    if (e.getSource() == year30){
            l.setText("Selected Term is 30 years.");
    }}

public static void main(String[] args){
    MenuEnterKey f= new MenuEnterKey();
    f.setVisible(true);
}}
hiwa - 10 May 2006 08:15 GMT
Try this simplified one:
----------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MenuEnterKey extends JFrame
implements MenuKeyListener, ItemListener{

 JLabel l = new JLabel();
 JLabel a = new JLabel("Enter Loan Amount");
 JLabel pay = new JLabel("");

 ButtonGroup group0 = new ButtonGroup( );

 JRadioButtonMenuItem year7 = new JRadioButtonMenuItem("7 Years at
5.35%");
 JRadioButtonMenuItem year15 = new JRadioButtonMenuItem("15 Years at
5.5%");
 JRadioButtonMenuItem year30
  = new JRadioButtonMenuItem("30 Years at 5.75%",true);

 Container pane;

 public MenuEnterKey(){
   super("Loan Payment");
   setSize(300, 200);
   setLocation(200, 200);
   setDefaultCloseOperation(EXIT_ON_CLOSE);

   JMenuItem quitItem = new JMenuItem("Exit");

   year30.addMenuKeyListener(this);

   year7.addItemListener(this);
   year15.addItemListener(this);
   year30.addItemListener(this);

   JMenu menu1 = new JMenu("Select a Term");
   menu1.setMnemonic('S');
   year7.setMnemonic('7');
   year15.setMnemonic('1');
   year30.setMnemonic('3');

   group0.add(year7);
   menu1.add(year7);
   group0.add(year15);
   menu1.add(year15);
   group0.add(year30);
   menu1.add(year30);
   menu1.addSeparator( );
   menu1.add(quitItem);

   // create a menu bar and use it in this JFrame
   JMenuBar menuBar = new JMenuBar();
   menuBar.add(menu1);

   JPanel p1 = new JPanel();
   JPanel p2 = new JPanel();
   FlowLayout flo = new FlowLayout(FlowLayout.LEFT,10,20);
   BorderLayout brd = new BorderLayout(5,5);
   pane = getContentPane();
   p1.setLayout(brd);
   p2.setLayout(flo);
   pane.setLayout(flo);
   pane.add(l);
   pane.add(p2);
   pane.add(p1);
   setJMenuBar(menuBar);
 }

 public void menuKeyTyped(MenuKeyEvent e){
   System.out.println("type " + e.getKeyText(e.getKeyCode()));
 }

 public void menuKeyReleased(MenuKeyEvent e){
   System.out.println("release " + e.getKeyText(e.getKeyCode()));
 }

 public void menuKeyPressed(MenuKeyEvent e){
     System.out.println("press " + e.getKeyText(e.getKeyCode()));
 }

 public void itemStateChanged(ItemEvent e) {
   if (e.getSource() == year7){
     l.setText("Selected Term is 7 years.");
   }
   else if (e.getSource() == year15){
     l.setText("Selected Term is 15 years.");
   }
   else if (e.getSource() == year30){
     l.setText("Selected Term is 30 years.");
   }
 }

 public static void main(String[] args){
   MenuEnterKey f= new MenuEnterKey();
   f.setVisible(true);
 }
}
--------------------------------------------------------------------
niceguy16 - 10 May 2006 17:16 GMT
Thanks a lot.

Do you know why when I press the Enter key on one menu item, the other
two menu items also receive the Enter key?

It looks like the menuKeyPressed() method works with the Enter key and
the space key and the menuKeyTyped() and released method do not work
with these two keys.  Why?
hiwa - 11 May 2006 11:11 GMT
According to the API documentation, ...typed()
method should work only for keys having characters.
[Enter], [F1] etc. do not have characters.

> I press the Enter key on one menu item
You press it on the menu, not on a particular menu item.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.