0
我想開發一個簡單的例子,我可以選擇帶按鈕的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
我想選擇樹idem Item A1
。
你能告訴我一些工作的例子嗎? –
使用您的代碼更新了答案以創建一個工作示例。 – crusam
我可以嘗試從列表中選擇對象。有沒有辦法將樹項目作爲列表? –