1

Possible Duplicate:
When does one need to call revalidate() on a swing component to make it refresh, and when not?

i am making a Explorer program in Java it works as follows
i input the path from the user, a textbox after the user presses enter the path is set and the list is computed of the folder and corresponding labels are created which are supposed to be displayed on the window
but the application doesn't display the new contents of the folder, if i change the text then it directs to a new directory so when i press enter then it must show the contents of the new directory loaded but only after resizing the window does it show the new contents of the frame

the code is as follows

package explorer;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Explorer extends JFrame{
    File file;
    Scanner scan;
    String path;
    String [] listOfFiles;
    JTextField txtPath;
    JLabel lblLocation;
    JLabel child[];
    JPanel childPanel;
    JPanel masterPanel;

       public Explorer(){
      lblLocation = new JLabel("Location: ");
      /*
       * the declaration of panels
       */
          masterPanel = new JPanel();
          childPanel = new JPanel();
      JPanel panel = new JPanel();

      /*declaration of other components*/

      txtPath = new JTextField("",20);
      /*addition of components to panel for layout*/
      panel.add(lblLocation);
      panel.add(txtPath);
      /*adding to master panel, for sophisticated layout*/
      masterPanel.add(panel, BorderLayout.NORTH);
      masterPanel.add(childPanel, BorderLayout.SOUTH);  

      getContentPane().add(masterPanel);
      /*this place from where address is fetched like /home/revolution/Desktop etc on ubuntu*/

      txtPath.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ev){
            childPanel.removeAll();
        path = new String(txtPath.getText());//the absolute path
        file = new File(path);
        File childFiles[];
        String name = path.substring(path.lastIndexOf('/')+1, path.length());//the name of the directory being displayed
        setTitle(name);

          if(!file.isDirectory()){
            JOptionPane.showMessageDialog(null, "Error file is not a directory");
          } else {
            listOfFiles = file.list();
            child = new JLabel[listOfFiles.length];// labels equal to the number fo files and with name of the coresponding file/folder

            childFiles = new File[listOfFiles.length];//references to files
            childPanel.setLayout(new GridLayout(listOfFiles.length/2,listOfFiles.length/2));//setting grid layout

            for(int i=0; i<listOfFiles.length;i++){
                childFiles[i] = new File(listOfFiles[i]);
                child[i] = new JLabel(listOfFiles[i]);
                child[i].setToolTipText(childFiles[i].isFile()?"File":"Folder");
                childPanel.add(child[i]);
            }
          childPanel.setVisible(true);  
          }

        }
      });
}   
}

what is wrong? how can i 'refresh' the contents of the window??

Community
  • 1
  • 1
thewhitetulip
  • 3,235
  • 3
  • 21
  • 26

1 Answers1

1

I think you need to revalidate() the panel.

More precisely at the end of your action listener you can add childPanel.revalidate();

      childPanel.setVisible(true);  
      }
    }
    childPanel.revalidate();
});
Nitin Chhajer
  • 2,299
  • 1
  • 24
  • 35