• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable and cell aligning

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic