TreeItem javaFX的不同行为

编程入门 行业动态 更新时间:2024-10-20 05:34:29
本文介绍了TreeItem javaFX的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用JavaFX中的treeview,我想知道如何区分类型的节点,我的意思是,我具有下一个结构:

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 ...

这只是树的外观的一个示例,但事实是每个代理,用例图和模型都有不同的ContextMenu.我的困惑是:例如UseCaseDiagram和模型是同一节点的子代,所以:

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-我如何区分它们? 2-是否可以将字符串附加到每个节点以保存类型? 3-如何使节点不可编辑?

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.

推荐答案

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

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

1-我如何区分它们?

1- How could I differentiate between them?

by instance of.

2-有没有一种方法可以将字符串附加到每个节点以进行保存 类型?

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

存储Object而不是String.

3-如何使节点不可编辑?

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

设置treeView.setEditable( false )并从自定义TreeCell中删除startEdit()和cancelEdit()方法.

Set treeView.setEditable( false ) and remove startEdit(), cancelEdit() methods from your custom 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 ); } }

更多推荐

TreeItem javaFX的不同行为

本文发布于:2023-11-15 23:28:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TreeItem   javaFX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!