0
我已经写在Java GUI应用程序递归搜索方法找到一个驱动器中的文件。 UI响应, 搜索成功发生,但JList的不填充,而控制台打印成功的文件名,经过3次点击文件是在JList中添加,但每个文件递归searchfile方法点击
DefaultListModel lm=new DefaultListModel();
public void search(String path){
File root = new File(path);
File[] list=root.listFiles();
if(list==null){
return;
}
for(File f: list){
if(f.isDirectory()){
if(list==null){
return;
}
search(f.getAbsolutePath());
}
else{
if(f.getName().endsWith(".txt") && f.getName().startsWith("abc")){
lm.addElement(f.getName());
System.out.println(f.getName());
found=true;
}
}
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
jList1.setModel(lm);
}
private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {
//just added this code in my program to resolve unresponsive UI
Thread t=new Thread(new Runnable() {
@Override
public void run() {
search("c:\\");
}
});
t.start();
}
日Thnx ...,它解决了反应迟钝UI概率LEM但JList的仍然是空的..... –
@ShaheryarBhatti的代码更新您的问题或创建一个新的答案,在这里发布的链接作为注释 –
http://stackoverflow.com/questions/31010273/jlist-populates -after-3-clicks-on-searchbtn-with-repeatitve-file-names –