• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing to a File Within an ActionListener

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, when I press a certain button, my file is supposed to "safely" update. The code I have for that is:




And when I hit a JButton is when this file needs to be updated. The code for the ActionListener is here:



Now, I have tested my method to write update the file in another class and it seems to work, but once I wrap it in the try, catch, which is apparently required for the ActionListener, it doesn't seem to do anything.

Does anyone have any suggestions or hints so that I can implement my method when the JButton is pressed?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you run the application from the command line or look at a console where errors may be visible? The most likely problem is an exception is happening, but you need to be able to view the error.
 
Zake Bake
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as the console suggests, there are no exceptions occurring.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then there is not enough information provided. Create an SSCCE (<= link). Make sure it shows only what is required to reproduce the problem, is small enough so we don't have to trudge through tons of code to help you, and can be compiled and run so we can see the issue.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like too much code for an ActionListener. It also looks as though you have a book which teaches that abomination against object‑oriented programming, addActionListener(this);
I suggest once you have got the method working from the command line, using a call like this, you create a Listener classNote:-
  • 1: I don’t like main methods more than 1 statement long, but I shall make an exception and deprecate the method from further use.
  • 2: I have assumed you are overloading your method so you pass the details of the files, data to write, etc as arguments.
  • 3: You would do well to try out things like file choosers for finding files before writing the Listener.
  • 4: There are several ways to write Listener classes, which you can find about by searching in our GUIs forum. This is a rough‑and‑ready example of one way to do it.
  • I can try duplicating this thread into the GUIs forum; that might get you more attention.
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Zake Bake wrote:Does anyone have any suggestions or hints...


    Yes: Don't put extremely long lines in your code.
    For one thing, it's bad practise; and here specifically, it screws up the windowing and makes your thread hard to read. I've broken yours up this time, but please re-read the UseCodeTags (←click) page thoroughly.

    Thanks.

    Winston
     
    Whatever you say buddy! And I believe this tiny ad too:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic