Align the values of the cells in JTable?

后端 未结 7 801
闹比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:48

    I have got a method that aligns a column in a table to the right:

    private void alignRight(JTable table, int column) {
        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
        table.getColumnModel().getColumn(column).setCellRenderer(rightRenderer);
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 19:01

    We need to make a small correction, the right way is DefaultTableCellRenderer.RIGHT

    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
    rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
    tableExample.getColumn("Price").setCellRenderer( rightRenderer );
    

    Notice the difference with the original response of camickr, "Price" is the name of the column, change according to the case.

    0 讨论(0)
  • 2020-12-18 19:03

    A simple way is using DefaultTableModel and override method getColumnClass()
    Ex:

    DefaultTableModel model = new DefaultTableModel() {
            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIndex == 0) {
                    return Integer.class;
                } else {
                    return String.class;
                }
            }
        };
    

    If you return Integer, your column will align right anh if return String your column will align left.

    0 讨论(0)
  • 2020-12-18 19:03

    as for more than one table at once i managed to do this ... its as @camickr posted but i added the headers too

        DefaultTableCellRenderer rightRenderer_c = new DefaultTableCellRenderer();
        DefaultTableCellRenderer rightRenderer_h = new DefaultTableCellRenderer();
        rightRenderer_c.setHorizontalAlignment( javax.swing.JLabel.RIGHT );
    
        for(JTable t : Tables){ //Tables is an ArrayList<JTable>
    
            //for the headers :
            rightRenderer_h = (DefaultTableCellRenderer) t.getTableHeader().getDefaultRenderer();
            rightRenderer_h.setHorizontalAlignment( javax.swing.JLabel.RIGHT );
    
            //for cells :
            for(int i=0; i < t.getColumnCount(); i++){
                t.getColumnModel().getColumn(i).setCellRenderer(rightRenderer_c);
            }
        }
    
    0 讨论(0)
提交回复
热议问题