简体   繁体   中英

different behaviour for TreeItem javaFX

i'm working with treeview in JavaFX, I want to know how it is possible to differentiate nodes for a type, I mean, I have the next structure:

1.-Agent 
   1.1-Use Case Diagram
      1.1.1-UseCase1
      1.1.2-Description
      1.1.3-Activities Diagram
   1.2-UseCase2
      1.2.1-Description
      1.2.2-Activities Diagram
   1.3-Models
      1.3.1-AgentModel
      1.3.2-Services
      1.3.3-Comunication
2-Agent2
...

That is just an example of how the tree looks like, but the thing is that every agent, use case diagram and models have different ContextMenu. my confusion is: UseCaseDiagram and models for example, are children of the same node, so:

1- How could I differentiate between them?
2- Is there a way that I could attach a string to every node to save the type?
3- How can I make a node non-editable?

I really need to differentiate them because, later the action for the leafs are different for each. Thanks.

You may store Object s in TreeView and determine the exact type by instance of .

1- How could I differentiate between them?

by instance of .

2- Is there a way that I could attach a string to every node to save the type?

store Object instead of String .

3- How can I make a node non-editable?

Set treeView.setEditable( false ) and remove startEdit() , cancelEdit() methods from your custom TreeCell .

See the following example code:

public class App extends Application
{

    private final List<Country> countries = Arrays.<Country>asList(
            new Country( "Country 1" ),
            new Country( "Country 2" ),
            new Country( "Country 3" ),
            new Country( "Country 4" ),
            new Country( "Country 5" ) );


    @Override
    public void start( Stage stage )
    {
        TreeItem<Object> rootNode = new TreeItem<>( new Country( "Dummy Country that will not be shown" ) );
        rootNode.setExpanded( true );

        for ( Country country : countries )
        {
            TreeItem<Object> item = new TreeItem<>( country );
            item.getChildren().add( new TreeItem<>( new City( country.getName() + " - City 1" ) ) );
            item.getChildren().add( new TreeItem<>( new City( country.getName() + " - City 2" ) ) );
            rootNode.getChildren().add( item );
        }

        TreeView<Object> treeView = new TreeView<>( rootNode );
        treeView.setEditable( false );
        treeView.setShowRoot( false );
        treeView.setCellFactory( new Callback<TreeView<Object>, TreeCell<Object>>()
        {
            @Override
            public TreeCell<Object> call( TreeView<Object> p )
            {
                return new MyTreeCell();
            }
        } );

        final Scene scene = new Scene( new VBox( treeView ), 400, 300 );
        stage.setScene( scene );
        stage.show();
    }


    private final class MyTreeCell extends TreeCell<Object>
    {

        private final ContextMenu addMenu = new ContextMenu();
        private final MenuItem addMenuItem = new MenuItem();


        public MyTreeCell()
        {
            addMenu.getItems().add( addMenuItem );
        }


        @Override
        public void updateItem( Object item, boolean empty )
        {
            super.updateItem( item, empty );

            if ( empty )
            {
                setText( null );
            }
            else
            {
                setText( getDisplayText( item ) );
                addMenuItem.setText( getContextMenuText( item ) );
                setContextMenu( addMenu );
            }
            setGraphic( null );
        }
    }


    private String getDisplayText( Object item )
    {
        if ( item instanceof Country )
        {
            return (( Country ) item).getName();
        }
        else if ( item instanceof City )
        {
            return (( City ) item).getCode();
        }
        else
        {
            return null;
        }
    }


    private String getContextMenuText( Object item )
    {
        if ( item instanceof Country )
        {
            return "This is a Country";
        }
        else if ( item instanceof City )
        {
            return "This is a City";
        }
        else
        {
            return null;
        }
    }


    public static class Country
    {

        private final SimpleStringProperty name;


        private Country( String name )
        {
            this.name = new SimpleStringProperty( name );
        }


        public String getName()
        {
            return name.get();
        }


        public void setName( String fName )
        {
            name.set( fName );
        }
    }


    public static class City
    {

        private final SimpleStringProperty code;


        private City( String code )
        {
            this.code = new SimpleStringProperty( code );
        }


        public String getCode()
        {
            return code.get();
        }


        public void setCode( String newcode )
        {
            code.set( newcode );
        }
    }


    public static void main( String[] args )
    {
        launch( args );
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM