如何更改JTable标题高度?

时间:2012-07-27 11:06:07

标签: java swing jtable jtableheader preferredsize

标题解释了这个问题。我怎么能轻易做到这一点?

6 个答案:

答案 0 :(得分:38)

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

答案 1 :(得分:1)

试试这个......

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

else
  table.getTableHeader().setPreferredSize(
     new Dimension(width,height)
);

答案 2 :(得分:1)

Font bigFont = new Font("sansserif", Font.PLAIN, 24); // or whatever
myTable.getTableHeader().setFont(bigFont);

这是增加TableHeader的preferredSize的简单方法。它将增加标题的高度,但是如果您的列名太长,则它们可能不适合宽度方向。

答案 3 :(得分:0)

试试这个

JTableHeader th = table.getTableHeader();
th.setPreferredSize(new Dimension(100, 100));

答案 4 :(得分:0)

您可以设置首选宽度,而不是像在aterai答案中那样覆盖JViewport.getPreferredSize。

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

JScrollPane忽略列标题宽度,因此您可以在此安全地设置零。

答案 5 :(得分:-1)

在设置首选标题尺寸时,您也可以使用非常大的宽度,根据列模型中的所需宽度,它会截断。

tableHeader.setPreferredSize(new Dimension(10000, HEADER_HEIGHT));