JTable header alignment

后端 未结 2 1613
栀梦
栀梦 2021-01-14 15:07

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);<

相关标签:
2条回答
  • 2021-01-14 15:28

    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;
                }
            });
    
    0 讨论(0)
  • 2021-01-14 15:34
    JTableHeader header = table.getTableHeader();
    TableCellRenderer renderer = header.getDefaultRenderer();
    renderer.setVerticalAlignment(SwingConstants.BOTTOM);
    
    0 讨论(0)
提交回复
热议问题