File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Struggling with this table of Objects data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Struggling with this table of Objects data" Watch "Struggling with this table of Objects data" New topic
Author

Struggling with this table of Objects data

Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Hi All,

So I have been reading the JTable tutorials but I am struggling.

Here is the general Idea:

I have a Book class that has the fields:



I have written code to read in a list of books from a csv file and have serialised the class and written the book objects into a txt file. I have static methods to save and load the objects.

now what I am trying to do:

Display the list of books into a Table, the table must allow me to have the isRead column as a check box and be editable, but I need it to affect the actual status of the object to change the status from not read to read.
On changing the status to read I have made in the set status method that it must populate the objects dateStamp field with a dateStamp, I want this to then reflect in the table straight away.

Lastly I want the app to be able to then save out all the objects again into the file when a user hits the close button.

Please advise how to do this.

Thanking you.

Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Ok I have found out how to add a hook to call my save method when a user closes, I added in a System.out.prinltn and it fires when I close so it does appear to work.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
There is something wrong with a Book class which maintains a list of books. That belongs in a different class, I would think. Also there is something wrong when you start writing static.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Campbell Ritchie wrote:There is something wrong with a Book class which maintains a list of books. That belongs in a different class, I would think. Also there is something wrong when you start writing static.


so should I rather create a separate class just to hold this books in a set?

also apologies what do you mean there is something wrong when I start writing static? I made the read and save mothods static so they can be called without requiring an instance of the class.

thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
I am always suspicious of things static. Your read and save methods do not apply to a book; they apply to a library. So maybe you should write a Library class to encapsulate that set of books.
Make the read and save methods non-static, unless you are sure you cannot have several libraries each with a different set of books.
Why are you using a linked set rather than a list?
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Campbell Ritchie wrote:I am always suspicious of things static. Your read and save methods do not apply to a book; they apply to a library. So maybe you should write a Library class to encapsulate that set of books.
Make the read and save methods non-static, unless you are sure you cannot have several libraries each with a different set of books.
Why are you using a linked set rather than a list?


there will only be one single list of books thus why I originally never wrote a separate Library class.

I am using a Set rather as I want to ensure no duplicates. is there any other particular type of set that will be better to use in this case. would using a HashSet or TreeSet be better? or should I just use an ArrayList?

Again apologies I am still beginner
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Yes, you have got a good explanation for using a set I don’t know whether a hash set or a tree set would be better. I suggest you go through the collections part of the Java Tutorials, which section you can read in less than an hour. There you will find that hash sets, tree sets, linked sets and array lists all do different things. You should also consider which of them is best suited to populating a JTable. I could never get tables to work well myself, and I can’t remember such details, but I can remember that if you go back to the tutorials, there is a section about tables.
If you are creating library objects from a file, one valid way is to use a method like thisYou have a getInstance method, which allows you to create as many Library instances as you have files you can create them from. That is called a factory method, and it uses a private constructor, so there is no way to create a Library object from outside that method. Nor can you populate the library from elsewhere, though a public addBook(Book b) method would probably be useful. Note the use of single‑letter variable names is pertty naff, but it’s quicker for me to write.
Note I have got rid of all the static bits. Well, nearly. Factory methods must be available before an instance has been created, so they usually have a good reason for being static.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
yeah thanks, the main issue I am concerned with first is the table of objects
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You’re welcome

I presume you noticed the mistake I made, missing out the return type for that factory method. The compiler would have noticed it soon enough.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
any other takers?
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
no one?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Lastly I want the app to be able to then save out all the objects again into the file when a user hits the close button.

much better to save whenever a change is made - computer crash, electricity blackout, etc etc

look up TableModelListener
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Michael Dunn wrote:> Lastly I want the app to be able to then save out all the objects again into the file when a user hits the close button.

much better to save whenever a change is made - computer crash, electricity blackout, etc etc

look up TableModelListener


very good point thank you.

I see it only has one method to implement, which I would use to save my Set and make the table refresh?

If possible can you explain slightly how I could go about implementing this?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> If possible can you explain slightly how I could go about implementing this?

I could, but you'd get better info from the source - bookmark this page (will answer many of your swing queries)

http://docs.oracle.com/javase/tutorial/uiswing/TOC.html

scroll down to writing event listeners/how to write a TableModelListener
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Ok I eventually came right.

I extended default table model, I override the isCellEditable method to make the columns i needed editable. Then I override teh getClass method and returned boolean for the check boxes. I also aded in a JComboBox to have a drop down for the rating column. then I used the setValueAt method to make the actual changes, in this method I called my saveBooks method that made sure the changes were written to the serialized file in case of power outage etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struggling with this table of Objects data
 
Similar Threads
DownloadManager 2009
object cloning
Practical way to write persistence methods / DAO
Panel program(help plzzzzzzzzzz)
returning user defined objects