• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struggling with this table of Objects data

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah thanks, the main issue I am concerned with first is the table of objects
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any other takers?
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no one?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic