25

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

How to align as above the JTable

Venkat
  • 20,802
  • 26
  • 75
  • 84

7 Answers7

46

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.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    The correct constant name is `DefaultTableCellRenderer.RIGHT`, see the @Adrian's post in this thread. (Or `SwingConstats.RIGHT`). – xmedeko Sep 17 '12 at 08:45
11

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.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
Adrian
  • 655
  • 6
  • 10
  • 1
    RIGHT is actually defined in interface SwingConstants (which JLabel and DefaultTableCellRenderer both implement/inherit). So any of these prefixes get you a value for RIGHT... but for clarity why not just use SwingConstants.RIGHT? Or if you feel you want to be absolutely sure you have the "RIGHT" that pertains to the renderer at hand, you could use rightRenderer.RIGHT. – gwideman Nov 26 '12 at 12:26
  • 4
    `SwingConstants.RIGHT` is the appropriate constant. – Erick Robertson May 07 '14 at 12:07
  • 2
    In fact when using `DefaultTableCellRenderer.RIGHT`, Eclipse gives a warning _"The static field SwingConstants.RIGHT should be accessed directly"_ – Zach Olivare Jan 06 '16 at 16:29
5

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.

Lê Quang Duy
  • 767
  • 7
  • 14
  • This is a bad solution because it tricks the renderer into thinking that it's dealing with a different class than it actually is, which might cause problems later. See [this](https://stackoverflow.com/a/3467088/2016165) answer. – Neph Oct 23 '19 at 15:13
4

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
3

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);
}
Al Kepp
  • 5,831
  • 2
  • 28
  • 48
fabian
  • 31
  • 1
0

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);
        }
    }
OmAr
  • 97
  • 8
0

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, ...)

Itay Maman
  • 30,277
  • 10
  • 88
  • 118