Results 1 to 4 of 4
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default newbi. ActionEvent. Close on click

    Hello I have searched everywere for information so i can close my app with my Arkiv-Exit Menu. i Need some kind of if statement in my public void actionPerformed(ActionEvent e) so that i can save with save and exit with exit..

    Best Reagards / ocean

    Here is my code.
    Java Code:
    import java.awt.event.*;
     import javax.swing.*;
     
     
       public class gui implements ActionListener  {
     	  
     	  
     	  JTextArea output;
     	  JScrollPane scrollPane;
     	  String newline = "\n";	  
     	 
     	  public JMenuBar createMenuBar(){
     		  
     		  JMenuBar aMenuBar;
     		  JMenu aMenu;
     		  JMenuItem aMenuItem;
     		  
     		  	aMenuBar = new JMenuBar();
     		  	
     		  	aMenu = new JMenu("Arkiv");
     		  	aMenuBar.add(aMenu);
     
     		  			  	
     		  	aMenuItem = new JMenuItem("exit");
     		  	aMenuItem.addActionListener(this);
     		   	aMenu.add(aMenuItem);
                        
                                  
                            aMenuItem = new JMenuItem("save");
     		  	aMenuItem.addActionListener(this);
     		   	aMenu.add(aMenuItem);
     		   return aMenuBar;
     	 }
    
     	  public void actionPerformed(ActionEvent e) {
     		 	System.exit(0);
     	  }
     	  
     	  
     	  private static void createAndShowGUI() {
     		  JFrame aFrame = new JFrame("oceanSQL");
     		  aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     		   
     		  
     		  gui aDemo = new gui();
     		  aFrame.setJMenuBar(aDemo.createMenuBar());
     		  
     		  aFrame.setSize(300, 300);
     		  aFrame.setVisible(true);
     		 
     		 }
     	 
     	  public static void main( String[] args){
     		  javax.swing.SwingUtilities.invokeLater(new Runnable(){
     		  public void run() {
     			  createAndShowGUI();
           }
       });
       }
     }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    If you look at the ActionEvent class you'll notice there's a method called getSource(). This is used to tell which object threw the ActionEvent.

    Or you could just add separate listeners to the different Menu Items.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The ActionListener's ActionEvent parameter, here called "e" holds useful information that you can use. For one, it holds a String called the actionCommand that will return the text of the menu that tripped the action listener. For instance if you actionPerformed method looked like so:
    Java Code:
      public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        System.out.println(e.getActionCommand());
      }
    You would see which menu causd the actionPerformed to run. If your code then looked like so:
    Java Code:
      public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if (e.getActionCommand().equals(.....)) {
          
        } else if (.....) {
          
        }
      }
    You could use this information.

    Having said this, I strongly recommend that you don't make your class implement ActionListener and pass "this" as your action listener. Instead I'd create separate anonymous classes for each menu item:
    Java Code:
        aMenuItem = new JMenuItem("save");
        //aMenuItem.addActionListener(this);
        aMenuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            // TODO finish this
            
          }
        });
        aMenu.add(aMenuItem);

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Fubarable Thank you so very much. You solved my question and now it works fine. Here is the cod that works.

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    
      public class gui implements ActionListener  {
    	  
    	  
    	  JTextArea output;
    	  JScrollPane scrollPane;
    	  String newline = "\n";	  
    	 
    	  public JMenuBar createMenuBar(){
              // Create and Add Menus
    	 JMenuBar aMenuBar;
    	 JMenu aMenu;
    	 JMenuItem aMenuItem;
    		  
    	aMenuBar = new JMenuBar();
    		  	
    	aMenu = new JMenu("Arkiv");
    	aMenu.addActionListener(this);
    	aMenuBar.add(aMenu);
    		  		
    	aMenuItem = new JMenuItem("exit");
    	aMenuItem.addActionListener(this);
    	aMenu.add(aMenuItem);
    		   	
    	aMenuItem = new JMenuItem("save");
    	aMenuItem.addActionListener(this);
    	aMenu.add(aMenuItem);
    				  	
    	return aMenuBar;
    	 
      }
    	public void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals("exit")){
            System.exit(0); }
    		  
    	else if (e.getActionCommand().equals("save"));{
    	System.out.println(e); }
     }
    	
    	  private static void createAndShowGUI() {
             JFrame aFrame = new JFrame("oceanSQL");
    	 aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		   
    		  
    	 gui aDemo = new gui();
    	 aFrame.setJMenuBar(aDemo.createMenuBar());
    		  
    	 aFrame.setSize(300, 300);
    	 aFrame.setVisible(true);
    		 
     }
    	 
    	  public static void main( String[] args){
    	  javax.swing.SwingUtilities.invokeLater(new Runnable(){
    	  public void run() {
    	  createAndShowGUI();
          }
    	});
       }
    }


    EDIT 2009-09-19 22:52

    Thank you for the advice on aMenuItem.addActionListener(new ActionListener() Here is the code. I have the classes DB and TB in a seperate file. Please let me know if there is other things i can write in a better way.

    Java Code:
    public class gui extends cdata {
    	  
    	  JTextArea output;
    	  JScrollPane scrollPane;
    	  String newline = "\n";	  
    	 
    	  public JMenuBar createMenuBar (){
    		  // Create and Add Menus
    		  JMenuBar aMenuBar;
    		  JMenu aMenu;
    		  JMenuItem aMenuItem;
    		  
    		  aMenuBar = new JMenuBar();
    		  	
    		  aMenu = new JMenu("Arkiv");
    		  aMenuBar.add(aMenu);
    	          aMenuItem = new JMenuItem("Exit");
    		   	
    	          aMenuItem.addActionListener(new ActionListener() {
    		  public void actionPerformed(ActionEvent e) {
    	          if (e.getActionCommand().equals("Exit")){
                      System.exit(0);}
    	}
    });   
    	        aMenu.add(aMenuItem);
    		  	
    		aMenu = new JMenu("Sql");
    		aMenuBar.add(aMenu);
    		aMenuItem = new JMenuItem("New DB");
    		   	
    		aMenuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
    	        if (e.getActionCommand().equals("New DB")){
    	        showNewWindow();}
      }
    });   
    		   
    		aMenu.add(aMenuItem);
                    aMenuItem = new JMenuItem("New TB");
    		   	
    		aMenuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
    	        if (e.getActionCommand().equals("New TB")){
    	        showNewWindow1();}
        }
    });   
    		  	
    		aMenu.add(aMenuItem);
    		return aMenuBar;		  
    }
    	  
    	  private static void createAndShowGUI() {
    		 JFrame aFrame = new JFrame("oceanSQL");
    		 aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		 gui aDemo = new gui();
    		 aFrame.setJMenuBar(aDemo.createMenuBar());
    		  
    		 aFrame.setSize(300, 300);
    		 aFrame.setVisible(true);
    		 
    }
    	  public static void main( String[] args){
    		 javax.swing.SwingUtilities.invokeLater(new Runnable(){
    		 public void run() {
    		 createAndShowGUI();
      }
    });
    }
    }
    Last edited by ocean; 09-19-2009 at 10:50 PM. Reason: Tryed a new way to write the code.

Similar Threads

  1. Check if ctrl is pressed in an ActionEvent
    By poet in forum AWT / Swing
    Replies: 2
    Last Post: 09-10-2009, 08:50 AM
  2. Problems with ActionEvent for JButton
    By TrueBear in forum AWT / Swing
    Replies: 1
    Last Post: 08-24-2009, 04:26 PM
  3. Replies: 0
    Last Post: 05-23-2009, 04:26 PM
  4. [SOLVED] Actionevent problem
    By Cymro in forum New To Java
    Replies: 3
    Last Post: 04-04-2008, 07:11 AM
  5. ActionEvent example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:00 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
  •