Align the values of the cells in JTable?

后端 未结 7 832
闹比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:59

    From this forum post:

    Create a class that extends DefaultTableCellRenderer and implement the getTableCellRendererComponent() method, something like:

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        JLabel renderedLabel = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        renderedLabel.setHorizontalAlignment(SwingConstant s.RIGHT);
        return renderedLabel;
    }
    

    and install this renderer for the column in question.

    Now you only need to make sure that each value has the same number of decimal places because for most fonts, all digits have the same width.

提交回复
热议问题