This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to open a file using a JButton?  RSS feed

 
Greenhorn
Posts: 13
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried saving it to my desktop and using the Desktop.getDesktop method but it still won't open. This is the code I have so far:
 
Ranch Hand
Posts: 81
1
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You miss to add a close bracket on line 68.
The correct should be like this
 
Marshal
Posts: 59717
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you could do with a JFileChooser (see documentation. That enables you to choose a file to save to or to open.
 
Blaine Jackson
Greenhorn
Posts: 13
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 59717
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Decide what size your file is, and how you are going to display it, before you write the GUI. If you haven't decided what you want to do, you can't work out how to do it, and the worst way to work out that sort of thing is by writing lots of code about whose intent you are uncertain.

I see that you haven't taken notice of the suggestions I made before. Your code style is barely improved from yesterday's offering.
 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Blaine,

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.

Regards
Alin
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!