sanweikui 0 Report post Posted October 8, 2004 My question is that how to set the alignment about a column of the table ? Share this post Link to post Share on other sites
arp 0 Report post Posted October 9, 2004 Hi;I strongly suggest you try to be a bit more specific in your question - i.e. which version are you using?what needs to be aligned? Text in a cell?You may want to look at DefaultTableCellRenderer in the javaDoc.if you can be more specific, then it will be easier to give you the answer you need.best regards,- arp Share this post Link to post Share on other sites
sanweikui 0 Report post Posted October 10, 2004 ok,my java version is jdk1.4 . I want to set one column of the table alignment. Certainly the Text of the all the cell of the column. My code is: [/br]public class TableColumnTest extends JFrame {[br] private TableModel model=new AbstractTableModel(){[/br] public int getColumnCount(){return 10;};[br] public int getRowCount(){return 10;};[/br] public Object getValueAt(int row,int col){ return new Integer(row*col); };[br] };[/br] public TableColumnTest(){[br] super("tableColumnTest");[/br] setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);[br] [/br] JTable table=new JTable(model);[br] TableColumn column1=table.getColumn(table.getModel().getColumnName(1));[/br] DefaultTableCellRenderer renderer=(DefaultTableCellRenderer)column1.getCellRenderer();[br] if(renderer==null){[/br] renderer=(DefaultTableCellRenderer)table.getDefaultRenderer(table.getColumnClass(1));[br] }[/br] renderer.setHorizontalAlignment(SwingUtilities.RIGHT);[br] getContentPane().add(new JScrollPane(table));[/br] }[br][/br] public static void main(String[] args) {[br] JFrame f=new TableColumnTest();[/br] f.setSize(300,200);[br] f.setVisible(true);[/br] }[br]}[/br] Not only the single column but all of the table columns align right. Share this post Link to post Share on other sites
gdpglc 0 Report post Posted March 11, 2005 DefaultTableCellRenderer renderer=(DefaultTableCellRenderer) column1.getCellRenderer(); ?# if(renderer==null)# {# renderer=(DefaultTableCellRenderer)# table.getDefaultRenderer(table.getColumnClass(1));#?} ? renderer.setHorizontalAlignment(SwingUtilities.RIGHT);Not only the single column but all of the table columns align right. 18997[/snapback] ,I think the cause all table colmns aligning right is the code signed by me is executed. the variable "renderer" comes from method "getDefaultRenderer". All table colmns use shared DefaultRenderer, So you modify the DefaultRenderer, make all tabe colmns align right. You can define self renderer class and set to the second colmns and invoke the method "setHorizontalAlignment" of your renderer. I don't try it. but I think it will work. Good Luck. Share this post Link to post Share on other sites