• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a File Within an ActionListener

 
Zake Bake
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
As far as the console suggests, there are no exceptions occurring.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 52513
118
  • Mark post as helpful
  • send pies
  • 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.
     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!