How to change JTable header height? 设置JTableHeader 高度的正确方式

 1-设置 JTable header height错误方式

  table.getTableHeader().setPreferredSize(new Dimension(10,10)); 

 

  为什么是错误的?请看截图

  当拖动列的时候 错误就出现了?表头和列错乱了.而国内清一色的采用上一种错误方式

 

enter image description here

 

2 正确的解决方式:

我们公司大神 坤哥在http://stackoverflow.com/ 找到的解决方案:

http://stackoverflow.com/questions/11686535/how-to-change-jtable-header-height

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值