2

我正在尝试使用自定义复选框创建一棵树。Scenebuilder 中没有具有此类选项的选项。我试图在控制器中编写代码,但没有奏效。

有人请帮帮我。FXML 和控制器代码:

CheckBoxTreeItem 不工作。

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

        <AnchorPane prefHeight="486.9609375" prefWidth="472.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="abcd.controller">
      <!-- TODO Add Nodes -->
      <children>
        <Button id="button" fx:id="button1" layoutX="99.0" layoutY="51.0" mnemonicParsing="false" onAction="#processLogin" text="Button" />
        <TextArea fx:id="textarea" layoutX="40.0" layoutY="83.0" prefHeight="76.0" prefWidth="129.0" wrapText="true" />
        <TreeView fx:id="treeview" layoutX="69.0" layoutY="204.0" prefHeight="200.0" prefWidth="200.0" />       
      </children>
    </AnchorPane>


import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.CheckBoxTreeItemBuilder;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.util.Callback;

public class controller implements Initializable {

// Binding with the FXML
@FXML
private Button taskBarButton1;

@FXML private TextArea textarea;

@FXML private TreeView<String> treeview;

@FXML private CheckBoxTreeItem<String> root;

//@FXML private CheckBoxTreeItem<String> rootitem ;


@FXML
private void processLogin(ActionEvent event) {

    textarea.appendText("clicked");

}

public void loadonstart()
{
    for (int i=0; i<5;i++)
    {
        //System.out.println("numbers" +i);

        textarea.appendText("\n"+i);
    }
}

@Override
public void initialize(URL location, ResourceBundle resources) {
   loadonstart();
   loadtreeitems();

}

private void loadtreeitems()
{   
       CheckBoxTreeItem<String> root = new CheckBoxTreeItem<String>("Source Root"); 
       root.setExpanded(true);
       TreeView<String> tree = new TreeView<String> (root);     

        //tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
        for (int j=10; j<15; j++)
        {               
            root.setExpanded(true);
            root.getChildren().add(new CheckBoxTreeItem<String>("" +j));


        }
        treeview.setRoot(root);

}
}
4

0 回答 0