javafx - Customizing TreeCells using cell factory -


I am trying to represent TreeCell like this:

I have tried to read on cell factories and I know that I have to use this functionality but how do I set such graphics for all trees of trees? The image is stored in .fxml-file as an HBox.

Thank you very much :)

PS is not necessarily looking for code in response, this is a general description of how to do it or why it does not work.

The code I tried to have is the fxml file in the same folder as the file.

This is the error code I received:

Exception in the thread "JavaFX application thread" java.lang.NullPointerException: Location is required.

  @ Override public zero update item (string item, boolean empty) {super.updateItem (item, empty); {This.hBox = (HBox) FXML Loader Load (GetClass (.) GetResource ("/view / TreeCell.fxml")); } Hold (IOException e) {System.out.println ("this does not work"); E.printStackTrace (); } If (item! = Null) {set graphic (this.hBox); } Other {setgraphic (empty); }}  

The exception that you are receiving means that FXMLLoader < / Code> You can not find the specified Fax file if the FxML file is in the same package as the current class, then you

  this.hBox = (HBox) FXMLLoader.load (getClass (). GetResource ("TreeCell should be able to use .fxml"));  

If you start the resource path with a major / , then it will be interpreted relative to the classpath.

The probability of display is very poor using the code you show. When using cell factories, the cells are rarely born, but their updateItem (...) methods often (especially during sharp scrolling, or during the expansion and collapse of tree nodes ) Can be called.

Instead, when you create a cell, you can read the file once, and as a result, you can reuse the HBox Code> in updateItem () method:

  tree.setCellFactory (triview -> try {hbox hbox; {hbox = (HBox) FXMLLoader.load (GetClass () .getResource ("TreeCell.fxml"));} Catch (Exception Act) {New Runtime Expansion (Exposure);} Return to New Trecel; String & gt; () {@Override Public Zero Update Item (string) Items, boolean empty) {Super.updateItem (items, r ); If configured with graphic (item == faucet) {setGraphic (null);} else {// cell data etc ... setGraphic (hbox);}}};});  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -