当我将策略设置为HORIZONTAL_SCROLLBAR_​​AS_NEEDED时,为什么我看不到水平滚动条

时间:2012-10-15 06:19:28

标签: java swing jtable jscrollpane jscrollbar

我在jTable内添加了jScrollPane作为:

    public Scenario_One() {
      initComponents();
      jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    }

    private void initComponents() {
       .
       .
       jScrollPane1.setViewportView(jTable1);
       .
       .
    }

显示在一行中的文字太大,所以我不得不使用水平滚动条。但即使将策略设置为as needed,我也看不到水平滚动条。这可能是什么原因?

2 个答案:

答案 0 :(得分:3)

正如@MadProgrammer已经评论过的那样,控制JScrollPane中的表大小调整行为的属性是autoResizeMode。基本上,有两种选择:

  • autoResizeOff:列始终以其prefWidth显示,根据需要显示/隐藏水平scrollBar。如果它们的总宽度小于视口宽度,则尾随空格保持空闲(显示视口)
  • 所有其他:列的大小使得表的总宽度适合scrollPane,水平scrollBar从不显示

(直观)期望表“适合”视口直到列太宽而无法完全显示然后自动允许滚动在核心JTable中支持。 JXTable(包含在SwingX project中)提供了额外的大小调整选项。

答案 1 :(得分:2)

尝试这样的事情......

enter image description here

public class TestTableColumns {

    public static void main(String[] args) {
        new TestTableColumns();
    }

    public TestTableColumns() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                DefaultTableModel model =new DefaultTableModel(
                                new Object[][] {
                                    {"1", "2"},
                                    {"11", "21"},
                                    {"12", "22"},
                                    {"13", "23"},
                                    {"14", "24"},
                                    {"15", "25"},
                                    {"16", "26"},
                                    {"17", "27"},
                                    {"18", "28"}}, 
                                new Object[] {
                                    "Small", "Big"});

                JTable table = new JTable(model);
                table.setShowHorizontalLines(true);
                table.setShowVerticalLines(true);
                table.setShowGrid(true);
                table.setGridColor(Color.DARK_GRAY);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                table.getColumn("Small").setPreferredWidth(100);
                table.getColumn("Small").setWidth(12);
                table.getColumn("Big").setPreferredWidth(400);
                table.getColumn("Big").setWidth(100);

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }            
        });
    }    
}

视口的宽度由其包含的组件的宽度决定。对于JTable,这取决于列的大小和自动尺寸政策