JTable中的数据为什么向右对齐,可不可以强制设置让其左对齐?

时间:2021-02-09 04:49:20
我没有对JTable中的显来位置进行设置,但JTable中的数据为什么向右对齐,可不可以强制设置让其左对齐?

2 个解决方案

#1


默认是左对齐吧?

#2


public class TbCellRender extends DefaultTableCellRenderer {
    public TbCellRender(String orient,double down) {
        if(orient==null||orient.trim().equals(""))
            orient = "left";

        this.down = down;
        orient = orient.toLowerCase().trim();
        char align = orient.charAt(0);
        switch (align) {
            case 'r':
                setHorizontalAlignment(JLabel.RIGHT);
                break;
            case 'l':
                setHorizontalAlignment(JLabel.LEFT);
                break;
            case 'c':
                setHorizontalAlignment(JLabel.CENTER);
                break;
            default:
                setHorizontalAlignment(JLabel.LEFT);
                break;
        }
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int col) {

        Component comp = super.getTableCellRendererComponent(table, value,
            isSelected, hasFocus, row, col);
        return comp;
    }

#1


默认是左对齐吧?

#2


public class TbCellRender extends DefaultTableCellRenderer {
    public TbCellRender(String orient,double down) {
        if(orient==null||orient.trim().equals(""))
            orient = "left";

        this.down = down;
        orient = orient.toLowerCase().trim();
        char align = orient.charAt(0);
        switch (align) {
            case 'r':
                setHorizontalAlignment(JLabel.RIGHT);
                break;
            case 'l':
                setHorizontalAlignment(JLabel.LEFT);
                break;
            case 'c':
                setHorizontalAlignment(JLabel.CENTER);
                break;
            default:
                setHorizontalAlignment(JLabel.LEFT);
                break;
        }
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int col) {

        Component comp = super.getTableCellRendererComponent(table, value,
            isSelected, hasFocus, row, col);
        return comp;
    }