Jump to content
xisto Community
Sign in to follow this  
sanweikui

does anyone use the java swing?

Recommended Posts

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

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

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]

:angry: ,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.

:blink: I don't try it. but I think it will work. Good Luck.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.