Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with File I/O within my GUI  RSS feed

 
Andrew Broda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how to add and read from files through my GUI. I get the message saying things are saved, however, they are not saving!

Collection Panel


Item Panel


Item


Main GUI
 
Knute Snortum
Sheriff
Posts: 3546
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Item implements Serializable but doesn't have a serial UID.  This could be a problem.  Try adding something like:
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You have empty catch blocks, so you'll never know if there's been an error.
You also don't close your output stream, which means it's quite possible nothing gets written.
It should be closed in a finally block, or you should be using a try-with-resources:
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also not be doing file IO from a GUI. You should have a separate class which does the IO independently of the GUI. You can then test it and later add methods to the GUI to use that code.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second Campbell's comment.

It doesn't take 600 lines of code to test I/O. Test the I/O separately.

This is a general rule for writing code. You test small pieces of code to get them working then you add that functionality into the GUI.

Learn to simplify the code when learning something new. You NEVER need to write hundreds of lines of code to test/learn a concept.

 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Andrew Broda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my current GUI looks as I want it to. My issue that I am having is that not my cards not refresh once an Item is added(only gotten as far as that, I'll apply to the other classes as I get this one working). The display doesnt show my new item until I exit and reopen my GUI.
The file writer writes to the binary dat file, and says it does, however if I go back to adding a new item, the old item is still in the fields, and it adds whatever I write over it in an increment of 1, so as long as my GUI stays open. Please help me understand what is going on here, and how I can fix it!

THE MAIN GUI


THE DISPLAY COLLECTION PANEL(READS FROM A FILE TO DISPLAY IN ONE OF THE PANELS AS WELL



THE ITEM PANEL


MY ITEM CLASS

 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The display doesnt show my new item until I exit and reopen my GUI.
...
if I go back to adding a new item, the old item is still in the fields, and it adds whatever I write over it in an increment of 1


You've explained what the program does.  Can you also describe what you want it to do differently?

Note there is more code posted than most people are interested in copy for testing.  Can you make a http://sscce.org/ for the problem?
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:. . . Note there is more code posted than most people are interested in copy for testing.  . . .
Agree. As I said in your other thread, you should not be doing any File IO from a GUI. You should create classes which do the “business logic”, and those classes should be completely separate from the GUI. You should have them working nicely before you even think about the GUI. You should be able to write things like:-Once you have got that working, you can add the GUI. Also, you can write 40 lines of code and it is much easier to find errors in 40 lines than in the amount of code you provided.

I think this question is closely related to your other thread, so I shall join the two into one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!