How to add the icon image into the CheckBoxTreeItem?


Question

I've problem with the TreeView component, It has the CheckBoxTreeItem to check enable or disable, it's very select and unselect with this reference ==> Using JavaFX UI Controls: Tree View | JavaFX 2 Tutorials and Documentation with Using Tree Cell Editors. This is the image with the tree view have CheckBoxTreeItem (http://docs.oracle.com/javafx/2/ui_controls/img/tree-view-checkbox1.png)

At now, I want to add the icon image beside the CheckBoxTreeItem (Its mean that we have the icon image beside the checkbox).

Could anyone help me this problem?

I saw that when I set like that tree.setCellFactory(CheckBoxTreeCell.forTreeView()); ==> It can not show the icon

This is my coding

package sample;  
import javafx.application.Application;  
import javafx.fxml.FXMLLoader;  
import javafx.scene.Node;  
import javafx.scene.Parent;  
import javafx.scene.Scene;  
import javafx.scene.control.CheckBoxTreeItem;  
import javafx.scene.control.TreeView;  
import javafx.scene.control.cell.CheckBoxTreeCell;  
import javafx.scene.image.Image;  
import javafx.scene.image.ImageView;  
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
public class Main extends Application {  
    @Override  
    public void start(Stage primaryStage) throws Exception{  
        primaryStage.setTitle("Tree View Sample");  
        Node graphic  = new ImageView(new Image("https://duke.kenai.com/iconSized/duke4.gif"));  
        CheckBoxTreeItem<String> rootItem =  
                new CheckBoxTreeItem<String>("View Source Files", graphic);  
        rootItem.setExpanded(true);  
        final TreeView tree = new TreeView(rootItem);  
        tree.setEditable(true);  
        tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());  
        for (int i = 0; i < 8; i++) {  
            final CheckBoxTreeItem<String> checkBoxTreeItem =  
                    new CheckBoxTreeItem<String>("Sample" + (i+1), graphic);  
            rootItem.getChildren().add(checkBoxTreeItem);  
        }  
        tree.setRoot(rootItem);  
        tree.setShowRoot(true);  
        StackPane root = new StackPane();  
        root.getChildren().add(tree);  
        primaryStage.setScene(new Scene(root, 300, 250));  
        primaryStage.show();  
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
}  

Thanks 4 your reading.

Tran Quoc Ung

1
0
10/17/2013 2:22:58 AM

CheckBoxTreeItem have a one object property we cannot use more than one object within it.

try this..

CheckBoxTreeItem<ImageView> chkbobj;
ImageView mv = new ImageView();
mv.setImage(new Image(getClass().getResourceStreamAs("abc.png")));
chkboobj.setGraphic(mv);
0
10/31/2013 11:48:57 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon