How to change JTable header height?
Asked Answered
S

6

17

Title explains the question. How can I easily do that?

Strickman answered 27/7, 2012 at 11:6 Comment(0)
S
41

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.table.*;

public class TableHeaderHeightTest {
  private static int HEADER_HEIGHT = 32;
  private JTable makeTable() {
    JTable table = new JTable(new DefaultTableModel(2, 20));
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    return table;
  }
  public JComponent makeUI() {
    JPanel p = new JPanel(new GridLayout(2,1));

    JTable table1 = makeTable();
    //Bad: >>>>
    JTableHeader header = table1.getTableHeader();
    //Dimension d = header.getPreferredSize();
    //d.height = HEADER_HEIGHT;
    //header.setPreferredSize(d); //addColumn case test
    header.setPreferredSize(new Dimension(100, HEADER_HEIGHT));
    p.add(makeTitledPanel("Bad: JTableHeader#setPreferredSize(...)", new JScrollPane(table1)));
    //<<<<

    JTable table2 = makeTable();
    JScrollPane scroll = new JScrollPane(table2);
    scroll.setColumnHeader(new JViewport() {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        d.height = HEADER_HEIGHT;
        return d;
      }
    });
//         //or
//         table2.setTableHeader(new JTableHeader(table2.getColumnModel()) {
//             @Override public Dimension getPreferredSize() {
//                 Dimension d = super.getPreferredSize();
//                 d.height = HEADER_HEIGHT;
//                 return d;
//             }
//         });
    p.add(makeTitledPanel("Override getPreferredSize()", scroll));

    final List<JTable> list = Arrays.asList(table1, table2);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(p);
    panel.add(new JButton(new AbstractAction("addColumn") {
      @Override public void actionPerformed(ActionEvent e) {
        for(JTable t: list) {
          t.getColumnModel().addColumn(new TableColumn());
          JTableHeader h = t.getTableHeader();
          Dimension d = h.getPreferredSize();
          System.out.println(d);
        }
      }
    }), BorderLayout.SOUTH);
    panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    return panel;
  }
  private static JComponent makeTitledPanel(String title, JComponent c) {
    JPanel p = new JPanel(new BorderLayout());
    p.add(c);
    p.setBorder(BorderFactory.createTitledBorder(title));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new TableHeaderHeightTest().makeUI());
    f.setSize(320, 320);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
Shack answered 27/7, 2012 at 11:20 Comment(2)
mKorbel thx :), kleopatra "not using setXXSize" that's for sure.Shack
@Shack That's a nice one, really nice. I used the "bad" way before and faced with that problem you've shown above. Actually couldn't figure out what was the reason..Thanks a lot, appreciate your effort!Strickman
C
2

try this.....

if jtable attached to scrollpane 
  then 
table.getTableHeader().setPreferredSize(
     new Dimension(scrollpane.getwidth(),height)
);

else
  table.getTableHeader().setPreferredSize(
     new Dimension(width,height)
);
Celinecelinka answered 27/11, 2014 at 9:30 Comment(1)
If I do it your way, then the table's header doesn't scroll with the rows. If I do it aterai's way, then the header does scroll with the rows.Kellene
D
1
Font bigFont = new Font("sansserif", Font.PLAIN, 24); // or whatever
myTable.getTableHeader().setFont(bigFont);

This is a simple way to increase the preferredSize of the TableHeader. It will increase the height of the header, but if your column names are too long then they might not fit widthwise.

Dickman answered 7/7, 2018 at 20:30 Comment(1)
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.Heaps
M
0

Try this

JTableHeader th = table.getTableHeader();
th.setPreferredSize(new Dimension(100, 100));
March answered 30/5, 2017 at 23:34 Comment(0)
L
0

Instead of overriding JViewport.getPreferredSize like in aterai answer you can just set preferred width.

JScrollPane scroll = new JScrollPane(table2);
scroll.getColumnHeader().setPreferredSize(new Dimension(0, HEADER_HEIGHT));

JScrollPane ignores column header width, so you can safely set zero here.

Liverwort answered 15/6, 2018 at 14:0 Comment(0)
H
-1

You can also use a very large width when setting the preferred header dimension, which swing truncate as per the required widths in the column model.

tableHeader.setPreferredSize(new Dimension(10000, HEADER_HEIGHT));
Hypogynous answered 11/1, 2013 at 2:4 Comment(1)
please read the answers to this question :Bostwick

© 2022 - 2024 — McMap. All rights reserved.