代码之家  ›  专栏  ›  技术社区  ›  perp

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

  •  2
  • perp  · 技术社区  · 14 年前

    长话短说,我正在制作一个定制的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 回复  |  直到 14 年前
        1
  •  2
  •   Klarth    14 年前

    我想你所缺少的也是 JTable 关于 JTableHeader . 在方法中 创建滚动视图 在返回之前尝试添加以下内容:

        table.setTableHeader(th);