Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Header renderer in JTable

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

    Default Header renderer in JTable

    Hi,

    I have a small problem with setting a table header renderer.

    This code works fine on the contents of the selected column:
    Java Code:
    ledgerTable.setModel(new Classes.LedgerGrid());
    TableColumn col1 = ledgerTable.getColumn("Saldo");
    DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
    dtcr.setHorizontalAlignment(SwingConstants.RIGHT);
    col1.setCellRenderer(dtcr);
    The content is aligned to the right as expected.

    In addition I have written this to also align the header:

    Java Code:
    HeaderRenderer dth = new HeaderRenderer();
    col1.setHeaderRenderer(dth);
    But for some reason the header on this column does not look the same as the rest of the columns.
    How can I set the look just like the rest of the columns?

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

    Default Re: Header renderer in JTable

    Moved from Advanced Java. Not an advanced topic either.

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

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

    Default Re: Header renderer in JTable

    Quote Originally Posted by asai View Post
    Java Code:
    HeaderRenderer dth = new HeaderRenderer();
    col1.setHeaderRenderer(dth);
    But for some reason the header on this column does not look the same as the rest of the columns.
    How can I set the look just like the rest of the columns?
    There's no class named HeaderRenderer in the JDK. Is that a class you wrote yourself?

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

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

    Default Re: Header renderer in JTable

    Ups... sorry...

    Heres the code:

    Java Code:
    public class HeaderRenderer extends DefaultTableCellRenderer {
    
     public HeaderRenderer() {
     setHorizontalAlignment(RIGHT);
         
      }
    }

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

    Default Re: Header renderer in JTable

    Yup, that would naturally look different, it's a DefaultTableCellRenderer. Have you tried getting a reference to the table's header renderer, casting that to JLabel and setting the alignment?

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

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

    Default Re: Header renderer in JTable

    Ok, I have read som docs about this and wrote this class:

    Java Code:
    public class IntervalHeaderRenderer extends JLabel implements TableCellRenderer {
    
        private TableCellRenderer delegate;
        private Component component;
    
        public IntervalHeaderRenderer(TableCellRenderer defaultRenderer)
        {
            this.delegate = defaultRenderer;
        }
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                               boolean isSelected, boolean hasFocus, int row, int column)
        {
            component = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            component.setBackground(Color.YELLOW);
    
            return component;
     
        }
    }
    Where I now only have set that, well i hoped, that one header should be yellow...
    And added this to my table:
    Java Code:
    TableCellRenderer renderer = ledgerTable.getTableHeader().getDefaultRenderer();
    ledgerTable.getColumnModel().getColumn(3).setHeaderRenderer(new IntervalHeaderRenderer(renderer));
    col1.setHeaderRenderer(renderer);
    But no success...

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

    Default Re: Header renderer in JTable

    Didn't you read my suggestion?
    Have you tried getting a reference to the table's header renderer, casting that to JLabel and setting the alignment?
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Header renderer in JTable

    I am sorry if I'm slow, but my english isn't fantastic... (I'm norvegian)

    Ok, so I have tried this:

    Java Code:
    public HeaderRenderer(JTable table, JLabel label) {
    renderer = (DefaultTableCellRenderer)
    table.getTableHeader().getDefaultRenderer();
    renderer.setHorizontalAlignment(label.CENTER);
    }
    }
    And this:

    Java Code:
    JLabel label = new JLabel("Sum");
    HeaderRenderer hr = new HeaderRenderer(ledgerTable,label);
    col1.setHeaderRenderer(hr);
    But still no change...
    Possibly I totally misreads your advices, but I am trying...

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

    Default Re: Header renderer in JTable

    Java Code:
    // Get a reference to the table header
    JTableHeader header = table.getTableHeader();
    // Get a reference to its renderer
    TableCellRenderer renderer = header.getDefaultRenderer();
    // Cast that to JLabel and set the alignment
    ((JLabel) renderer).setHorizontalAlignment(JLabel.RIGHT);
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Header renderer in JTable

    Thanks! Now we're close. I have 4 colums in the table, but I only wants the alignment for one of my colums.
    How could I do that?

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

    Default Re: Header renderer in JTable

    Quote Originally Posted by asai View Post
    I have 4 colums in the table, but I only wants the alignment for one of my colums.
    That's something you should have said earlier.

    Wrap the header renderer in a TableCellRenderer. This is from some old code of mine. Adapt it to suit your purpose, but more importantly, make sure you understand it.
    Java Code:
        JTableHeader header = table.getTableHeader();
        TableCellRenderer renderer = header.getDefaultRenderer();
    
        header.setDefaultRenderer(new HeaderRenderer(renderer, JLabel.CENTER));
        TableColumn c = table.getColumnModel().getColumn(1);
        c.setHeaderRenderer(new HeaderRenderer(renderer, JLabel.RIGHT));
    Java Code:
    class HeaderRenderer implements TableCellRenderer {
    
      private final TableCellRenderer renderer;
      private final int alignment;
    
      public HeaderRenderer(TableCellRenderer renderer, int alignment) {
        this.renderer = renderer;
        this.alignment = alignment;
      }
    
      public Component getTableCellRendererComponent(JTable table, Object value,
              boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel l = (JLabel) renderer.getTableCellRendererComponent(
                table, value, isSelected, hasFocus, row, column);
        l.setHorizontalAlignment(alignment);
        return l;
      }
    }
    If there's anything at all that you don't understand, please ask.

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

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

    Default Re: Header renderer in JTable

    Fantastic. It works great. Now I will play around with it so I completly understand it.
    Thank you very much db. Greatly appreciated.

Similar Threads

  1. Customize Jtable header
    By md_suzon in forum AWT / Swing
    Replies: 3
    Last Post: 10-12-2011, 07:48 AM
  2. Complet details of jtable and renderer
    By anilkumar_vist in forum AWT / Swing
    Replies: 1
    Last Post: 12-12-2009, 02:10 PM
  3. jtable cell renderer
    By ankitmcgill in forum New To Java
    Replies: 2
    Last Post: 05-22-2009, 02:08 AM
  4. how to use renderer in JTable
    By sunilpatel28 in forum Advanced Java
    Replies: 0
    Last Post: 12-09-2008, 09:01 AM
  5. header of jtable
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 05:11 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
  •