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

JTable and cell aligning

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi,
How do I right-align data in one of my JTable cells? I know that by default integer values are right aligned, but I have a string in my JTable representing an integer -- so can't use that. I read that I can use DefaultTableCellRenderer, I tried it, but it has no affect on my code. What am I doing wrong?
This is what I have:
model=new com.kwi.ChildOrdModel(model1);
ordbox=new JTable(model);
ordbox.setPreferredScrollableViewportSize(new Dimension(650,192));
DefaultTableCellRenderer Quantity = new DefaultTableCellRenderer();
Quantity.setHorizontalAlignment(SwingConstants.RIGHT);
TableColumnModel columns = ordbox.getColumnModel();
columns.getColumn(2).setCellRenderer(Quantity);
thanks in advance,
Alex

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Alex,
What's this Quantity variable?
try this:
MyCellRenderer extends JLabel implements TableCellRenderer
{
public Component getComponent(// args)
{
this.setHorizontalAlignment(SwingConstants.RIGHT);
return this;
}
}
I've not tested this code. maybe something will be missing, but I hope it gives you an idea.
chantal
 
Consider Paul's rocket mass heater.
 
subject: JTable and cell aligning