我想开发一个简单的例子,我可以选择带按钮的TreeView节点(在某些情况下点击图像等)。我创建了这个简单的示例:
package click.treenodeclicktest;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class MainApp extends Application
{
@Override
public void start(Stage stage) throws Exception
{
HBox hb = new HBox(initTree(), initPanel());
Scene scene = new Scene(hb, 800, 800);
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
public TreeView<String> initTree()
{
TreeItem<String> treeItemRoot = new TreeItem<>("Root");
TreeItem<String> nodeItemA = new TreeItem<>("Item A");
TreeItem<String> nodeItemB = new TreeItem<>("Item B");
TreeItem<String> nodeItemC = new TreeItem<>("Item C");
treeItemRoot.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC);
TreeItem<String> nodeItemA1 = new TreeItem<>("Item A1");
TreeItem<String> nodeItemA2 = new TreeItem<>("Item A2");
TreeItem<String> nodeItemA3 = new TreeItem<>("Item A3");
nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3);
TreeView<String> treeView = new TreeView<>(treeItemRoot);
return treeView;
}
public HBox initPanel()
{
Button bt1 = new Button("Item A1");
Button bt2 = new Button("Item A2");
Button bt3 = new Button("Item A3");
HBox hb = new HBox(bt1, bt2, bt3);
hb.setPadding(new Insets(80, 80, 80, 80));
return hb;
}
public static void main(String[] args)
{
launch(args);
}
}
问题是当我点击按钮时如何选择树节点。例如,当我点击按钮Item A1
时,我想选择树同上Item A1
。
答案 0 :(得分:1)
您的Button需要对treeView的引用以及对要选择的对象的引用。所以你不得不让它们可用。然后你可以打电话:
bt1.setOnAction( new EventHandler<ActionEvent>()
{
@Override
public void handle( final ActionEvent event )
{
treeView.getSelectionModel().select( nodeItemA1 );
}
} );
<强> - 编辑 - 强>
我接受了您的代码并对其进行了修改,以满足您的请求,但请记住,可能不希望将所有DataObjects定义为您班级中的私有成员。它只是为了说明如何选择节点:
public class MainApp extends Application
{
private TreeView<String> treeView;
private TreeItem<String> treeItemRoot;
private TreeItem<String> nodeItemA;
private TreeItem<String> nodeItemB;
private TreeItem<String> nodeItemC;
private TreeItem<String> nodeItemA1;
private TreeItem<String> nodeItemA2;
private TreeItem<String> nodeItemA3;
@Override
public void start( final Stage stage ) throws Exception
{
HBox hb = new HBox( initTree(), initPanel() );
Scene scene = new Scene( hb, 800, 800 );
stage.setTitle( "JavaFX and Maven" );
stage.setScene( scene );
stage.show();
}
public TreeView<String> initTree()
{
treeItemRoot = new TreeItem<>( "Root" );
nodeItemA = new TreeItem<>( "Item A" );
nodeItemB = new TreeItem<>( "Item B" );
nodeItemC = new TreeItem<>( "Item C" );
treeItemRoot.getChildren().addAll( nodeItemA, nodeItemB, nodeItemC );
nodeItemA1 = new TreeItem<>( "Item A1" );
nodeItemA2 = new TreeItem<>( "Item A2" );
nodeItemA3 = new TreeItem<>( "Item A3" );
nodeItemA.getChildren().addAll( nodeItemA1, nodeItemA2, nodeItemA3 );
treeView = new TreeView<>( treeItemRoot );
return treeView;
}
public HBox initPanel()
{
Button bt1 = new Button( "Item A1" );
bt1.setOnAction( new EventHandler<ActionEvent>()
{
@Override
public void handle( final ActionEvent event )
{
treeView.getSelectionModel().select( nodeItemA1 );
}
} );
Button bt2 = new Button( "Item A2" );
bt2.setOnAction( new EventHandler<ActionEvent>()
{
@Override
public void handle( final ActionEvent event )
{
treeView.getSelectionModel().select( nodeItemA2 );
}
} );
Button bt3 = new Button( "Item A3" );
bt3.setOnAction( new EventHandler<ActionEvent>()
{
@Override
public void handle( final ActionEvent event )
{
treeView.getSelectionModel().select( nodeItemA3 );
}
} );
HBox hb = new HBox( bt1, bt2, bt3 );
hb.setPadding( new Insets( 80, 80, 80, 80 ) );
return hb;
}
public static void main( final String[] args )
{
launch( args );
}
}