2012-07-27 93 views

回答

37

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

+1很好的例子,對於_not_使用setXXSize :-) – kleopatra 2012-07-27 15:21:04

+3

mKorbel THX :),克列奧帕特拉「不使用setXXSize「這是肯定的。 – aterai 2012-07-30 05:39:59

+3

@aterai這是一個很好的,非常好。我之前使用了「不好」的方式,並面對上面顯示的問題。其實無法弄清楚是什麼原因..感謝很多,感謝你的努力! – Anubis 2012-07-30 10:57:48

-1

設定優選的報頭尺寸,其擺動截斷按列模型中的所需要的寬度時,也可以使用非常大的寬度。

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

請閱讀[這個問題]的答案(http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/): – bobndrew 2013-06-06 08:57:35

1

試試這個.....

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

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

試試這個

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