2017-04-04 109 views
0

所以我不知道它是不是工作,或者我希望它以不同的方式工作,但無論如何。ScrollPane自動調整內容水平不滾動不起作用

我有一個org.openide.windows.TopComponent它有三個不同項目的列表。這個列表可以通過點擊它們的名字來隱藏,因此高度是可變的,取決於哪些是可見的。如果列表總高度高於應顯示滾動條的TopComponent高度。如果寬度更改,列表中的元素應重新排列,以便列表使用可用的整個寬度。所以我也跟着做法是:

的TopComponent

JScrollPane的內部(給我,我需要滾動)。只有垂直滾動。

內部JPanel。

在三個列表(具有JList的JScrollPanes)內使用它們各自的JLabel單擊並隱藏內容。

所以,如果我遵循這種模式,一切看起來不錯,當我改變寬度使其更大時,列表中的元素重新排列以使用新的寬度。但是,當我嘗試再次將它縮小到原始大小時,列表會保持原樣並隱藏一些元素(沒有足夠的空間),就像有一個滾動(水平)一樣。但沒有!但是,如果我把外滾動面板和我做更改寬度名單調整完全正常(包括當我讓更廣泛和更窄),但我沒有垂直滾動,我需要......

public class ViewPalettePanel extends TopComponent { 

    fcp = new PalettePanel(); 
    scroll = new JScrollPane(fcp, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = 1; 
    gbc.gridy = 1; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.weightx = 1; 
    gbc.weighty = 1; 
    add(scroll, gbc); 
} 

public class PalettePanel extends javax.swing.JPanel { 

    public PalettePanel() { 
     initComponents(); 
    } 

    private void initComponents() { 
     java.awt.GridBagConstraints gridBagConstraints; 

     lbl1 = new javax.swing.JLabel(); 
     scPnl1 = new javax.swing.JScrollPane(); 
     lbl2 = new javax.swing.JLabel(); 
     scPnl2 = new javax.swing.JScrollPane(); 
     lbl3 = new javax.swing.JLabel(); 
     scPnl3 = new javax.swing.JScrollPane(); 
     lblPadding = new javax.swing.JLabel(); 

     setLayout(new java.awt.GridBagLayout()); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3); 
     add(lbl1, gridBagConstraints); 

     scPnl1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scPnl1.setMinimumSize(new java.awt.Dimension(60, 60)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 1; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3); 
     add(scPnl1, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 2; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2); 
     add(lbl2, gridBagConstraints); 

     scPnl2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scPnl2.setMinimumSize(new java.awt.Dimension(60, 60)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 3; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3); 
     add(scPnl2, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 4; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3); 
     add(lbl3, gridBagConstraints); 

     scPnl3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scPnl3.setMinimumSize(new java.awt.Dimension(60, 60)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 5; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3); 
     add(scPnl3, gridBagConstraints); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 6; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 
     gridBagConstraints.weighty = 1.0; 
     add(lblPadding, gridBagConstraints); 
    } 
} 

這是我使用的代碼(或多或少都是完整的)用於視覺部分。如果我更改添加(滾動,gbc)是添加(fcp,gbc)我想要的大小調整,但不是滾動。

任何想法?

+1

爲了更好地幫助越早,發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

回答

0

我發現,添加這它的工作原理,但我不知道是否會有任何副作用JScrollPane的內JPanel的......(對我的作品罰款雖然)

@Override 
public java.awt.Dimension getPreferredSize() { 

    int h = super.getPreferredSize().height; 
    int w = getParent().getSize().width; 
    return new java.awt.Dimension(w, h); 
}