File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable, reloading the table...

 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to reload the table at certain times in my application. I didn't find a reload method, so I tried doing this but it didn't work. Any ideas?

- getAllStudents returns a 2D object array with all of my info.
- columnData is a 1D object array with the column names.
 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update...
I'm not sure how to update the table, so I'm just using the setValueAt methods from the TableModel object. Problem is my event code continues to run in a loop because I'm changing the table inside of my tableChanged event. Does anyone know how I can get this to work like I want to?
 
Abhik Sarkar
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
If your table's data is going to be dynamic, I would stronly suggest that you write your own TableModel... it will give you a lot more control and I can assure you that it will be worth the additional effort.
However, if you must use the array, this might work...
1. Don't put anything in the event handling code.
2. Change the values directly in the array.
Then,

You should see the updated values in the table.
Hope this helps. I will try and give a working example when I have the time.
Regards,
Abhik.
 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a ClassCastException on this line of code:
 
Garandi Garandi
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have extended "AbstractTableModel", then cast to sub-class of AbstractTableModel.

This should do it.
Thank you
Garandi
Now in your code do
 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't extend anything else because I'm already extending JFrame. Would making another class work?
[ June 20, 2003: Message edited by: Chris Stewart ]
 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried creating a second class called myModel and extending AbstractTableModel, but on this line:
MyModel model = (MyModel) table.getModel();
I'm getting a ClassCastException.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Stewart:
I'm getting a ClassCastException on this line of code:

Why are you casting? I don't think you need to.
 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got it working. Here is the myModel class:

Then in my Layout class, I use this to reload it:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic