Align the values of the cells in JTable?

后端 未结 7 800
闹比i
闹比i 2020-12-18 18:28

I\'m not aware of how to align the values of cells in JTable.

For Ex,The Jtable shows, Name Salary Mr.X 100000.50 XXXX 234.34

7条回答
  •  时光说笑
    2020-12-18 18:55

    There is no need to create a custom class for this, just use the default renderer:

    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
    rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
    table.getColumnModel().getColumn(???).setCellRenderer(rightRenderer);
    

    Or a better approach is to actually store Double values in the table and then a proper numeric renderer will be used and number renderers are automatically right aligned. You can then customize the formatting of the number using a Table Format Renderer.

提交回复
热议问题