如何获得JTable的水平滚动条

时间:2014-11-19 08:15:06

标签: java swing jtable jscrollpane preferredsize

我有3列表格,固定滚动窗格尺寸。

第3列的数据超出列长。

要查看第3列中的数据,我需要调整嵌入的整个窗口的大小。

相反,我希望将3列拉伸到其最大数据长度。

如果这超出了滚动窗格的尺寸,我希望有一个水平滚动条。

==========

所以要实现的第一件事是如何指定“将列拉伸到其最大数据长度”。

示例代码没有功能

import java.awt.*;

import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders.MarginBorder;
import javax.swing.plaf.metal.MetalBorders.TableHeaderBorder;
import javax.swing.table.*;
import javax.swing.border.*;

public class TableRowRenderingTip extends JPanel
{
    public TableRowRenderingTip()
    {
        Object[] columnNames = {"Check","Allowed Commands", "Specification", "Description"};
        Object[][] data =
        {
            {Boolean.FALSE, "BASELINE-CONTROL", "WebDav", "Creates a snapshot of version control"},
            {Boolean.FALSE, "CHECKIN", "WebDav", "Checks in a verion controlled Web"},
            {Boolean.FALSE, "CHECKOUT", "WebDav", "Checks out a verion controlled Web"},
            {Boolean.FALSE, "CONNECT", "1.1", "Allow proxies to act as tunnels for"},

        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames)
        {           
            @Override
            public  Class<? extends Object> getColumnClass(int column)
            {
                return getValueAt(0, column).getClass();
            }

            @Override 
            public boolean isCellEditable(int row, int column)
            {
                return column==0;
            }

        };

        add( createData(model) );
    }



    private static JComponent createData(final DefaultTableModel model) 
    {       
        JTable inLineTable = new JTable( model );       
        inLineTable.setPreferredScrollableViewportSize(inLineTable.getPreferredSize());
        inLineTable.setShowGrid(false);
        inLineTable.setShowVerticalLines(false);
        JScrollPane scrollPane = new JScrollPane(inLineTable);
        scrollPane.setSize(10, 10);
        return scrollPane;
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.add( new TableRowRenderingTip() );
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

0 个答案:

没有答案