Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable cell alignment

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the DefaultTableModel, is there a way to align the contents in a cell? I have the following data (dollar amount, column 6) that I would prefer be right-aligned in the cell.


 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found my solution:




 
Rob Camick
Ranch Hand
Posts: 2600
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you an answer 3 weeks ago.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, you did give me an answer when I was working with an AbstractTableModel, but when I switched to the DefaultTableModel it no longer worked. Maybe it was the way I implemented your suggestion, not sure. That is why I asked again for the DTM, sorry if it was inappropriate.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean, it no longer worked? It's exactly what you did to solve it! I quote from the other thread:
Rob Camick wrote:This is done by overriding the getColumnClass() method to return the proper class of data. So for dates, store a Date object in the model. For currency, store a Double object in the model. The table will choose the appropriate renderer and the renderer will choose the appropriate alignment.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, before the code was:



When I change it to DefaultTableModel from AbstractTableModel it didn't work. The second change was small, but (at the time of this post) I thought perhaps there was something different between Default and Abastract. The new code worked, maybe I made other changes that messed things up.

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's like another issue I'm having, and I've been real hesitant to post it. I searched for answers on how to capture a change in a cell, and all of the responses I've seen mention using setValueAt, no final code is posted and, in most cases, the thread just ends. Well, I tried that:



And, yes, I can capture the changes, but when the table initially displays all of its' contents are blank. Right number of rows and columns, but no data. Further searching showed suggesting adding



Well, that doesn't compile. Changing it to

compiles, but nothing is different. I figure others got it to work, but I'll be d--ned if I can figure out from the docs I've looked at on how it's done. Guess I'm just dumber than the others here, which makes me hesitant to even ask questions.
 
Rob Camick
Ranch Hand
Posts: 2600
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe I made other changes that messed things up.


I'm sure I've asked you for a SSCCE before. This question is no different and this is one of the reasons why. We don't have time to guess what else you might have changed.

I searched for answers on how to capture a change in a cell,


Yes, well that question has nothing to do with this question. Keep a thread to one question otherwise it gets confusing with all the suggestions to multiple question in the same thread.
 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Lipay wrote:That's like another issue I'm having, and I've been real hesitant to post it. I searched for answers on how to capture a change in a cell, and all of the responses I've seen mention using setValueAt, no final code is posted and, in most cases, the thread just ends. Well, I tried that:



And, yes, I can capture the changes, but when the table initially displays all of its' contents are blank. Right number of rows and columns, but no data.


What you did there was to override the setValueAt method to just throw away the parameters it was passed. What it should do is to update the model with that value in some way.

If I'm not mistaken, DefaultTableModel already has an implementation of setValueAt which updates the model data, so just removing that method entirely might be the best approach. But if you want to do something at the time that setValueAt is called, while still updating the model data, then just call super.setValueAt(val, row, col) inside your overridden method.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, thanks, using the super. worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic