Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 24th, 2007, 1:02 PM   #1
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
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.
sackarias is offline   Reply With Quote
Old Apr 25th, 2007, 12:11 AM   #2
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
This may help.
http://java.sun.com/docs/books/tutor...ents/menu.html
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Apr 25th, 2007, 2:17 PM   #3
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
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.
sackarias is offline   Reply With Quote
Old Apr 26th, 2007, 2:08 AM   #4
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Click event triggered unexpectedly ParadiseIsle Visual Basic 1 Dec 16th, 2006 7:25 PM
School's Out Bash - LAN Event in Ash Flat, Arkansas bigguy Coder's Corner Lounge 21 Apr 24th, 2006 4:12 PM
callback functions aloksave C++ 16 Nov 21st, 2005 9:10 AM
question: usage of delagates and custom events melbolt C# 1 Oct 3rd, 2005 7:17 PM
Help! I have a problem thomas55 C++ 1 Apr 24th, 2005 1:16 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:04 PM.

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