• 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

DefaultTableModel and Multiple Selections.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm stumped.

I have a JTable. In the model, I have two columns. The first is an object and the table uses it's toString() method to determine what it displays. The second is another object and that works similarly.

When the user presses a button, I want to retrieve the object in the first column and make some changes to it. What I change depends on whether one row is selected or multiple.

When the table has only a single row selected, this works perfectly. However, if multiple rows are selected, calling getValueAt(n,0) returns the String from the object's toString() rather than the object itself. Can somebody explain this to me? My code is below. I am using NetBeans 6.8 on 64-bit Java 6 on a Mac.

When one row selected:



When multiple rows are selected:



In the first case, t is an instance of Territory. In the second, t is an instance of String and a ClassCastException is thrown.

 
Marshal
Posts: 28137
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're going to have to look at the code of your table model to see why it's doing what it's doing. Just showing us code which calls methods of the table model isn't helpful.
 
Sheriff
Posts: 22780
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
Have you by any chance edited some of the columns? If your model says the column class is Object.class and you haven't defined a table cell renderer yourself for that column then the default is used, and that will simply put Strings into the model.
 
Gerald Meazell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You're going to have to look at the code of your table model to see why it's doing what it's doing. Just showing us code which calls methods of the table model isn't helpful.



I am using the DefaultTableModel class from the JDK.
 
Gerald Meazell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Have you by any chance edited some of the columns? If your model says the column class is Object.class and you haven't defined a table cell renderer yourself for that column then the default is used, and that will simply put Strings into the model.



Interesting idea, but why would it behave one way when it's a single select and a completely different way when multiple?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gerald Meazell wrote:

Paul Clapham wrote:You're going to have to look at the code of your table model to see why it's doing what it's doing. Just showing us code which calls methods of the table model isn't helpful.



I am using the DefaultTableModel class from the JDK.



It works fine for me. My SSCCE:



I suggest that if you're still having a problem, you produce an SSCCE so that we can test your code and isolate your problem. Else, we'll probably not be able to help you.
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic