Results 1 to 5 of 5
  1. #1
    Angie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Question [SOLVED] How do you align/justify idividual table column header text?

    If I have a JTable with 3 columns where the headers are "ID", "IN STOCK", "QUANTITY" and I want to individually align each header different.

    I would like "ID" header to be left aligned, "IN STOCK" header to be center aligned and "QUANTITY" header to be right aligned, how do I do that?

    I found the following code:

    TableCellRenderer renderer = table().getTableHeader().getDefaultRenderer();
    JLabel label = (JLabel)renderer;
    label.setHorizontalAlignment(JLabel.LEFT);

    but it causes all table column header text to be aligned.

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    This was extremely harder than I thought it would be... Check out:

    ToolTip Table : Grid Table**Swing Components**Java

    You want to use the MultiLineHeaderRenderer... Then do this:

    Java Code:
            TableColumn column = table().getColumnModel().getColumn(1);
            MultiLineHeaderRenderer headerRenderer = new MultiLineHeaderRenderer(
            SwingConstants.LEFT, SwingConstants.BOTTOM);
            column.setHeaderRenderer(headerRenderer);
    Let me know if you get stuck...
    Who Cares... As Long As It Works...

  3. #3
    Angie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    That was a good learning experience for me... Thank you once again markw8500!!! :)

  4. #4
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    No Prob...
    Who Cares... As Long As It Works...

  5. #5
    FALL3N is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default MultiLineHeaderRenderer: symbol not found

    is there some package I need to import or something?

    Im getting an error that says:
    cannot find symbol:
    class MultiLineHeaderRenderer

Similar Threads

  1. Replies: 2
    Last Post: 02-28-2009, 07:30 AM
  2. Replies: 0
    Last Post: 07-11-2008, 04:30 PM
  3. Sorting a SWT table by column
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 03:07 PM
  4. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 07:08 PM
  5. Help with Format.justify method
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:29 AM

Posting Permissions

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