Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Weird actionPerformed method.

 
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did try the .equals() method, but it still didn't work - will try it again.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic