java - How do I make JTree stop cell editing, when either Focus Lost or Left Click occurs outside JTree Location? -


what want happen while editing jtree, want commit edit if click outside of edit box. tree.setinvokesstopcellediting(true); helped, if click somewhere in jtree, edit gets committed. if click outside of jtree, jtextpane edit won't commit. question: question: how make jtree stop cell editing, when focus lost? or when left click outside jtree location? there prefered way this?

note: in code below tried solve using focus listener annoymous inner class, , tried pseudocode whentreelosesfocus() { if( tree.isediting() ) tree.stopediting(); }

that doesn't work because on edit focus changes celleditor component within tree, , see edit box flash on , flash off quickly.

the solution doesn't have focus based, can left click outside jtree area.

ssce follows (btw if run code convienence both spacebar , f2 rename nodes.)

import java.awt.event.focusevent; import java.awt.event.focuslistener; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jsplitpane; import javax.swing.jtextpane; import javax.swing.jtree; import javax.swing.keystroke;  public class stackexchangequestion3 {       public static void main(string[] args){           jframe window = new jframe();      window.setdefaultcloseoperation(jframe.exit_on_close);      window.settitle("stack exchange question");      window.setsize(400,500);//variable parameters best      window.setvisible(true);                  jsplitpane split = new jsplitpane();      window.getrootpane().setcontentpane(split);      jscrollpane left = new jscrollpane();      jscrollpane right = new jscrollpane();      jtree tree = new jtree();//loads sample tree data      tree.seteditable(true);//select node press f2 edit (built in keybinding)      tree.getinputmap().put(keystroke.getkeystroke("space"), "startediting");//can edit space      tree.setinvokesstopcellediting(true);//this helps stop editing within focus of tree       jtextpane text = new jtextpane();      split.setleftcomponent(left);      split.setrightcomponent(right);      left.setviewportview(tree);      right.setviewportview(text);      split.setdividerlocation(200);       tree.addfocuslistener(new focuslistener(){          public void focusgained(focusevent e) {         }          public void focuslost(focusevent e) {          jtree tree = (jtree)e.getsource();          if( tree.isediting() ) tree.stopediting();          }      });//end addfocuslistener      }//end main } 

commentary reasoning on if 1 way preferred on other welcome. tips on how detect if mouse click outside of jtree/tips in right direction.

    import java.awt.component;     import java.awt.event.focusevent;     import java.awt.event.focuslistener;     import javax.swing.jframe;     import javax.swing.jscrollpane;     import javax.swing.jsplitpane;     import javax.swing.jtextpane;     import javax.swing.jtree;     import javax.swing.keystroke;     import javax.swing.tree.defaulttreecelleditor;     import javax.swing.tree.defaulttreecellrenderer;     import javax.swing.tree.treecellrenderer;       public class stackexchangequestion3ansa {           public static void main(string[] args){            new stackexchangequestion3ansa();                     }              stackexchangequestion3ansa(){          jframe window;              window = new jframe();          window.setdefaultcloseoperation(jframe.exit_on_close);          window.settitle("stack exchange answer");          window.setsize(400,500);//variable parameters best          window.setvisible(true);                       jsplitpane split = new jsplitpane();        window.setcontentpane(split);          jscrollpane left = new jscrollpane();          jscrollpane right = new jscrollpane();          jtree tree;          tree = new jtree();//loads sample tree data          tree.seteditable(true);//select node press f2 edit (built in keybinding)          tree.getinputmap().put(keystroke.getkeystroke("space"), "startediting");//can edit space          tree.setinvokesstopcellediting(true);//this helps stop editing within focus of tree          //even fix above line still needed           jtextpane text = new jtextpane();          split.setleftcomponent(left);          split.setrightcomponent(right);          left.setviewportview(tree);          right.setviewportview(text);          split.setdividerlocation(200);              tree.setcelleditor( new mytreecelleditor(tree, tree.getcellrenderer()) );     }//end constructor   private class mytreecelleditor extends defaulttreecelleditor {      public mytreecelleditor(jtree tree, treecellrenderer renderer) {                     super(tree, (defaulttreecellrenderer)renderer); //note: http://stackoverflow.com/questions/5031101/why-i-lose-the-focus-for-a-short-time-from-jtree-after-editing-a-node?rq=1  //andré mentions "the tree adds defaultcelleditor jtree hierarchy when editing starts. textfield gains focus."                     //it's not simple this.addfocuslistener()  //2 variables: editingcomponent , editingcontainer, inherited defaulttreecelleditor //the constructor doesn't initialize editingcomponent //it's null atm, can't add focuslistener it, (yet @ later time gain focus)     //focusowner (on edit): javax.swing.tree.defaulttreecelleditor$defaulttextfield //editingcomponent default defaulttextfield                             }              @override     public component gettreecelleditorcomponent(final jtree tree, object value, boolean isselected, boolean expanded, boolean leaf, int row) {        component container = super.gettreecelleditorcomponent(tree, value, isselected, expanded, leaf, row); //note: system.out.println("components type: "+containeractually.getclass().getname()); //was used show container, javax.swing.tree.defaulttreecelleditor$editorcontainer  //gettreecelleditorcomponent(parameters) called editing begins //also @ time editingcomponent != null (aka initialized) //so it's place add in focus listener  editingcomponent.addfocuslistener( new focuslistener(){                 @override  public void focusgained(focusevent e) { }                 @override  public void focuslost(focusevent e) {                 tree.stopediting();}                  } );  //editorcontainer responsible displaying editingcomponent //so added focuslistener, after editingcomponent initialized, , before it's used //(i think return statement means it's used)         return container;         } }//end mytreecelleditor  }//end class          

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -