Results 1 to 5 of 5
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Right align a cell in JTable

    I have a JTable and would like to right align one of the cells. Default all cells is left align, but I would like one of the cells including the header to be right aligned.
    How can I do this? Any suggestions?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Right align a cell in JTable

    Moved from New to Java.

    Read the API for JTable and follow the link to the tutorial. Go through the section related to renderers.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Right align a cell in JTable

    Good tip. I solved this almost perfect with this code:
    Java Code:
     ledgerTable.setModel(new Classes.LedgerGrid());
     TableColumn col = ledgerTable.getColumn("Saldo");
     DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
     dtcr.setHorizontalAlignment(SwingConstants.RIGHT);
     col.setCellRenderer(dtcr);
     col.setHeaderRenderer(dtcr);
    However the HeaderRenderer made the background color on this cellheader disappeared. Any suggestions on this?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Right align a cell in JTable

    You could try my Default Table Header Cell Renderer « Java Tips Weblog for the header. Be sure to go through and understand the code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Right align a cell in JTable

    Thanks for the tip Darryl, but I only get all the headers aligned. Any suggestions how to align only one of the cells?

    Java Code:
    ledgerTable.setModel(new Classes.LedgerGrid());
    TableColumn col = ledgerTable.getColumn("Saldo");
    DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
    dtcr.setHorizontalAlignment(SwingConstants.RIGHT);
    col.setCellRenderer(dtcr);
    ledgerTable.getTableHeader().setDefaultRenderer(new DefaultTableHeaderCellRenderer());
    This right align the content of my table column named "Saldo" and all the headers. In the DefaultTableHeaderCellRenderer class i have this:
    Java Code:
    setHorizontalAlignment(RIGHT);
    But I only wants it for the "Saldo" column.

    I managed this, but not completely as I would like. The header design is changed on this column.
    I altered the code like this:
    Java Code:
    DefaultTableHeaderCellRenderer dth = new DefaultTableHeaderCellRenderer();
    col.setHeaderRenderer(dth);
    Right align a cell in JTable-columnheader.png

    As you can see the design is not like I want it to be on this column...

    EDIT: Added code
    EDIT2: Adjusted code
    Last edited by asai; 08-03-2012 at 05:20 PM.

Similar Threads

  1. Replies: 3
    Last Post: 01-10-2012, 11:33 AM
  2. Align Row to Right in JTable
    By shomid in forum AWT / Swing
    Replies: 2
    Last Post: 11-25-2011, 11:23 PM
  3. Replies: 0
    Last Post: 01-31-2011, 10:08 AM
  4. Setting jTable cell?
    By greatmajestics in forum New To Java
    Replies: 5
    Last Post: 04-22-2010, 06:33 PM
  5. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •