Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   event handeling assistance (http://www.programmingforums.org/showthread.php?t=13052)

sackarias Apr 24th, 2007 2:02 PM

event handeling assistance
 
I have a menu bar with some menus, file->open, new, save..etc. But I also have a tool bar that I placed buttons on for open, new, save..etc.

My problem is I'm not sure how to have these buttons fire the same events as those from the menu bar. I know I can do getActionCommand() and see if it equals the string name of the button, but I have the buttons set as images so I never assigned a string name to them... is there a way I could over ride getActionCommand() to say check for the name of the button as opposed to the string so I can throw an 'or' and see if it was either triggered from the file menu OR the button I have specified located on the tool bar.

:


    public void actionPerformed(ActionEvent e)
    { 
      String command = e.getActionCommand();

      if( command.equals("Exit") )
      {
          System.out.println("Exiting");
      }
      // check which menu alternative the user has chosen

        //*************************************************
        // File JMenu Items
        //*************************************************

      // New

      //I have a button for new... jbtNew, which I want to attach to this
      //code, same would go for the 5 or so other buttons that I put on
      //the tool bar.
      if(command.equals("New") ) 
      {     
        saveOld();
        area.setText("");  // empty the text area
        currentFile = "noName";
        setTitle(currentFile);
        changed = false;
      }

      //there are more...omitted for conciseness


I originally thought I had gotten around this problem because I added anonymous listeners, however, with my "open" event I use a JFileChooser and for some reason if I make an anonymous listener with any code pertaining to a JFileChooser the compiler yells..

:

jbtOpen.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
              saveOld();
              JFileChooser jFileChooser2 = new JFileChooser(new File("."));
             
              if(jFileChooser2.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
              {
                loadFile(jFileChooser.getSelectedFile());
              }
          }
      });


I hope my problem has been made clear. Thanks a ton to whoever helps me out. Very much appreciated.

mrynit Apr 25th, 2007 1:11 AM

This may help.
http://java.sun.com/docs/books/tutor...ents/menu.html

sackarias Apr 25th, 2007 3:17 PM

Good call. I figured it out.

I moved the code with JFileChooser into a function, then whenever the button was selected, I just added an anonymous listener that called the function, it got me around the "you can't have a JFileChooser here" error.

Thanks a lot mrynit.

mrynit Apr 26th, 2007 3:08 AM

I'm in java GUI now and we did that example as part of our lab. If you need more GUI help I can provide some data.


All times are GMT -5. The time now is 2:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC