aspose file tools*
The moose likes Beginning Java and the fly likes Weird actionPerformed method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Weird actionPerformed method. " Watch "Weird actionPerformed method. " New topic
Author

Weird actionPerformed method.

Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

Working through books and tutorials, led me to believe that I can set up listeners to my widgets, and then have one actionPerformed method which will test for those. I have set up such a method, and am getting some weird behaviour. The save button opens a save JFileChooser and saves text to a file, and the open button, opens an open JFileChooser. However, the open button's JFIleChooser, doesn't read text to a window. I've even put a bit of debugging code such as:



which doesn't appear when the Open button is called. I also changed the open button to a save button, which threw up an open button, despite my change in the code! There is also a Help->About window, which is called fine, but again, no

works.

What should I be looking out for? The code runs fine, until I try and implement the open in actionPeformed to start reading text from a file. That's when I noticed the weirdness.

On to the main code chunks

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Common beginner error: Don't use the == operator to compare the contents of two String objects. It actually compares the references, i.e. it asks whether the two Strings are the same object. But different String objects can have the same contents, so that isn't what you want. Call the equals() method of a String if you want to find whether its contents are the same as the contents of another String.
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

I did try the .equals() method, but it still didn't work - will try it again.
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

Paul Clapham wrote:Common beginner error: Don't use the == operator to compare the contents of two String objects. It actually compares the references, i.e. it asks whether the two Strings are the same object. But different String objects can have the same contents, so that isn't what you want. Call the equals() method of a String if you want to find whether its contents are the same as the contents of another String.


I still get the same results. Saved called shows up, but none of the others.

I have if in the actionPerformed with the equivalent of:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

And when you debugged the code, what was the value of e.getActionCommand() and what code was run that you think shouldn't have been run, or vice versa?
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

@Paul Clapham, I guess I'm confused about what code has run as none of the expected code shows in the console (e.g. System.out.println("open called. ");) - however, JFileChoosers do open as does the JOptionPane which shows the about. Without System.out.println showing to console, I'm finding it difficult to debug.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Well, if you don't have a console then you're going to have to find some other way of debugging that code. What tool are you using which doesn't provide a view of the console?
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

I do have a console, as the Save section of the actionPerformed reports that save is called - that's why this is weird behaviour.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Okay. So if you have lines 1, 2, and 3 of code in sequence and your debugging shows you that line 1 is not executed but line 3 is executed, then you aren't looking at the right source code. Make sure the compiled code you are running does actually correspond to the source code you are looking at.
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

OK. This must be an eclipse weirdness, as I moved all my .java files to a temporary directory - recompiled and it's working as it should.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird actionPerformed method.