This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Display and Edit JTable after Deserialization  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a Jtable as an "excel like" sheet for work use to build a schedule for my fellow employees. I'm serializing the model (defaulttablemodel) and need to have the ability to open, display and edit it (numerous times). I think(?) it's opening (being read) but I can't display it. Here's partial code listing (thanks in hopeful advance):

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
File currentDirectory = new File(".");
JFileChooser jFileChooser = new JFileChooser(currentDirectory);
jFileChooser.setFileFilter(new txtFileFilter());
jFileChooser.setFileFilter(new datFileFilter());

if(jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
// Deserialize from a file
File file = new File("butttestTable.dat");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
// Deserialize the object
//javax.swing.JButton button = (javax.swing.JButton) in.readObject();
DefaultTableModel readModel= (DefaultTableModel)in.readObject();
JTable jTableRead = new JTable(readModel);

jTableRead.getTableHeader().setReorderingAllowed(f alse);
JScrollPane jScrollPaneRead = new JScrollPane();


catch (ClassNotFoundException e) {
catch (IOException e) {
//code to handle choosed file here.
Posts: 24089
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand what you are trying to do there. You are creating some new Swing components and filling them with the contents of the table model you just read in, but you don't ever display those components. I think that would be why you don't see the data.

Why don't you just put the table model into an existing JTable which is already displayed?
John Grotteland
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks for the advice. I tried your suggestion and received an error message to the effect that I was trying to load a JTable with a null model.

A gentleman from '' suggested the following:

"You've created a JTable with your model, and you've added it to a JScrollPane, but you haven't added the JScrollPane to a contentpane of any panel.

You could created a JFrame, and then add the JScrollPane to it's contentpane...

JFrame frame = new JFrame();
frame.getContentPane().add(jScrollPaneRead );

I tried his approach and got a blank frame.

So, I checked the code that was serializing the table model and realized that I was remiss in not taking advantage of the try/catch. Which yielded the following error message:

Exception during serialization: java.lang.reflect.Constructor

So it appears that I have something in my JTable/model that is not serializable.

Thanks again for you help; now it's time for more head scratchin'.
Posts: 62875
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got a Constructor object in your display in the first place?

Thank you for telling us you posted on another site; I presume you have told them you are posting here, too?
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!