Question

Hi I have a JPanel which has many JPanel inside. When the user changed data in the inside panel. I need to refresh the outerpanel; I removed all insider panel and add the new inside panel. My problem is that there are no inside panel showing after refresh. If I make the JFrame minimize and then maximize, it shows the refresh panel. Would someone tell me how to solve this problem. Thanks in advance.

There is my code to remove and add the new JPanel

private void getListCommentPane(){

      //sortPage

     Component[] components = jpListCommentPane.getComponents();
        for (Component component : components) {
            jpListCommentPane.remove(component);
            jpListCommentPane.validate();
        }

      ArrayList<CommentItem>sortComment= lstComment;
     Collections.sort(sortComment,CommentItem.sortPage);
     for(CommentItem comm: sortComment){
                    //The class DivCommentJPane extends JPanel
         DivCommentJPane d=new DivCommentJPane(comm, this);
         jpListCommentPane.add(d);

     }  

     jpListCommentPane.repaint();

 }
Was it helpful?

Solution

After removing and adding your components to jpListCommentPane, try calling jpListCommentPane.revalidate() instead of jpListCommentPane.repaint(), do this last, do it once. There should be no need to call jpListCommentPane.validate(); and especially from within a loop

revalidate will instruct the container that it needs to perform a layout and update its contact hierarchy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top