• 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

updating jTable's display

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for displaying this jTable, i call this method:

my question:
every time i insert a record and want the jTable to immediately reflect this insert do i have to go all the way calling inicializaTableActivo()?
or there is a better way?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is not need to create a custom TableModel. You can use the DefaultTableModel to hold the data. Then whenever you update the database you can just invoke the addRow(...) or insertRow(...) method of the DefaultTableModel.

Or you can duplication the addRow(...) method of the DefaultTableModel and implement the logic in your custom model. Look at the DefaultTableModel source code for an example of how this might be done.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
walking very slowly. here's what i'v done:
inside the method that saves data to database, i created a vector, populated it with data, called addRow in this model passing the vector and set the model to my table:
but now my table doesnt show up (even after setVisible(true))
what have i to do in order to make it visible?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you hide it or remove it from the GUI, you wouldn't need to do anything. The change of table model would cause a full repaint.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the meanwhile i tried second suggestion:


but with same result - cant see the table
at first i thought there would be some spaghetti confusion among several models - the default and the custom one, responsilble for the disapearing, but, as it is, i dont get it
i cross checked the code for the insert button, but it simply calls the code i already posted - i'm puzzled
well, i have listeners, sorters and renderers for the table, but the last two stoped being invoked when i disposed of the method i first posted
as to listeners, i dont even click the table, since she has gone...
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
things are getting uglier
(anyway, its getting closer to xmas dinner time - so i''l be here only much later, or even tomorrow)

ok, i changed a bit my code:

and inside custom tableModel too:

and now i get a ClassCastException:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
at gui.TableModelActivo.getValueAt(TableModelActivo.java:56)


here:


a merry Chrstmas for you guys
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rowData is a Vector<String> but you treat it as a Collection<? extends Object[]>.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yet again, you haven't learned to simplify the problem. Instead of learning how to use the DefaultTableModel by creating a SSCCE you are trying to change the code in your program without understanding what you are doing.

I made two suggestions:

a) forget about creating a custom model and use the DefaultTableModel to hold the data.
b) use the addRow(...) method

If these concepts are new to you then you can search the web. Using newly introduced keywords like "defaulttablemodel" and "addrow", to find existing examples. You then use the examples to create your SSCCE.

Instead you again post a few random lines of code including:



I have no idea what you are doing. I don't know why you are creating a second TableModel or using the setModel() method. I didn't make those suggestions anywhere.

The problem is you are trying to fit the code into your existing program rather that start from the beginning and learn how to use the DefaultTableModel properly.

This is my last olive branch to you. Its an example of creating a SSCCE. All the SSCCE does it try to add a row of data to the table when a button is pressed. Your SQL database is irrelevant to your problem and so is real data. The only thing that matter is how to use the addRow(...) method properly. Once you master that you figure out how to change your real program.




This basic structure can be used for any SSCCE you create in the future.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have good and bad news
good ones:
i can deal with default table model:

another good new is that i can implement addRow in my real code and it works fine:


the bad news is that i'm stuck with writting the ss to my custom model...
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need to use a custom model period. Don't reinvent the wheel unless you have a specific reason. Based on your stated requirements and the code you posted you are not doing anything the DefaultTableModel doesn't already do.

When you read the data from the database you load it into the DefaultTableModel directly. The code is almost identical to what you currently have. You create a Vector for the columne names and a Vector of Vectors for each row in the ResultSet and then you use the Vectors to create the DefaultTableModel.

And you now know how to use the addRow(...) method so the problem is solved.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:(...) so the problem is solved.


not so fast
i'm still stuck with my custom DefaultTableModel - i guessed i had all in place, but no... table doesnt show up
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a slight improvement:
added to tablemodel
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my first reply I suggested:

There is no need to create a custom TableModel. You can use the DefaultTableModel to hold the data.



In my next reply I repeated myself:

a) forget about creating a custom model and use the DefaultTableModel to hold the data.



In my last reply I repeated myself a third time:

There is no need to use a custom model period. Don't reinvent the wheel unless you have a specific reason.



So how do you respond. You post code of a "custom DefaultTableModel".

Do you not know what a "custom class" is? A custom class is when you extend a class to change its default behavour or add new behaviour. There is absolutely no need to do this. I don't know how I can be any clearer.

