JTable Right Align Header

前端 未结 13 2336
太阳男子
太阳男子 2020-11-27 19:03

Basically, I have a JTable containing columns with right-aligned cells but left-aligned headers which looks really bad. I would like to right-align the headers of these colu

相关标签:
13条回答
  • 2020-11-27 19:21

    The secret is to use the renderer from a dummy table to get correct L&F, and copy the alignment from the real table's row renderer. That way each column in aligned separately. Here is the code:

    table.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
            Component c2 = dummy.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            if (table.getRowCount() > 0) {
                Component c3 = table.getCellRenderer(0, col).getTableCellRendererComponent(table, value, isSelected, hasFocus, 0, col);
                if (c2 instanceof JLabel && c3 instanceof JLabel)
                    ((JLabel)c2).setHorizontalAlignment(((JLabel)c3).getHorizontalAlignment());
            }
            return c2;
        }
        private final JTable dummy = new JTable();
    });
    

    The above code does not keep any references to renderers, so it avoids the NPE bug mentioned above. It does not require any named class, so you can just drop the code in wherever you need it.

    0 讨论(0)
  • 2020-11-27 19:24

    Try this:

    ((DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer())
        .setHorizontalAlignment(JLabel.RIGHT);
    
    0 讨论(0)
  • 2020-11-27 19:24

    Try this code,

    JTableHeader jtableHeader = jtable.getTableHeader();
    DefaultTableCellRenderer rend = (DefaultTableCellRenderer) jtable.getTableHeader().getDefaultRenderer();
    rend.setHorizontalAlignment(JLabel.CENTER);
    jtableHeader.setDefaultRenderer(rend);
    
    0 讨论(0)
  • 2020-11-27 19:25
    for (int i = 0 ; i < table.getColumnCount(); i++){
    
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(SwingConstants.RIGHT);
        table.getColumn(i).setHeaderRenderer(renderer);
    
    }
    
    0 讨论(0)
  • 2020-11-27 19:30
    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) your_jtable.getTableHeader().getDefaultRenderer();
    renderer.setHorizontalAlignment(0);
    

    Where 0 is Centre.

    0 讨论(0)
  • 2020-11-27 19:35

    Here's an alternate approach to modifying the TableCellRenderer of a table's JTableHeader. It's not strictly necessary for this usage, but it minimizes the impact on the UI delegate's appearance.

    Typical usage:

    JTable table = new JTable(…);
    JTableHeader header = table.getTableHeader();
    header.setDefaultRenderer(new HeaderRenderer(table));
    

    Custom header renderer:

    private static class HeaderRenderer implements TableCellRenderer {
    
        DefaultTableCellRenderer renderer;
    
        public HeaderRenderer(JTable table) {
            renderer = (DefaultTableCellRenderer)
                table.getTableHeader().getDefaultRenderer();
            renderer.setHorizontalAlignment(JLabel.CENTER);
        }
    
        @Override
        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int col) {
            return renderer.getTableCellRendererComponent(
                table, value, isSelected, hasFocus, row, col);
        }
    }
    
    0 讨论(0)
提交回复
热议问题