22

I am creating an application for a billing facility. I want the amount column to display with right alignment. How do I set the right alignment for a JTable column?

Nathan
  • 8,093
  • 8
  • 50
  • 76
Arivu2020
  • 2,485
  • 7
  • 25
  • 24

4 Answers4

39
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
table.getColumnModel().getColumn(4).setCellRenderer(rightRenderer);
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
sathya
  • 1,404
  • 2
  • 15
  • 26
  • 7
    +1 for a correct answer, just: a) *DON'T SHOUT* b) please learn java naming conventions and stick to them (you can edit your answer and fix both) – kleopatra Jul 17 '13 at 08:29
  • I found it working but only partially. 1) If you set the column renderer first and then the model, the model invalidate the already set renderer, make it `null`. 2) I am using Nimbus L&F, and the newly set renderer change the background color to be inconsistent with other cells. I may open a question about this issue. – WesternGun Feb 27 '17 at 14:42
  • +0 This solution is simple and works, yes, but please consider the solution from @trashgod,more elegant and works easy to extend, particular if you plan to sort – user1136452 Jan 06 '23 at 19:28
8

Try this:

JTable tbl = new JTable(3,3) {
    DefaultTableCellRenderer renderRight = new DefaultTableCellRenderer();

    { // initializer block
        renderRight.setHorizontalAlignment(SwingConstants.RIGHT);
    }

    @Override
    public TableCellRenderer getCellRenderer (int arg0, int arg1) {
        return renderRight;
    }
};
Oliver Muchai
  • 279
  • 1
  • 8
  • 18
Emil
  • 13,577
  • 18
  • 69
  • 108
  • 1
    I hadn't thought of using an initializer block in anonymous classes. Solves the lack of constructor, nicely. – PeterVermont Apr 25 '13 at 22:00
  • -1 for short-circuiting all support for fine-grained renderer configuration of the table (by returning one-for-all) Also there is no need at all to subclass, instead use table/column's api to install custom renderer's as needed – kleopatra Jul 17 '13 at 08:25
  • An addition to kleopatra's comment as I'm currently doing the same thing as op: You can still kind of customize the renderer according to your needs with Emil's answer. `arg0` is the row and `arg1` is the column, so a simple e.g. `if(arg1==0)` lets you align all the cells in column 0 a certain way. I'm subclassing `JTable` anyway to set a custom color for specific cells (with `prepareRenderer`) and surprisingly they don't conflict - just don't return `null` (unless you want an exception). Plus, you don't have to set the column titles before doing it (like with sathya's accepted answer). – Neph Oct 23 '19 at 15:04
6

See Concepts: Editors and Renderers, noting "Number — rendered by a right-aligned label." Just have your TableModel return the correct class. As a concrete example, note that Integer is a Number, while examining the implementation of getColumnClass() in this example. In this related example, the zeroth colIndex returns Object.class, which is "rendered by a label that displays the object's string value." By default, the label is left-aligned.

switch (colIndex) {
    case 0: return Object.class;
    …
}

left-aligned

In contrast, Integer.class is "rendered by a right-aligned label."

switch (colIndex) {
    case 0: return Integer.class;
    …
}

right-aligned

These are examples of using Class Literals as Runtime-Type Tokens, discussed here in the context of JTable.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Have you even tried this? this doesn't work. Even if it did, I still wouldn't really like this approach just because you're trying to trick the class into thinking you're using a certain Object type, which may have other consequences that go beyond just aligning text. – searchengine27 Nov 09 '15 at 23:28
2

You will have to get DefaultTableCellRenderer for table cells and call setHorizontalAlignment(alignment).

Example can be found on links:

http://www.techrepublic.com/article/how-to-justify-data-in-a-jtable-cell/5032692/

http://www.coderanch.com/t/337549/GUI/java/align-data-columns-JTable

Shog9
  • 156,901
  • 35
  • 231
  • 235
YoK
  • 14,329
  • 4
  • 49
  • 67
  • +1 I'm a little wary of articles that misspell `JTable`, but the sidebar show a complete example. :-) – trashgod Aug 12 '10 at 11:38
  • class RightTableCellRenderer extends DefaultTableCellRenderer { protected RightTableCellRenderer() { setHorizontalAlignment(JLabel.RIGHT); } i add this code, after that how can i set the renderer to my jtable column. } – Arivu2020 Aug 12 '10 at 12:04