aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes updating jTable's display Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "updating jTable Watch "updating jTable New topic
Author

updating jTable's display

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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?


java amateur
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
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

Joined: Feb 08, 2004
Posts: 1281
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

rowData is a Vector<String> but you treat it as a Collection<? extends Object[]>.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
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

Joined: Feb 08, 2004
Posts: 1281
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Feb 08, 2004
Posts: 1281
a slight improvement:
added to tablemodel
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
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

Joined: Feb 08, 2004
Posts: 1281
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

In your code you never attach the model to the table.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Prime wrote:In your code you never attach the model to the table.

exactly
thank you!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: updating jTable's display
 
Similar Threads
java.lang.NumberFormatException when sorting table column
updating jTable
How to retrieve data from database every 10 minutes?
Generate JTable from a SQL Query containing UNION clause
how to "clear" jTable