Results 1 to 2 of 2
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default Make a menybutton (action)listen!

    hi guys. im trying out the GUI function in java and im kind of stuck. im not sure how to make the meny-button to open up a new window. its the actionlistener thats the problem id say... anyway, here's my code
    Java Code:
    import java.awt.*;	
    import java.awt.event.*;
    import javax.swing.*;
    
    	public class Kund extends JFrame{
    		private JMenuBar huvudmeny;
    		private JMenu Profiler, Meny;
    		private JMenuItem LäggTillKund, TagBortKund;
    		
    		public Kund(){
    			huvudmeny = new JMenuBar();
    			Profiler = new JMenu("Profiler");
    			Meny = new JMenu("Meny");
    			LäggTillKund = new JMenuItem("Lägg till kund");
    			TagBortKund = new JMenuItem("Tag bort kund");
    			LäggTillKund.addActionListener(new Lyssnare());
    			TagBortKund.addActionListener(new Lyssnare());
    			Profiler.add(LäggTillKund);
    			Profiler.add(TagBortKund);
    			Meny.add(Profiler);
    			huvudmeny.add(Meny);
    			
    			setJMenuBar(huvudmeny);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    			setSize(500, 400);
    			setLocationRelativeTo(null);
    			setVisible(true);
    			}
    		public class Lyssnare implements ActionListener{
    			public void ActionPerformed(ActionEvent e){
    			
    
    			}
    		}
    	}
    Java Code:
    public class Mainmetod {
    
    	public static void main (String[] args){
    		new Kund();
    		
    	}
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    use e.getSource() inside your actionPerformed method to find out which object triggered the event then use appropiate code associated with that objects function.

    for e.g. say if TagBortKund was meant to close the JFrame :

    Java Code:
    if(e.getSource().equals(TagBortKund)){
         dispose();
    }

Similar Threads

  1. Replies: 3
    Last Post: 08-19-2010, 09:27 AM
  2. serversocket listen connection
    By gisler in forum Networking
    Replies: 1
    Last Post: 12-17-2009, 03:57 AM
  3. Listen for changes in a JTextField
    By matzahboy in forum AWT / Swing
    Replies: 5
    Last Post: 10-13-2009, 05:15 PM
  4. Replies: 2
    Last Post: 09-29-2009, 03:11 AM
  5. Cannot Listen TAB key
    By sh4dyPT in forum AWT / Swing
    Replies: 5
    Last Post: 07-16-2009, 02:45 PM

Posting Permissions

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