title: JavaFX disable TreeView collapsing
body: <p>In JavaFX when I use a TreeView, whenever I click on the main menus (which have submenus) sometimes they "stuck" and don't show me their submenus.</p>
  
  <p>In 10 clicks seven is okay, 3 is not. I observed, that the problem shows up only, if I click to another main menu that I am in. For example:</p>
  
  <pre><code>&gt;File
     New File
     Open File
     Close
  &gt;Tools
     Settings
  </code></pre>
  
  <p>So if I click: File, it shows me it's submenus (New, Open, Close). If I click it again, it closes the submenus. <strong>BUT!</strong></p>
  
  <p>When I click to another submenu, just like "Settings" and after I click to File (to expand, or collapse it's submenus) I get this error message:</p>
  
  <pre><code>java.lang.NullPointerException
  at utiokos_program.FoablakController$1.changed(FoablakController.java:166)
  at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:196)
  at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
  at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:195)
  at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:161)
  at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
  at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
  at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:101)
  at javafx.scene.control.MultipleSelectionModelBase$1.invalidated(MultipleSelectionModelBase.java:65)
  at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
  at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
  at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:195)
  at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:161)
  at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:130)
  at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:163)
  at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:67)
  at javafx.scene.control.MultipleSelectionModelBase.shiftSelection(MultipleSelectionModelBase.java:253)
  at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1114)
  at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1059)
  at com.sun.javafx.scene.control.WeakEventHandler.handle(WeakEventHandler.java:62)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
  at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
  at javafx.event.Event.fireEvent(Event.java:171)
  at javafx.scene.control.TreeItem.fireEvent(TreeItem.java:713)
  at javafx.scene.control.TreeItem.access$300(TreeItem.java:197)
  at javafx.scene.control.TreeItem$5.invalidated(TreeItem.java:529)
  at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127)
  at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161)
  at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:81)
  at javafx.scene.control.TreeItem.setExpanded(TreeItem.java:509)
  at com.sun.javafx.scene.control.behavior.TreeCellBehavior.doSelect(TreeCellBehavior.java:192)
  at com.sun.javafx.scene.control.behavior.TreeCellBehavior.mousePressed(TreeCellBehavior.java:122)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:335)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
  at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
  at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
  at javafx.event.Event.fireEvent(Event.java:171)
  at javafx.scene.Scene$MouseHandler.process(Scene.java:3369)
  at javafx.scene.Scene$MouseHandler.process(Scene.java:3209)
  at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3164)
  at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1582)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2267)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
  at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
  at com.sun.glass.ui.View.notifyMouse(View.java:924)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
  at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
  at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>After I click it again, and it is okay, it shows me it's subs again! (Since now the File was the last I clicked and not the Settings)</p>
  
  <p><strong>//One possible solution would be to disable the treeview's collapse, and then I use it everytime in expanded mode. Is it possible to disable collapsing in JavaFX Treeview?</strong></p>
  
  <p>//I copy the whole FoablakController.java on request</p>
  
  <pre><code>import java.net.URL;
  import java.util.ResourceBundle;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.application.Platform;
  import javafx.scene.layout.StackPane;
  import javafx.scene.image.Image;
  import javafx.scene.image.ImageView;
  import javafx.scene.Node;
  import javafx.scene.control.TreeItem;
  import javafx.scene.control.TreeView;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.event.Event;
  import javafx.event.EventDispatchChain;
  import javafx.event.EventDispatcher;
  import javafx.scene.control.Button;
  import javafx.scene.control.TextArea;
  import javafx.scene.input.ContextMenuEvent;
  import javafx.scene.input.KeyCode;
  import javafx.scene.input.KeyEvent;
  import javafx.scene.input.MouseEvent;
  
  public class FoablakController implements Initializable {
  
       public Windows operation = null;    
  
          private final Node fileicon =  new ImageView(new Image(getClass().getResourceAsStream("file.png")));
          private final Node editicon =  new ImageView(new Image(getClass().getResourceAsStream("edit.png")));
          private final Node windowsicon =  new ImageView(new Image(getClass().getResourceAsStream("windows.png")));
          private final Node toolsicon =  new ImageView(new Image(getClass().getResourceAsStream("tools.png")));
          private final Node helpicon =  new ImageView(new Image(getClass().getResourceAsStream("help.png")));
  
  
      @FXML 
      public static StackPane menuholder;
      @FXML 
      public static StackPane main_right;
      @FXML
      public static StackPane help_right;
      @FXML
      public static StackPane contact_right;
      @FXML
      public static StackPane settings_right;
      @FXML
      public static StackPane contrast_right;
      @FXML
      public static StackPane comments_right;
      @FXML
      public static StackPane nice_right;
      @FXML
      public static StackPane faq_right;
      @FXML 
      public TextArea summary;    
      @FXML 
      public static TextArea textArea_help_summary;  
      @FXML 
      public Button btn_print;        
  
  
      @FXML
      public void Quitgomb (ActionEvent event) {
           Platform.exit();
            }
  
  
  
      public void ClearScreen(){
        main_right.setVisible(false);
        help_right.setVisible(false);
        settings_right.setVisible(false);
        contrast_right.setVisible(false);
        contact_right.setVisible(false);
        comments_right.setVisible(false);
        nice_right.setVisible(false);
        faq_right.setVisible(false);
      }
  
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
  
          TreeItem&lt;String&gt; treeItemRoot1 = new TreeItem&lt;&gt; ("Menu");
  
          TreeItem&lt;String&gt; nodeItemA = new TreeItem&lt;&gt;("File",fileicon);
          TreeItem&lt;String&gt; nodeItemB = new TreeItem&lt;&gt;("Edit",editicon);
          TreeItem&lt;String&gt; nodeItemC = new TreeItem&lt;&gt;("Windows",windowsicon);
          TreeItem&lt;String&gt; nodeItemD = new TreeItem&lt;&gt;("Tools",toolsicon);
          TreeItem&lt;String&gt; nodeItemE = new TreeItem&lt;&gt;("Help",helpicon);
          treeItemRoot1.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC,nodeItemD,nodeItemE);
  
          nodeItemA.setExpanded(true);
          nodeItemB.setExpanded(true);
          nodeItemC.setExpanded(true);
          nodeItemD.setExpanded(true);
          nodeItemE.setExpanded(true);
  
          TreeItem&lt;String&gt; nodeItemA1 = new TreeItem&lt;&gt;("New File");
          TreeItem&lt;String&gt; nodeItemA2 = new TreeItem&lt;&gt;("Open");
          TreeItem&lt;String&gt; nodeItemA3 = new TreeItem&lt;&gt;("Quit");
          nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3);
  
          TreeItem&lt;String&gt; nodeItemB1 = new TreeItem&lt;&gt;("Edit A");
          TreeItem&lt;String&gt; nodeItemB2 = new TreeItem&lt;&gt;("Edit B");
          TreeItem&lt;String&gt; nodeItemB3 = new TreeItem&lt;&gt;("Edit C");
          TreeItem&lt;String&gt; nodeItemB4 = new TreeItem&lt;&gt;("Edit D");
          TreeItem&lt;String&gt; nodeItemB5 = new TreeItem&lt;&gt;("Edit E");
          TreeItem&lt;String&gt; nodeItemB6 = new TreeItem&lt;&gt;("Edit F");
          nodeItemB.getChildren().addAll(nodeItemB1, nodeItemB2, nodeItemB3, nodeItemB4, nodeItemB5, nodeItemB6);
  
          TreeItem&lt;String&gt; nodeItemC1 = new TreeItem&lt;&gt;("Windows Settings");
          TreeItem&lt;String&gt; nodeItemC2 = new TreeItem&lt;&gt;("Windows Brightness");
          TreeItem&lt;String&gt; nodeItemC3 = new TreeItem&lt;&gt;("Windows Quit");
          nodeItemC.getChildren().addAll(nodeItemC1, nodeItemC2, nodeItemC3);
  
          TreeItem&lt;String&gt; nodeItemD1 = new TreeItem&lt;&gt;("Settings");
          TreeItem&lt;String&gt; nodeItemD2 = new TreeItem&lt;&gt;("Print");
          nodeItemD.getChildren().addAll(nodeItemD1, nodeItemD2);
  
          TreeItem&lt;String&gt; nodeItemE1 = new TreeItem&lt;&gt;("Help");
          TreeItem&lt;String&gt; nodeItemE2 = new TreeItem&lt;&gt;("FAQ");
          nodeItemE.getChildren().addAll(nodeItemE1, nodeItemE2);
  
  
          TreeView&lt;String&gt; treeView = new TreeView&lt;&gt;(treeItemRoot1);
                     treeView.setShowRoot(false);
                     menuholder.getChildren().add(treeView);
  
  
  //    
          treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
  
          public void changed(ObservableValue observable, Object oldValue,
                  Object newValue) {
  
  
          operation = new Windows();
          TreeItem&lt;String&gt; selectedItem = (TreeItem&lt;String&gt;) newValue;
  
                           String actualmenu;
              **actualmenu = selectedItem.getValue();**
  
  
                    if (null != actualmenu) {
                        switch (actualmenu) {
                            case "File":
                                try{
                                selectedItem.setExpanded(true);
                                 }
                               catch (Exception ex) {
                                    Logger.getLogger(FoablakController.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                                break;
                            case "Edit":
                                selectedItem.setExpanded(true);
                                break;
                            case "Windows":
                                selectedItem.setExpanded(true);
                                break;
                            case "Tools":
                                selectedItem.setExpanded(true);
                                break;
                            case "Help":
                                selectedItem.setExpanded(true);
                                break;
                            case "New File":
                                try {
                                    menuholder.setDisable(true);
                                    textArea_help_summary.setDisable(true);
                                    operation.first();
                                } catch (Exception ex) {
                                    Logger.getLogger(FoablakController.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                break;
                            case "Open":
                                System.out.println("OK");
                                break;
                            case "Quit":
                                System.out.println("OK");
                                break;
                            case "Edit A":
                                System.out.println("OK");
                                break;
                            case "Edit B":
                                ClearScreen();
                                contrast_right.setVisible(true);
                            case "Edit C":
                                break;
                            case "Edit D":
                              System.out.println("OK");
                                break;
                            case "Edit E":
                                System.out.println("OK");
                                break;
                           }
          }
                              }
  
        });
  
              }
  
  
  
  
  
  
  }
  </code></pre>