aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable cell alignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable cell alignment" Watch "JTable cell alignment" New topic
Author

JTable cell alignment

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
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

Joined: Sep 11, 2007
Posts: 171
Found my solution:




Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
I gave you an answer 3 weeks ago.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
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

Joined: Sep 11, 2007
Posts: 171
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

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

Joined: Oct 14, 2005
Posts: 18121
    
    8

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

Joined: Sep 11, 2007
Posts: 171
Paul, thanks, using the super. worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable cell alignment
 
Similar Threads
Is there a way to show an image in a JTable as a thumbnail?
Multi-dimension arrays
Table cells not editable
Out of bound on removeRow
JTable dates