java - File Searcher: how to work with JFrame while main Thread Execute? -


this first post, if wrong, i'll try fix it. test task dealing make file searcher ability navigate text, etc. finished part except 1 thing: application needs allow user work while searching files in directories.

i've read lot multithreading, still can't answer how this... code executes when user presses "choose folder" in jframe("gui" class):

private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                              defaulttreemodel model = (defaulttreemodel) jtree1.getmodel();     defaultmutabletreenode root = (defaultmutabletreenode) model.getroot();     int j = jtree1.getrowcount() - 1;     final jfilechooser fc = new jfilechooser();     filenameextensionfilter filter = new filenameextensionfilter(jcombobox1.getselecteditem().tostring() + " files", jcombobox1.getselecteditem().tostring());     fc.setfilefilter(filter);     fc.setfileselectionmode(jfilechooser.directories_only);     fc.setdialogtitle("choose folder fing ." + jcombobox1.getselecteditem().tostring() + " files");     jlabel4.settext("searching files in folder, please wait");     if (fc.showopendialog(jbutton3) == jfilechooser.approve_option) {         root.setuserobject("your root");         jtextarea1.settext("");         jtextfield1.settext("");         if (jtree1.getrowcount() > 0) {             (int = 0; < j; i++) {                 model.removenodefromparent((mutabletreenode) model.getchild(root, 0));             }         }         long starttime = system.currenttimemillis();         file f2 = new file(fc.getselectedfile().getabsolutepath());         string name2 = "." + jcombobox1.getselecteditem().tostring();         //filenamefilter filter1 = (file dir, string name1) -> name1.endswith(name2);         arraylist<file> files = new arraylist<>();         new newТhread();         listf(f2, files, name2);         root.setuserobject(f2);         model.nodechanged(root);         (int = 0; < files.size(); i++) {             defaultmutabletreenode child = new defaultmutabletreenode(files.get(i).getabsolutepath().replace(f2.tostring(), ""));             root.add(child);             model.reload();             jlabel4.settext("done! type text search , press \"search files\"");         }         system.out.println("time used getting files: " + (system.currenttimemillis() - starttime) / 1000 + "sec");         joptionpane.showmessagedialog(null, "directory opened:" + fc.getselectedfile().getabsolutepath());         system.out.println("main thread finished");     } else if (fc.showopendialog(jbutton3) == jfilechooser.cancel_option) {         jlabel4.settext("please, choose folder , extension find files");     } } 

listf method check subdirectories too:

    public arraylist<file> listf(file directory, arraylist<file> files, string extension) {     file[] flist = directory.listfiles();     (int = 0; < flist.length; i++) {         if (flist[i].isfile() && flist[i].getname().endswith(extension) == true) {             files.add(flist[i]);         } else if (flist[i].isdirectory()) {             listf(flist[i], files, extension);         }     }     return files; } 

newthread class still can't in. looks this:

class newТhread extends thread {  newТhread() {     super("demo");     start(); }  public synchronized void run() {     try {         (int = 3; > 0; i--) {             thread.sleep(500);             system.out.println(i);             gui gui = new gui();             gui.setvisible(true);         }     } catch (interruptedexception е) {         system.out.println("interrupted");     } } 

after 3-2-1 count should open new gui object (jframe) should allow make actions. elements of object gui gui frozen while main thread executes (and become unfrozen when search finishes). help, please?

you can start thread putting code, not require user's interaction, inside thread's run method shown below:

new thread() {     public void run()     {         // code not require user's interaction     } }.start(); 

in case code placed in run method, code written in "jbutton1actionperformed" action listener. :-)


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -