![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
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 concisenessI 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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
This may help.
http://java.sun.com/docs/books/tutor...ents/menu.html
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |