开发者

Align the values of the cells in JTable?

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 YYYy 1205.50

I want to align the "Salaries" in the following format.

   Name      Salary
   Mr.X      100000.50开发者_如何学C
   XXXX         234.34
   YYYy        1205.50

How to align as above the JTable


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.


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.


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.


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.


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);
}


The way to go about it is to specify a custom cell renderer for each column. Each renderer will format that data differently (names will e aligned to the left, decimals to the right, ...)


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