Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: menu item event

  1. #1
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default menu item event

    I want the program to exit when I click exit in the menu item. I don't know what I am doing wrong. Maybe its got something to do with choosing the correct event handler?
    Java Code:
     private void menuItem1MouseClicked(MouseEvent e) {
    		  System.exit(0);
    	  }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: menu item event

    Show us more code. Where is the line where you are registering the listener? Where is the mouseClicked method and the call of your own menuItem1MouseClicked method ?!

  3. #3
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: menu item event

    Here is all of the code haha
    Java Code:
       import java.awt.*;
       import java.awt.event.*;
       import java.beans.*;
       import javax.swing.*;
    import javax.swing.event.*;
    //Johannes van Zyl
    //Userinfo
    //GUI wat die inligting vanaf die gebruiker kry.
    
        public class Userinfo extends JFrame 
       {
         
           public Userinfo() 
          {
             initComponents();
             this.setVisible(true);
          }
          
           public static void main(String[]args)
          {
             Userinfo obj = new Userinfo();
          }
       
       
           private void button1MouseClicked(MouseEvent e) 
          {
             // TODO add your code here
          }
       
           private void button2MouseClicked(MouseEvent e) 
          {
             // TODO add your code here
          }
       
           private void radioButton1MouseClicked(MouseEvent e)
          {
          // TODO add your code here
          }
       
           private void radioButton2MouseClicked(MouseEvent e)
          {
           // TODO add your code here
          }
    
    	  private void menuItem1MouseClicked(MouseEvent e) {
    		  System.exit(0);
    	  }
       
           		
           private void initComponents() {
          // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    	  // Generated using JFormDesigner non-commercial license
    	  menuBar1 = new JMenuBar();
    	  menu1 = new JMenu();
    	  menuItem1 = new JMenuItem();
    	  label1 = new JLabel();
    	  label2 = new JLabel();
    	  textField1 = new JTextField();
    	  textField2 = new JTextField();
    	  label3 = new JLabel();
    	  label4 = new JLabel();
    	  textField3 = new JTextField();
    	  textField4 = new JTextField();
    	  button1 = new JButton();
    	  label5 = new JLabel();
    	  textField5 = new JTextField();
    	  textField6 = new JTextField();
    	  textField7 = new JTextField();
    	  separator1 = new JSeparator();
    	  label6 = new JLabel();
    	  label7 = new JLabel();
    	  label8 = new JLabel();
    	  label9 = new JLabel();
    	  label10 = new JLabel();
    	  label12 = new JLabel();
    	  label13 = new JLabel();
    	  textField8 = new JTextField();
    	  textField9 = new JTextField();
    	  textField10 = new JTextField();
    	  textField12 = new JTextField();
    	  textField13 = new JTextField();
    	  textField14 = new JTextField();
    	  scrollPane1 = new JScrollPane();
    	  textPane1 = new JTextPane();
    	  button2 = new JButton();
    	  label11 = new JLabel();
    	  radioButton1 = new JRadioButton();
    	  radioButton2 = new JRadioButton();
    
    	  //======== this ========
    	  setTitle("Table Tennis Score Keeper");
    	  Container contentPane = getContentPane();
    	  contentPane.setLayout(null);
    
    	  //======== menuBar1 ========
    	  {
    
    		  //======== menu1 ========
    		  {
    			  menu1.setText("Options");
    
    			  //---- menuItem1 ----
    			  menuItem1.setText("Exit");
    			  menuItem1.addMouseListener(new MouseAdapter() {
    				  @Override
    				  public void mouseClicked(MouseEvent e) {
    					  menuItem1MouseClicked(e);
    				  }
    			  });
    			  menu1.add(menuItem1);
    		  }
    		  menuBar1.add(menu1);
    	  }
    	  setJMenuBar(menuBar1);
    
    	  //---- label1 ----
    	  label1.setText("Player 1 name");
    	  label1.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label1);
    	  label1.setBounds(new Rectangle(new Point(75, 100), label1.getPreferredSize()));
    
    	  //---- label2 ----
    	  label2.setText("Player 2 name");
    	  label2.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label2);
    	  label2.setBounds(75, 150, 95, 15);
    
    	  //---- textField1 ----
    	  textField1.setBackground(new Color(0, 102, 0));
    	  textField1.setForeground(Color.lightGray);
    	  textField1.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField1.setText("Ping");
    	  contentPane.add(textField1);
    	  textField1.setBounds(40, 120, 155, textField1.getPreferredSize().height);
    
    	  //---- textField2 ----
    	  textField2.setBackground(new Color(0, 102, 0));
    	  textField2.setForeground(Color.lightGray);
    	  textField2.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField2.setText("Pong");
    	  contentPane.add(textField2);
    	  textField2.setBounds(40, 170, 155, 26);
    
    	  //---- label3 ----
    	  label3.setText("Play up to");
    	  label3.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label3);
    	  label3.setBounds(245, 100, 70, 15);
    
    	  //---- label4 ----
    	  label4.setText("Rotates serves every");
    	  label4.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label4);
    	  label4.setBounds(210, 150, 140, 15);
    
    	  //---- textField3 ----
    	  textField3.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField3.setText("21");
    	  textField3.setHorizontalAlignment(SwingConstants.CENTER);
    	  contentPane.add(textField3);
    	  textField3.setBounds(255, 120, 45, textField3.getPreferredSize().height);
    
    	  //---- textField4 ----
    	  textField4.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField4.setText("2");
    	  textField4.setHorizontalAlignment(SwingConstants.CENTER);
    	  contentPane.add(textField4);
    	  textField4.setBounds(255, 170, 45, 26);
    
    	  //---- button1 ----
    	  button1.setText("Start");
    	  button1.setFont(new Font("Tahoma", Font.BOLD, 16));
    	  button1.setToolTipText("Starts program");
    	  button1.addMouseListener(new MouseAdapter() {
    		  @Override
    		  public void mouseClicked(MouseEvent e) {
    			  button1MouseClicked(e);
    		  }
    	  });
    	  contentPane.add(button1);
    	  button1.setBounds(160, 210, 80, button1.getPreferredSize().height);
    
    	  //---- label5 ----
    	  label5.setText("Date:");
    	  label5.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label5);
    	  label5.setBounds(40, 75, 45, 15);
    
    	  //---- textField5 ----
    	  textField5.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField5.setText("YYYY");
    	  textField5.setHorizontalAlignment(SwingConstants.LEFT);
    	  contentPane.add(textField5);
    	  textField5.setBounds(75, 70, 50, 26);
    
    	  //---- textField6 ----
    	  textField6.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField6.setText("MM");
    	  textField6.setHorizontalAlignment(SwingConstants.LEFT);
    	  contentPane.add(textField6);
    	  textField6.setBounds(125, 70, 30, 26);
    
    	  //---- textField7 ----
    	  textField7.setFont(new Font("Tahoma", Font.PLAIN, 16));
    	  textField7.setText("DD");
    	  textField7.setHorizontalAlignment(SwingConstants.LEFT);
    	  contentPane.add(textField7);
    	  textField7.setBounds(155, 70, 30, 26);
    	  contentPane.add(separator1);
    	  separator1.setBounds(0, 245, 405, 5);
    
    	  //---- label6 ----
    	  label6.setText("Extra player information (not compulsory):");
    	  label6.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label6);
    	  label6.setBounds(55, 250, 275, 15);
    
    	  //---- label7 ----
    	  label7.setText("Date of birth:");
    	  label7.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label7);
    	  label7.setBounds(15, 270, 90, 15);
    
    	  //---- label8 ----
    	  label8.setText("Gender:");
    	  label8.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label8);
    	  label8.setBounds(15, 290, 55, 15);
    
    	  //---- label9 ----
    	  label9.setText("Place of birth:");
    	  label9.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label9);
    	  label9.setBounds(15, 315, 95, 15);
    
    	  //---- label10 ----
    	  label10.setText("Member of:");
    	  label10.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label10);
    	  label10.setBounds(15, 335, 75, 15);
    
    	  //---- label12 ----
    	  label12.setText("Current rank:");
    	  label12.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label12);
    	  label12.setBounds(15, 355, 85, 15);
    
    	  //---- label13 ----
    	  label13.setText("Motto:");
    	  label13.setFont(new Font("Tahoma", Font.BOLD, 12));
    	  contentPane.add(label13);
    	  label13.setBounds(15, 370, 45, 15);
    
    	  //---- textField8 ----
    	  textField8.setText("YYYY");
    	  textField8.setHorizontalAlignment(SwingConstants.CENTER);
    	  contentPane.add(textField8);
    	  textField8.setBounds(115, 270, 35, textField8.getPreferredSize().height);
    
    	  //---- textField9 ----
    	  textField9.setText("MM");
    	  textField9.setHorizontalAlignment(SwingConstants.CENTER);
    	  contentPane.add(textField9);
    	  textField9.setBounds(150, 270, 25, 20);
    
    	  //---- textField10 ----
    	  textField10.setText("DD");
    	  textField10.setHorizontalAlignment(SwingConstants.CENTER);
    	  contentPane.add(textField10);
    	  textField10.setBounds(175, 270, 25, 20);
    	  contentPane.add(textField12);
    	  textField12.setBounds(115, 315, 240, textField12.getPreferredSize().height);
    	  contentPane.add(textField13);
    	  textField13.setBounds(115, 335, 240, 20);
    	  contentPane.add(textField14);
    	  textField14.setBounds(115, 355, 240, 20);
    
    	  //======== scrollPane1 ========
    	  {
    		  scrollPane1.setViewportView(textPane1);
    	  }
    	  contentPane.add(scrollPane1);
    	  scrollPane1.setBounds(115, 375, 240, 55);
    
    	  //---- button2 ----
    	  button2.setText("Save information and start");
    	  button2.setFont(new Font("Tahoma", Font.BOLD, 16));
    	  button2.setToolTipText("Saves information and starts program");
    	  button2.addMouseListener(new MouseAdapter() {
    		  @Override
    		  public void mouseClicked(MouseEvent e) {
    			  button2MouseClicked(e);
    		  }
    	  });
    	  contentPane.add(button2);
    	  button2.setBounds(60, 440, 270, 29);
    
    	  //---- label11 ----
    	  label11.setText("Table Tennis Score Keeper");
    	  label11.setFont(new Font("Tahoma", Font.BOLD, 30));
    	  contentPane.add(label11);
    	  label11.setBounds(0, 10, 405, label11.getPreferredSize().height);
    
    	  //---- radioButton1 ----
    	  radioButton1.setText("Male");
    	  radioButton1.addMouseListener(new MouseAdapter() {
    		  @Override
    		  public void mouseClicked(MouseEvent e) {
    			  radioButton1MouseClicked(e);
    		  }
    	  });
    	  contentPane.add(radioButton1);
    	  radioButton1.setBounds(new Rectangle(new Point(115, 290), radioButton1.getPreferredSize()));
    
    	  //---- radioButton2 ----
    	  radioButton2.setText("Female");
    	  radioButton2.addMouseListener(new MouseAdapter() {
    		  @Override
    		  public void mouseClicked(MouseEvent e) {
    			  radioButton2MouseClicked(e);
    		  }
    	  });
    	  contentPane.add(radioButton2);
    	  radioButton2.setBounds(new Rectangle(new Point(165, 290), radioButton2.getPreferredSize()));
    
    	  { // compute preferred size
    		  Dimension preferredSize = new Dimension();
    		  for(int i = 0; i < contentPane.getComponentCount(); i++) {
    			  Rectangle bounds = contentPane.getComponent(i).getBounds();
    			  preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
    			  preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
    		  }
    		  Insets insets = contentPane.getInsets();
    		  preferredSize.width += insets.right;
    		  preferredSize.height += insets.bottom;
    		  contentPane.setMinimumSize(preferredSize);
    		  contentPane.setPreferredSize(preferredSize);
    	  }
    	  pack();
    	  setLocationRelativeTo(getOwner());
          // JFormDesigner - End of component initialization  //GEN-END:initComponents
          }
       
       // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
       // Generated using JFormDesigner non-commercial license
       private JMenuBar menuBar1;
       private JMenu menu1;
       private JMenuItem menuItem1;
       private JLabel label1;
       private JLabel label2;
       private JTextField textField1;
       private JTextField textField2;
       private JLabel label3;
       private JLabel label4;
       private JTextField textField3;
       private JTextField textField4;
       private JButton button1;
       private JLabel label5;
       private JTextField textField5;
       private JTextField textField6;
       private JTextField textField7;
       private JSeparator separator1;
       private JLabel label6;
       private JLabel label7;
       private JLabel label8;
       private JLabel label9;
       private JLabel label10;
       private JLabel label12;
       private JLabel label13;
       private JTextField textField8;
       private JTextField textField9;
       private JTextField textField10;
       private JTextField textField12;
       private JTextField textField13;
       private JTextField textField14;
       private JScrollPane scrollPane1;
       private JTextPane textPane1;
       private JButton button2;
       private JLabel label11;
       private JRadioButton radioButton1;
       private JRadioButton radioButton2;
       // JFormDesigner - End of variables declaration  //GEN-END:variables
       }

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: menu item event

    Try to use better the ActionListener!
    Java Code:
    				menuItem1.addActionListener(new ActionListener() {
    					@Override
    					public void actionPerformed(ActionEvent e) {
    						menuItem1MouseClicked(e);
    					}
    				});
    +
    Java Code:
    private void menuItem1MouseClicked(ActionEvent e) {
    http://download.oracle.com/javase/tu...enu.html#event

  5. #5
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: menu item event

    Now it gives the following error:
    Userinfo.java:109: method does not override or implement a method from a supertype
    @Override
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.

    And the code now looks like this:
    Java Code:
     menuItem1.setText("Exit");
                   menuItem1.addMouseListener(
                          new MouseAdapter() {
                             @Override
                             private void  menuItem1MouseClicked(ActionEvent e) {
                               menuItem1MouseClicked(e);
                            }
                         });
    All I want to to is to perform an action when an item is clicked in the menu. Thank you for the help so far.x

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: menu item event

    Compare your codesnippet with my one :)
    Replace the MouseListener with ActionListener (see my code!!)
    Fubarable likes this.

  7. #7
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: menu item event

    hahahahaha Thank you it works now...!!! thanx xxx

Similar Threads

  1. How can you add an action listener to a menu item?
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 08-24-2011, 08:55 PM
  2. Error when clicking a menu item JSF + Richfaces
    By pepelegal in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-25-2011, 12:03 AM
  3. Menu item activation in struts2+tiles project
    By aruna.hcu in forum Web Frameworks
    Replies: 0
    Last Post: 01-26-2010, 11:43 PM
  4. Menu item not working properly for mouse events
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 09:56 AM
  5. Checking toggle state of a menu item
    By xsive in forum SWT / JFace
    Replies: 1
    Last Post: 09-22-2008, 03:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •