无法拖动以调整JTable列的大小

时间:2010-03-23 11:25:13

标签: java swing resize jtable

长话短说,我正在制作一个自定义Swing组件,它基本上是一个带有面板的JTable。该表当然应该是可滚动的并且有一个表头,但由于我只希望标题位于实际的JTable之上而不是侧面板之上,所以我不得不拉一些技巧来使其工作。但那部分工作正常。

然而,在这个过程中,我以某种方式设法破坏了JTableHeader的鼠标拖动列调整大小功能。我完全不知道为什么以及我能做些什么。

下面是一个简单的工作样本来说明我的问题。

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.table.JTableHeader;


final class FooTable extends JPanel implements Scrollable {

    public FooTable() {
        initComponents();
    }

    // Generated by NetBeans IDE
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        sidePanel = new javax.swing.JPanel();
        table = new javax.swing.JTable();

        setLayout(new java.awt.GridBagLayout());

        sidePanel.setMinimumSize(new java.awt.Dimension(70, 0));
        sidePanel.setPreferredSize(new java.awt.Dimension(70, 0));
        sidePanel.setLayout(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        add(sidePanel, gridBagConstraints);

        table.setFont(table.getFont().deriveFont(table.getFont().getSize()+1f));
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "A", "I", "-"},
                {"2", "B", "II", "--"},
                {"3", "C", "III", "---"},
                {"4", "D", "IV", "----"},
                {"5", "E", "V", "-----"},
                {"6", "F", "VI", "------"},
                {"7", "G", "VII", "-------"},
                {"8", "H", "VIII", "--------"},
                {"9", "I", "IX", "---------"},
                {"10", "J", "X", "----------"}
            },
            new String [] {
                "Column 1", "Column 2", "Column 3", "Column 4"
            }
        ));
        table.setRowHeight(24);
        table.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        add(table, gridBagConstraints);
    }// </editor-fold>

    public JScrollPane createScrollView() {
        JScrollPane jsp = new JScrollPane(this);
        JViewport jvp = new JViewport();
        final JTableHeader th = new JTableHeader();

        th.setTable(table);
        th.setColumnModel(table.getColumnModel());
        th.setResizingAllowed(true);

        jvp.setView(new JPanel() {

            {
                setLayout(null);
                add(th);

                th.setLocation(70, 0);

                FooTable.this.addComponentListener(new ComponentAdapter() {

                    @Override
                    public void componentResized(ComponentEvent e) {
                        th.setSize(FooTable.this.getWidth(), th.getPreferredSize().height);
                    }
                });
                setPreferredSize(new Dimension(th.getPreferredSize().width, th.getPreferredSize().height));
            }
        });
        jsp.setColumnHeader(jvp);
        return jsp;
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel sidePanel;
    private javax.swing.JTable table;
    // End of variables declaration


    //
    // Scrollable implementation
    //
    public Dimension getPreferredScrollableViewportSize() {
        Dimension d = new Dimension();
        d.width = sidePanel.getPreferredSize().width + table.getPreferredSize().width;
        d.height = sidePanel.getPreferredSize().height + table.getPreferredSize().height;
        return d;
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return table.getScrollableUnitIncrement(visibleRect, orientation, direction);
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return table.getScrollableBlockIncrement(visibleRect, orientation, direction);
    }

    public boolean getScrollableTracksViewportWidth() {
        return table.getScrollableTracksViewportWidth();
    }

    public boolean getScrollableTracksViewportHeight() {
        return table.getScrollableTracksViewportHeight();
    }

    //
    // Test program
    //
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FooTable fooTable = new FooTable();
        f.add(fooTable.createScrollView());
        f.pack();
        f.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:2)

我认为您遗失的内容也在告诉JTable JTableHeader。在 createScrollView 方法中,尝试在返回之前添加以下内容:

    table.setTableHeader(th);