aspose file tools
The moose likes Beginning Java and the fly likes Writing to a File Within an ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Writing to a File Within an ActionListener" Watch "Writing to a File Within an ActionListener" New topic
Forums: GUI java
Author

Writing to a File Within an ActionListener

Zake Bake
Greenhorn

Joined: Nov 19, 2012
Posts: 2
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

Joined: Jan 28, 2003
Posts: 3036
    
    4

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.


Steve
Zake Bake
Greenhorn

Joined: Nov 19, 2012
Posts: 2
As far as the console suggests, there are no exceptions occurring.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3036
    
    4

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
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
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

    Joined: Mar 17, 2011
    Posts: 4756
        
        7

    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


    Isn't it funny how there's always time and money enough to do it WRONG?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Writing to a File Within an ActionListener
     
    Similar Threads
    Problem with Updating ProgressBar
    Waiting for input from PipedInputStream....thread yield?
    Blanking out a button if they do not have enough funds in their bank.
    Unable to update my data.. why is that so..?
    Java Server/Client String comunication over socket