Blaine Jackson wrote:I've tried saving it to my desktop and using the Desktop.getDesktop . . .
No, that won't help you. You need the usual way to open a file; I suggest you write a method on Jshell or a little standalone class which simply opens the file. Then enhance it to read or write something.
Don't extend JFrame. Don't make your class implement action listener. And if you the == operator on Strings, it is only a matter of time before it goes horribly wrong.
If you are writing a text file, avoid all those line separators. Put all the data onto one line. Don't write four separate lines in four separate statements: make it all into one statement writing one line at a time. That will give faster performance. There are other places where your code could be improved, particularly about closing writers, but I shall leave that for later posts.
Your variable names are a sure‑fire recipe for confusion. If you come back in six months, you won't know what s means; if you called it saveButton its meaning would be crystal‑clear. None of your other variable names is much better.
Well done Randy Too for finding the missing } That shows how much trouble you can store up for yourself if your indentation is inconsistent, which yours is. Look at lines 54‑55 and lines 64‑65. Since you are obviously not using an IDE, learn to write backwards, as I call it.
I managed to get the file to print to the jframe but I can only see the last line. is there a way I can make the JTextField bigger or something?
By the way, I'm using System.lineSeparator to make the file more readable.
posted 6 months ago
Forget all about frames, text fields, and other GUI components, until you can get this sort of thing to execute correctly:-Only when you have got the file opened properly can you put a GUI atop your application.
I have refactored a little bit your solution and one thing I wanted to prove with this refactoring is the separation of concerns. To be more specific the creation of the GUI should be separated from the file handling and here is the solution that I have come up with.
I have also added a text area as I did not know where to print the the file content.
In the above example along with the text area that I have added I also added the private static String sep = java.io.File.separator; this is to make your program platform independent.
I believe that this is a good example because it will let you focus on file operations and not how the GUI is constructed. And also will allow you to find grouped together the creation of the elements as well as how the mainTab is created and also what action listener you have.
If you have any questions or something is not clear please let me know.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database