Did I not show you how to create a SSCCE with an empty DefaultTableModel? So now your next step to to create a DefaultTableModel with some data in it. The concept you are tying to learn is how to initially load data into the DefaultTableModel. The SQL is irrelevant, start by using hard coded data. You do this because if you have problems you can post the SSCCE with the hardcoded data. Posting code that references SQL is useless to us because we don't have access to your database.

Have you even looked at the DefaultTableModel API yet? If so you would have noticed the following constructor:



So all you need to do is create some hard coded data in the Vectors, then create the DefaultTableModel using these Vectors and then create the JTable using the DefaultTableModel. Many times when problem solving the mind gets on a single track. Another benefit of a SSCCE is that it gets you thinking in another direction as its all about simplifying the problem. Now that you don't have to worry about the SQL the task is simpler, the code is simpler and the code can be posted as a SSCCE if problems are encountered.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote: (...)
Have you even looked at the DefaultTableModel API yet? If so you would have noticed the following constructor:



So all you need to do is create some hard coded data in the Vectors, then create the DefaultTableModel using these Vectors and then create the JTable using the DefaultTableModel. Many times when problem solving the mind gets on a single track. Another benefit of a SSCCE is that it gets you thinking in another direction as its all about simplifying the problem. Now that you don't have to worry about the SQL the task is simpler, the code is simpler and the code can be posted as a SSCCE if problems are encountered.


Rob: if you had look at my last post you'd see i used that very same constructor!

where i'm stuck is in creating - i guess - the vector of vectors

please take another look at my last post (which improves the previous one)
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

please take another look at my last post (which improves the previous one)



It is NOT an improvement. For the fourth time, there is absolutetly no need to use the word "extends" anywhere in your code. I've run out of ways to say this!!!

where i'm stuck is in creating - i guess - the vector of vectors



And I don't see your SSCCE anywhere. You know how to create a Vector of Strings. You know how to create a Vector of Arrays. So I don't understand the problem with a Vector of Vectors.

No SSCCE, no more help.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not use the super constructor that takes a Vector<Vector<Object>> and a Vector<Object>. Yes that's right - Vector of Vectors. You also have an own Vector with column names, which will clash with the one of DefaultTableModel.

Your setResultSet method, using existing methods:
You may want to consider using setDataVector since that only triggers one update instead of one for each column, then one for each row. It also removes the need for clearing the rows and column which trigger another 2 updates.

I'm sure you can get that Vector of Vectors that is needed for setDataVector.

Now you can just remove all instance fields except the ResultSet and ResultSetMetaData fields, and all methods except the setResultSet and close methods. You already inherit all of those from the DefaultTableModel you're extending.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:(...)So I don't understand the problem with a Vector of Vectors.

No SSCCE, no more help.


just like me...

sinking deeper:
@Rob Prime: thanks for your hepl, but please let me first understand Camicks help, and then i'll focus on your answer
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code you never attach the model to the table.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:In your code you never attach the model to the table.


exactly
thank you!
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thats it. The only problem is that the table is not using the model you just created:

// JTable table = new JTable(5, 2);
JTable table = new JTable(model);

Because of the SSCCE I was able to see the exact problem. In reality you did know how to create a Vector of Vectors, the problem was you didn't know how to add the model to the table.

So now the next step is to create the "data" and "columnNames" Vectors from the data in the ResultSet. Hopefully now you see there is no need to extend the DefaultTableModel. All you do is copy the data from the ResultSet to the Vectors and then you create a new DeaultTableModel which is used by the table.

A Model has two functions:

a) it stores the data
b) it notifies the view when the data is changed

It is not responsible for retrieving the data that is why the DefaultTableModel constructors allow you to specify the Vectors (Arrays) containing the data. This is the most efficient way to create the model, because the model only notifies the view once when it is created. You could also create an empty model and then use the addRow(...) to add each row of the ResultSet to the model, but then the view is notified every time. So given that you can add data when the model is created or add data dynamically using the addRow/insertRow methods there is no need to create a custom model.

The only common reasons for extending the model would be when you want to change the behaviour of it. For example you could override the isCellEditable(...) method to allow the first column to be editable but not the second column. (Although even this can be done by overriding the isCellEditable() method of JTable, which is what I prefer).

Edit:

Wow, I must have taken a long time to write that up, I don't remember seeing the last two postings before I started.
reply
    Bookmark Topic Watch Topic
  • New Topic