我在JavaFX中使用treeview,我想知道如何区分类型的节点,我的意思是,我有下一个结构:
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
...
这只是树的样子的一个例子,但问题是每个代理,用例图和模型都有不同的ContextMenu。我的困惑是:例如,UseCaseDiagram和模型是同一节点的子节点,所以:
1-我如何区分它们?
2-有没有办法可以将字符串附加到每个节点以保存类型?
3-如何使节点不可编辑?
我真的需要区分它们,因为后来每个叶子的动作都不同。 感谢。
答案 0 :(得分:2)
您可以在TreeView中存储Object
并按instance of
确定确切的类型。
1-我如何区分它们?
instance of
。
2-有没有办法可以将字符串附加到每个节点进行保存 类型?
存储Object
而不是String
。
3-如何使节点不可编辑?
设置treeView.setEditable( false )
并从您的自定义startEdit()
中删除cancelEdit()
,TreeCell
个方法。
请参阅以下示例代码:
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 );
}
}