Results 1 to 3 of 3
Thread: trouble with actionPerformed
- 12-25-2008, 07:59 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
trouble with actionPerformed
I'm still working on my notepad program and I've just been working on my "Open" button, which opens up a JFileChooser. I looked at this tutorial online and it said that I could write it into the actionPerformed method of my class like so (this code isn't from the tutorial, this is what I wrote based on what I learned from it... it is incomplete because right now I'm just focusing on getting the actual window to open):
However, when I use this code and click the open button, nothing happens. Keep in mind that I didn't make an inner class or anything like that, it's just the actionPerformed method of the overall class (Which I made implement ActionListener just for this).Java Code:public void actionPerformed(ActionEvent e) { if(e.getSource() == bOpen) { JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(null); fc.requestFocusInWindow(); fc.grabFocus(); if(returnVal == JFileChooser.APPROVE_OPTION) { try { File f = fc.getSelectedFile(); Scanner scan = new Scanner(f); String newText; } catch(FileNotFoundException exception) { } } } }
The weird thing is though, when I use it as an inner class and add it as an actionListener to the button itself, it works just fine. This is the successful code I used:
As you can see, it's the exact same thing as the above, only I attached it to the button instead of using the if statement with the e.getSource().Java Code:bOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(null); fc.requestFocusInWindow(); fc.grabFocus(); if(returnVal == JFileChooser.APPROVE_OPTION) { try { File f = fc.getSelectedFile(); Scanner scan = new Scanner(f); String newText; } catch(FileNotFoundException exception) { } } } });
If anybody could explain why the second way worked and the first didn't, I'd really appreciate it.Last edited by diggitydoggz; 12-25-2008 at 08:04 PM.
-
Making an actionPerformed is fine, but did you
1) have your class implement the ActionListener interface, and
2) add "this" as the button's ActionListener, i.e.,
Having said that, I am definitely of the camp that believes that using your main class as the actionlistener object is a bad idea. This can lead to creation of a "switchboard" actionPerformed method where too many buttons are relying on a single actionPerformed. Much better is to either create an anonymous inner class as you've done above, or use a private inner class or if complex enough a standalone separate ActionListener class.Java Code:myButton.addActionListener(this);
- 12-26-2008, 02:18 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
returning String from actionPerformed
By hardcore_teddy in forum New To JavaReplies: 2Last Post: 05-17-2008, 05:32 AM -
Issue with Buttons and ActionPerformed
By Deathmonger in forum Advanced JavaReplies: 1Last Post: 04-17-2008, 08:47 AM -
Help with actionPerformed Statements
By wco5002 in forum New To JavaReplies: 8Last Post: 03-26-2008, 04:02 AM -
actionPerformed problem
By tomitzel in forum New To JavaReplies: 1Last Post: 01-08-2008, 06:10 PM -
Problems with jButton ActionPerformed
By susan in forum AWT / SwingReplies: 3Last Post: 08-07-2007, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks