簡體   English   中英

將Java 7升級到Java 8后,不顯示帶有JTable的JScrollPane中的滾動

[英]Doesn't show scroll in JScrollPane with JTable after Upgrade Java 7 to Java 8

我必須將項目升級到Java8。運行應用程序后,從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中相同的滾動看起來還可以?

編輯:

也許我不太具體。 當我編譯項目時,我看到類似的內容(只是條):

在此處輸入圖片說明

我可以滾動表格,因為它可以正常工作,但是它是不可見的(我只看到一個條)。

          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.
          }
           }

某些代碼更改希望它對您有用§§謝謝

如果有人有同樣的問題。 我為LookAndFeel添加了默認的ScrollBar大小,它可以正常工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM