gpt4 book ai didi

java - TreeItem javaFX 的不同行为

转载 作者:行者123 更新时间:2023-12-02 04:28:58 26 4
gpt4 key购买 nike

我正在 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-如何使节点不可编辑?

我真的需要区分它们,因为稍后每个叶子的 Action 都是不同的。谢谢。

最佳答案

您可以存储Object TreeView 中的 s 并通过 instance of 确定确切的类型.

1- How could I differentiate between them?

作者:instance of .

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

商店Object而不是String .

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

设置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 );
}

}

关于java - TreeItem javaFX 的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31752549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com