簡體   English   中英

TreeItem 在 JavaFX 中被覆蓋。 如何解決這個問題?

[英]TreeItem is Overwritten in JavaFX. How to solve this?

每次我添加一個新類時,Treeitem 都會被覆蓋。 如何解決這個問題?

同時添加一個新的對象treeitem應該是動態增加試過的:

  1. 添加而不使用列表
  2. 使用列表添加
  3. 嘗試使用循環添加

這是一個示例代碼,抱歉命名錯誤。提前致謝

--------標記出問題的地方

窗格類.java

public class PanesClass extends Application {
    ObservableList<Connections> cList = FXCollections.observableArrayList();

    public static void main(String[] args) {
        launch(args);
    }

    @SuppressWarnings("all")@Override
    public void start(Stage primaryStage) throws Exception {
        NewConnection newConnection = new NewConnection();
        SplitPane root = new SplitPane();
        AnchorPane first = new AnchorPane();
        AnchorPane second = new AnchorPane();
        TreeTableView activeConnections = new TreeTableView();
        HBox buttonBox = new HBox();
        BorderPane topBar = new BorderPane();
        Button nConnection = new Button("+");
        Button deleteConnection = new Button("X");
        Button connect = new Button("Connect");

        buttonBox.setSpacing(10);
        buttonBox.getChildren().addAll(nConnection, deleteConnection, connect);
        topBar.setTop(buttonBox);

        TreeTableColumn<String, Connections > cNameColoumn = new TreeTableColumn<>("Name");

        cNameColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cname"));

        TreeTableColumn<String, Connections> cStatusColoumn = new TreeTableColumn<>("Status");
        cStatusColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cstatus"));

        activeConnections.getColumns().addAll(cNameColoumn, cStatusColoumn);
        activeConnections.setLayoutX(20);
        activeConnections.setLayoutY(40);
        activeConnections.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);

        first.getChildren().addAll(topBar, activeConnections);
        root.getItems().addAll(first, second);

        Scene sc = new Scene(root, 600, 480);

        primaryStage.setScene(sc);
        primaryStage.show();

        nConnection.setOnAction(new EventHandler<ActionEvent>() {@Override
            public void handle(ActionEvent event) {
                newConnection.getConnection(activeConnections);
            }
        });
    }
}

新連接.java

public class NewConnection {
    Connections connection = null;
    ObservableList<Connections> cList = FXCollections.observableArrayList();
    PanesClass panesClass = new PanesClass();
    TreeItem cItem = null;
    TreeItem nItem = null;

    public void getConnection(TreeTableView<Connections> activeConnections) {
        Stage secondaryStage = new Stage();
        VBox root = new VBox();
        GridPane cDetails = new GridPane();
        HBox actionButtons = new HBox();
        Button connect = new Button("Connect");
        Button save = new Button("Save");
        Button cancel = new Button("Cancel");

        actionButtons.getChildren().addAll(connect, save, cancel);
        actionButtons.setSpacing(10);

        Label name = new Label("Username : ");

        cDetails.add(name, 0, 0);

        TextField uName = new TextField();

        cDetails.setHgrow(uName, Priority.ALWAYS);
        cDetails.add(uName, 1, 0);

        Label password = new Label("Password : ");

        cDetails.add(password, 0, 1);

        TextField pwd = new TextField();

        cDetails.add(pwd, 1, 1);

        Label urllink = new Label("URL : ");

        cDetails.add(urllink, 0, 2);

        TextField url = new TextField();

        cDetails.add(url, 1, 2);
        cDetails.setVgap(10);
        cDetails.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;" + "-fx-border-width: 1;" + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: black;");
        root.getChildren().addAll(cDetails, actionButtons);

        Scene sc = new Scene(root, 500, 200);

        secondaryStage.setScene(sc);
        secondaryStage.initModality(Modality.APPLICATION_MODAL);
        secondaryStage.show();

        save.setOnAction(new EventHandler<ActionEvent>() {
            //*-----------------------------------------------------------------------*
            @Override
            public void handle(ActionEvent event) {
                cItem = getitem(cItem);
                activeConnections.setRoot(cItem);
                activeConnections.setShowRoot(false);
                secondaryStage.close();
            }

            private TreeItem getitem(TreeItem cItem) {
                cList.add(new Connections(uName.getText()));
                System.out.println(cList);

                for (Connections temp: cList) {
                    System.out.println(temp);
                    nItem = new TreeItem<Connections>(temp);
                    System.out.println(nItem);
                    cItem.getChildren().add(nItem);
                }
                return cItem;
            }
        });
        System.out.println(cList);
    }
}

連接.java

public class Connections {
    private String cname = null;
    private String cstatus = null;
    private String cpwd = null;
    private String curl = null;

    public Connections() {

    }

    public Connections(String cname, String cpwd, String curl) {
        super();
        this.cname = cname;
        this.cpwd = cpwd;
        this.curl = curl;
    }

    public Connections(String cname, String cstatus) {
        super();
        this.cname = cname;
        this.cstatus = cstatus;
    }

    public String getCpwd() {
        return cpwd;
    }

    public void setCpwd(String cpwd) {
        this.cpwd = cpwd;
    }

    public String getCurl() {
        return curl;
    }

    public void setCurl(String curl) {
        this.curl = curl;
    }

    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public String getCstatus() {
        return cstatus;
    }
    public void setCstatus(String cstatus) {
        this.cstatus = cstatus;
    }

    @Override
    public String toString() {
        return "Connections [cname=" + cname + ", cstatus=" + cstatus + ", cpwd=" + cpwd + ", curl=" + curl + "]";
    }
}

您不是在填充樹,而是在創建新項目而不將它們添加到樹中。 首先,您需要創建一個根:

// Instead of this line
// TreeItem nItem = null;
TreeItem rootItem = new TreeItem();

然后:

activeConnections.setRoot(rootItem);
save.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        // clear old connections
        rootItem.getChildren().clear();

        // Add new connection
        cList.add(new Connections(uName.getText(), pwd.getText(), url.getText()));

        // create new items and add them to rootItem
        for (Connections temp : cList) {
            rootItem.getChildren().add(new TreeItem<Connections>(temp));
        }

        secondaryStage.close();
        event.consume();
    }
});

注意:如果您沒有其他理由保留cList ,您可以將其刪除並直接添加新項目(無需每次都清除和重新生成項目):

save.setOnAction(event -> {
    Connections newConnection = new Connections(uName.getText(), pwd.getText(), url.getText());
    rootItem.getChildren().add(new TreeItem<>(newConnection));

    secondaryStage.close();
    event.consume();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM