3

I would like to set the vertical alignment of the label in the header of my JTable-derrived class.

I am aware of setVerticalAlignment(SwingConstants.BOTTOM);

My header is much higher than the font and I would like to place the text slightly below the vertical centre.

How can I do this, without overriding paint() ?

THX

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Adam
  • 637
  • 1
  • 8
  • 21
  • that's not possible, the layout of a JLabel is rather fixed. What you might try is setting an invisible icon of appropriate size and set the text position to be below the icon. Beware: that'll clash with sort markers, as most LAFs use the icon as such. – kleopatra Dec 16 '11 at 10:03
  • forgot the usual for unusual requirements: why? – kleopatra Dec 16 '11 at 10:04
  • 1
    Because attention to such tiny details make things looks better. Sometimes you need to think like a graphic designer, not a programmer :) – Adam Dec 16 '11 at 10:41
  • then you'll probably end writing an entire LAF :-) Or find one, commercial or free, there are several looking quite polished - _with_ attention to detail – kleopatra Dec 16 '11 at 10:45
  • BTW, what is your target LAF? – kleopatra Dec 16 '11 at 10:47
  • Kleo, It's windows for the moment, but we plan on improving upon it :) – Adam Dec 16 '11 at 11:17

2 Answers2

7

one of ways is to set Renderer, TableHeader by default returns JLabel, for example

  final TableCellRenderer tcrOs = myTable.getTableHeader().getDefaultRenderer();
       myTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, 
                   Object value, boolean isSelected, boolean hasFocus, 
                   int row, int column) {
                JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(table, 
                      value, isSelected, hasFocus, row, column);
                lbl.setForeground(AppVariables.textColor);
                lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), 
                      BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                lbl.setHorizontalAlignment(SwingConstants.LEFT);
                if (isSelected) {
                    lbl.setForeground(Color.red);
                    lbl.setBackground(Color.lightGray);
                } else {
                    lbl.setForeground(Color.blue);
                    lbl.setBackground(Color.black);
                }
                return lbl;
            }
        });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Thanks mKobel! kleo it IS related. The trick I needed is burried in there. All I needed was: setBorder(BorderFactory.createCompoundBorder(getBorder(), BorderFactory.createEmptyBorder(6, 0, 0, 0))); – Adam Dec 16 '11 at 10:52
  • okay, I stand corrected, actually overlooked that line in all the clutter ;-) – kleopatra Dec 16 '11 at 10:58
  • 1
    Still a lot of random code to simply add a Border to a renderer. Sometimes it is hard to see the tree through the forest because you always add so much unnecessary code. – camickr Dec 16 '11 at 16:33
2
JTableHeader header = table.getTableHeader();
TableCellRenderer renderer = header.getDefaultRenderer();
renderer.setVerticalAlignment(SwingConstants.BOTTOM);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    This is NOT an answer to my question. Read the question more carefully. – Adam Dec 16 '11 at 10:38
  • @Adam, I read the question 3 times. I still posted what I thought was the answer you where looking for. Usually I can read read between the lines for a poorly worded question, but I guess not this time. Next time post a proper [SSCCE](http://www.sscce.org) so we don't have to guess. – camickr Dec 16 '11 at 16:34
  • There's no method `setVerticalAlignment()` in the `TableCellRenderer` interface. The renderer has to be cast to `DefaultTableCellRenderer`. – 1000ml Jan 20 '17 at 17:40
  • setVerticalAlignment cannot be found. thus code isn't Not working – Kenny Dabiri Jun 06 '20 at 03:21