How to allign the Content of Cell according to some Condtion
I wants to allign the cell Left if the content is String and
to allign the cell Right if the content is a numerical(int ,long, double )
In this few cell are Rendered ,and the remaings are not.
So how i can go ahead?
I checked few of the column with renderer, are working fine with setHorizontalTextPosition(SwingConstants.RIGHT) code.
But myTable is contain 25 columns.
I plans to check the dataType of column,then use the switch case,but which method will be helpful for that?
please suggest.Thanks in Advance
[559 byte] By [
sheelata] at [2007-11-26 14:00:02]

# 1
> I wants to allign the cell Left if the content is
> String and
> to allign the cell Right if the content is a
> numerical(int ,long, double )
>
> n this few cell are Rendered ,and the remaings are
> not.
>
> So how i can go ahead?
>
> I checked few of the column with renderer, are
> working fine with
> setHorizontalTextPosition(SwingConstants.RIGHT)
> code.
>
> But myTable is contain 25 columns.
>
> I plans to check the dataType of column,then use the
> switch case,but which method will be helpful for
> that?
>
> please suggest.Thanks in Advance
The right approach will be to set the alignment in the renderer. If each cell is a bean, you can set the "data type" or else determine the alignment based on the row/column.
# 3
Associate a cellrender with a column class with the setDefaultRenderer method and create your own renderer like :
private class MyTableCellRenderer extends DefaultTableCellRenderer {
public MyTableCellRenderer() {}
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus,int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// Do what you want with the cell
cell.setBackground(new Color(255,200,200));
return cell;
}
}
ProZa at 2007-7-8 1:41:12 >

# 4
> I want to allign the cell Left if the content is String and to allign the cell Right if the content is a numerical(int ,long, double )
JTable already has different renderers to do this. You just need to tell the table the type of data in each column and it will use the appriate renderer:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=627108