在将Java 7升级到Java 8之后,不会在JScrollPane中使用JTable显示滚动

时间:2017-03-28 10:45:23

标签: java swing java-8 jtable jscrollpane

我要将项目升级到Java 8.运行我的应用程序后,从JScrollPane滚动不会显示。同样的代码在Java 7中工作,一切正常。

public static void main(String[] a){
    JPanel contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    contentPane.setLayout(new javax.swing.BoxLayout(contentPane, javax.swing.BoxLayout.Y_AXIS));

    JPanel aggregates = new JPanel("Aggregates", new Insets(40, 0, 0, 0));
    GridBagConstraints gbc_aggregates = new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(10, 10, 10, 10), 0, 0);
    gbl_panel.setConstraints(aggregates, gbc_aggregates);

    GridBagLayout gbl_agregats = new GridBagLayout();   
    addParametersLayout(gbl_agregats, new int[]{0,0,0}, new int[]{0, 0},new double[]{ 0.0, 0.0, Double.MIN_VALUE}, new double[]{0.0, Double.MIN_VALUE} );
    aggregates.setLayout(gbl_agregats);

    JTable table = new JTable(new MyTableModel(null));
    table.setPreferredSize(new Dimension(850, Const.rowHeight));

    JScrollPane scrollPane = new JScrollPane(table);
    table.setPreferredScrollableViewportSize(new Dimension(850, 10 *Const.rowHeight));
    GridBagConstraints gbc_scrollPane= new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(5, 5, 20, 5), 0, 0);
    gbl_agregats.setConstraints(scrollPane, gbc_scrollPane);
    aggregates.add(scrollPane);

    JPanel main_panel = new JPanel();
    main_panel.add(aggregates); 

    JScrollPane scrPane = new JScrollPane(main_panel);
    scrPane.getVerticalScrollBar().setUnitIncrement(16);
    scrPane.setBorder(BorderFactory.createEmptyBorder());
    scrPane.getInsets().set(0, 0, 0, 0);
    contentPane.add(scrPane);
    setContentPane(contentPane);    
}

public static void addParametersLayout(GridBagLayout gbl,
int[] columnWidths, int[] rowHeights, double[] columnWeights,
double[] rowWeights) {
    gbl.columnWidths = columnWidths;
    gbl.rowHeights = rowHeights;
    gbl.columnWeights = columnWeights;
    gbl.rowWeights = rowWeights;
}

问题:Java 8中有哪些变化?为什么在旧Java版本中相同的滚动看起来没问题?

编辑:

也许我不太具体。 当我编译我的项目时,我看到类似的东西(只是吧):

enter image description here

我可以滚动我的桌子,因为它正在工作,但它是不可见的(我看到的只是一个小节)。

2 个答案:

答案 0 :(得分:0)

          import java.awt.Color;
          import java.awt.Dimension;
          import java.awt.GridBagConstraints;
          import java.awt.GridBagLayout;
          import java.awt.Insets;
          import java.awt.LayoutManager;
          import javax.swing.BorderFactory;
          import javax.swing.JPanel;
          import javax.swing.JScrollPane;
          import javax.swing.JTab
          import javax.swing.border.EmptyBorder;



          public class source8 {




          public static void main(String[] a){
          JPanel contentPane = new JPanel();
          contentPane.setBackground(Color.WHITE);
          contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
          contentPane.setLayout(new javax.swing.BoxLayout(contentPane,  

          javax.swing.BoxLayout.Y_AXIS));

          JPanel aggregates = new JPanel((LayoutManager) new Insets(40, 0,0,
          0));
          GridBagConstraints gbc_aggregates = new GridBagConstraints(0, 2, 
          1, 1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(10, 10, 10, 
          10), 0, 0);
          GridBagLayout gbl_panel = new GridBagLayout();    

          gbl_panel.setConstraints(aggregates, gbc_aggregates);

          GridBagLayout gbl_agregats = new GridBagLayout();   
          addParametersLayout(gbl_agregats, new int[]{0,0,0}, new int[]{0, 
          0},new double[]{ 0.0, 0.0, Double.MIN_VALUE}, new double[]{0.0,
          Double.MIN_VALUE} );
          aggregates.setLayout(gbl_agregats);

          JTable table = new JTable(new MyTableModel(null));
          table.setPreferredSize(new Dimension(850, Const.rowHeight));

          JScrollPane scrollPane = new JScrollPane(table);
          table.setPreferredScrollableViewportSize(new Dimension(850, 10 
          *Const.rowHeight));
          GridBagConstraints gbc_scrollPane= new GridBagConstraints(0, 1, 3,
          1, 0.0, 0.0, GridBagConstraints.WEST, 1, new Insets(5, 5, 20, 5),
          0, 0);
          gbl_agregats.setConstraints(scrollPane, gbc_scrollPane);
          aggregates.add(scrollPane);

          JPanel main_panel = new JPanel();
          main_panel.add(aggregates); 

          JScrollPane scrPane = new JScrollPane(main_panel);
          scrPane.getVerticalScrollBar().setUnitIncrement(16);
          scrPane.setBorder(BorderFactory.createEmptyBorder());
          scrPane.getInsets().set(0, 0, 0, 0);
          contentPane.add(scrPane);
          setContentPane(contentPane);    
          }

          public static void addParametersLayout(GridBagLayout gbl,
          int[] columnWidths, int[] rowHeights, double[] columnWeights,
          double[] rowWeights) {
          gbl.columnWidths = columnWidths;
          gbl.rowHeights = rowHeights;
          gbl.columnWeights = columnWeights;
          gbl.rowWeights = rowWeights;
          } 

          private static void setContentPane(JPanel contentPane) {
          throw new UnsupportedOperationException("Not supported yet.");                
          //To change body of generated methods, choose Tools | Templates.
          }
           }

代码中的一些变化希望它适用于你§§thanks

答案 1 :(得分:0)

如果有人有同样的问题。我为LookAndFeel添加了默认的ScrollBar大小,它可以正常工作。

UIDefaults defaults = UIManager.getLookAndFeel().getDefaults();
defaults.put("ScrollBar.minimumThumbSize", new Dimension(30, 30));