Swing无法看到水平滚动条

时间:2014-11-27 10:57:48

标签: java swing jtable jscrollpane jscrollbar

我想要一个水平滚动条来查看"描述"列。

当第二列中的数据超出时,我想要出现一个水平滚动条。这是我的代码: ========= 工作示例 ===============

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
{
    static Object[] columnNames = {"Allowed Commands", "Description"};
    private final static Dimension scrollPaneDimenssion = new Dimension(70, 200);
    public TableRowRenderingTip()
    {       
        Object[][] data =
        {
            {"One",  "Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for"},
            {"Two",  "Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for"},
            {"Three","Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for" },
            {"Four", "Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for"},

        };      
        DefaultTableModel model = new DefaultTableModel(data, columnNames);     
        add( createData(model) );
    }



    private static JComponent createData(final DefaultTableModel model) 
    {       
        JTable inLineTable = new JTable( model );

        inLineTable.setShowGrid(false);
        inLineTable.setShowVerticalLines(false);

        inLineTable.getColumn(columnNames[0]).setMaxWidth(130);
        inLineTable.getColumn(columnNames[0]).setMinWidth(130);
        inLineTable.getColumn(columnNames[0]).setResizable(false);
        inLineTable.getColumn(columnNames[1]).setMinWidth(270);
        inLineTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

        JScrollPane scrollPane = new JScrollPane(inLineTable);
        scrollPane.setMinimumSize(scrollPaneDimenssion);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        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);
    }
}

=============================================== === 但是scrollPane大小是固定的: 我想使用 HORIZONTAL SCROLL BAR 查看第二列中的数据 Image of the current state of the table

0 个答案:

没有答案