2011-12-15 54 views
0

我需要清除我的JPanel。這JPanel調用mainPane有一個GridLayout它包含JScrollPane's, and these JScrollPane s contain custom JPanel s that override paintComponent()`。刪除對象後即使重繪對象

無論我嘗試什麼 - 將列表設置爲null,用新對象替換舊實例 - 只要我調整窗口框架的大小,視圖的paintComponent() get被調用,並且它再次在mainPane內繪製視圖。

public void createContentPane() 
    { 
     gridLay = new GridLayout(GRID_ROWS, GRID_COLUMNS); 

     graphicViewList = new ArrayList<View>(); 
     for (int i = 0; i < graphicViewList.size(); i++) { 
      View v = graphicViewList.get(i); 
      v = null; 
     } 
     mainPane = new JPanel(); 

     clearGrid(); 
     mainPane.removeAll(); 
     mainPane.validate(); 
     graphicViewList.clear(); 
     imageModel.deleteObservers(); 
     mainPane.setLayout(gridLay); 
     this.getContentPane().remove(mainPane); 
     this.getContentPane().add(mainPane, BorderLayout.CENTER); 
     mainPane.revalidate(); 
     mainPane.repaint(); 

    } 

    public void createViews() 
    { 
     int idx = 0; 
     graphicViewList.clear(); 

     while(idx < NUM_PERSPECTIVE) //iterator? 
     { 
      if(idx == 0) 
      { 
       graphicViewList.add(new ThumbnailView(this, imageModel)); 
       mainPane.add(new JScrollPane(graphicViewList.get(idx))); 
       idx++; 
       continue; 
      } 
      graphicViewList.add(new ImageView(this, imageModel)); 
      mainPane.add(new JScrollPane(graphicViewList.get(idx))); 
      idx++; 
     } 

     for (int i = 0; i < graphicViewList.size(); i++) 
      graphicViewList.get(i).removeFocus(); 

     this.getContentPane().add(mainPane, BorderLayout.CENTER); 
    } 

    private void clearGrid() 
    { 
     for (int i = 0; i < mainPane.getComponentCount(); i++) { 
      JScrollPane sP =(JScrollPane) mainPane.getComponent(i); 
      sP = null; 

     } 
    } 

createContentPane()是矯枉過正我知道,我只是絕望。所以基本上第一個函數和第二個函數在GUI構造函數中被調用。當我撥打電話createContentPane()來替換舊的UI(具有相同的結構,只是不同的內容)時,只要我調整容器的大小,就會重新繪製內容。唯一的區別是在第二個createContentPane()調用調整大小繪製繪製佈局和元素不在裏面了。

我認爲createContentPane()會清空所有內容並刪除我可能擁有的所有引用,但paintComponent()仍然可以使用視圖繪製滾動條。

+0

爲了更好地幫助您,請發佈[SSCCE](http://sscce.org/)。 – 2011-12-15 01:31:14

回答

2

在clearGrid(),你需要調用remove(Component comp)(在你的情況下,SP)或removeAll();

2

在你clearGrid方法,你是不是刪除從面板任何東西;您只是獲取mainPane內部的引用,然後將該引用設置爲null。相反,您需要爲每個滾動窗格實際調用mainPane.remove(sP);

此外,而不是該循環,您可以嘗試撥打mainPane.removeAll();。請注意,正如文檔中所述,您必須事後重新驗證面板。

Container.removeAll


下面是SSCCE安德魯·湯普森前面提到的。在提出問題時,請嘗試做這件事,以便人們可以運行它並幫助解決問題。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class PanelClearerExample { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       PanelClearerExample app = new PanelClearerExample(); 
       app.startUp(); 
      } 
     }); 
    } 

    private final String CLEAR_COMMAND = "Clear"; 
    private final String REBUILD_COMMAND = "Rebuild"; 
    private JFrame controlFrame; 
    private int displayCount = 0; 
    private JFrame displayFrame; 
    private JPanel displayPanel = new JPanel(); 

    private void startUp() { 
     displayFrame = new JFrame("Display"); 
     displayFrame.setSize(300, 300); 
     displayFrame.setLocation(300, 0); 
     displayFrame.add(buildDisplay(), BorderLayout.CENTER); 

     controlFrame = new JFrame("Control"); 
     controlFrame.setSize(200, 100); 
     controlFrame.add(buildControl(), BorderLayout.CENTER); 
     controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     displayFrame.setVisible(true); 
     controlFrame.setVisible(true); 
    } 

    private Component buildDisplay() { 
     populateDisplay(); 
     return displayPanel; 
    } 

    private void populateDisplay() { 
     displayCount++; 
     displayPanel.setLayout(new GridLayout(2, 2)); 
     String text = "Display " + displayCount; 
     for (int i = 0; i < 4; i++) { 
      displayPanel.add(new JLabel(text, JLabel.CENTER)); 
     } 
    } 

    private Component buildControl() { 
     JPanel p = new JPanel(new BorderLayout()); 
     final JButton button = new JButton(CLEAR_COMMAND); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       if (CLEAR_COMMAND.equals(ae.getActionCommand())) { 
        clearDisplay(); 
        button.setText(REBUILD_COMMAND); 
        button.setActionCommand(REBUILD_COMMAND); 

       } else { 
        repopulateDisplay(); 
        button.setText(CLEAR_COMMAND); 
        button.setActionCommand(CLEAR_COMMAND); 
       } 
      } 
     }); 
     p.add(button); 

     return p; 
    } 

    private void clearDisplay() { 
     displayPanel.removeAll(); 
     displayPanel.repaint(); 
    } 

    private void repopulateDisplay() { 
     populateDisplay(); 
     displayPanel.revalidate(); 
    } 
} 
+0

我打電話給mainPane.removeAll()稍微低一點?我會嘗試每次調用remove(sP)。 – 2011-12-15 01:24:49