如何从JavaFX中的流窗格中删除节点?

时间:2017-05-15 16:34:41

标签: java javafx netbeans scenebuilder

我创建了一系列HBox,Buttons和Labels。每次添加'按下按钮, 我设置:

hbox[count] = new HBox();
buttons[count] = new Button();
labels[count] = new Label();

(其中count从0开始到5结束) 然后我将按钮和标签添加到HBox(以便每个HBox包含一个按钮和一个标签),最后将5个HBox添加到流程窗格中。 如何通过单击HBox内的按钮从Flow窗格中删除HBox? enter image description here 这是Flow Pane中每个HBox的图片。

/**
 * FXML Controller class
 *
 * @author HeshamSaleh
 */
public class SecondaryViewController implements Initializable {

@FXML
private TextField searchBar;
@FXML
private Button addBtn;
@FXML
private FlowPane flowPane;

ArrayList<String> addedArtists = new ArrayList<String>();
String[] artists = {"Craig David", "Coldplay", "Eminem", "D12", "Shakira", "Radiohead", "Linkin Park", "Maroon 5", "Celine Dion", "50 Cent", "Tupac", "Snoop Dogg", "Metallica", "Backstreet Boys"};
List<String> artistNames  = new ArrayList<String>(Arrays.asList(artists));
int count = 4;
HBox[] hboxArr = new HBox[5];
Button[] buttonArr = new Button[5];
Label[] labelArr = new Label[5];
int hboxCount = 0;
/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    searchBar.setFocusTraversable (false);
    TextFields.bindAutoCompletion(searchBar, artistNames);
}   

@FXML
private void addBtnPressed(MouseEvent event) {
    String artistName = searchBar.getText();
    searchBar.setText("");

    if(artistNames.contains(artistName) && !addedArtists.contains(artistName) && count != -1) {

        hboxArr[hboxCount] = new HBox();
        buttonArr[hboxCount] = new Button();
        labelArr[hboxCount] = new Label();
        hboxArr[hboxCount].setAlignment(Pos.CENTER);
        hboxArr[hboxCount].setSpacing(-1);
        buttonArr[hboxCount].setText("X");
        buttonArr[hboxCount].setAlignment(Pos.CENTER);
        buttonArr[hboxCount].setStyle("-fx-background-color: TRANSPARENT; -fx-border-color: #000000;");
        buttonArr[hboxCount].setFont(Font.font("Open Sans", FontWeight.BOLD, 12));
        buttonArr[hboxCount].setMinWidth(20);
        buttonArr[hboxCount].setMinHeight(20);
        labelArr[hboxCount].setText(artistName.toUpperCase());
        labelArr[hboxCount].setFont(Font.font("Proxima Nova Rg", 12));
        labelArr[hboxCount].setAlignment(Pos.CENTER);
        labelArr[hboxCount].setStyle("-fx-background-color: TRANSPARENT; -fx-border-color: #000000;");
        labelArr[hboxCount].setMinWidth(90);
        labelArr[hboxCount].setMinHeight(27);

        hboxArr[hboxCount].getChildren().addAll(buttonArr[hboxCount], labelArr[hboxCount]);
        flowPane.setAlignment(Pos.CENTER);
        flowPane.getChildren().add(hboxArr[hboxCount]);
        addedArtists.add(artistName);
        count--;
        hboxCount++;
    }

}
}

1 个答案:

答案 0 :(得分:2)

以下是一个示例...您可以了解实施的想法并将其应用于您的计划:

import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Example extends Application{

    ArrayList<Node> components = new ArrayList<Node>(); // arraylist to contain all components


    @Override
    public void start(Stage ps) throws Exception {
        FlowPane root = new FlowPane();

        for (int i=0; i<5; i++){

            HBox hb = new HBox();
            // set hb attributes

            Button b = new Button("Button" + i);
            // set b attributes

            // then add action listener
            b.setOnAction(e->{ 
                root.getChildren().remove(hb); // remove by Object reference
            });

            Label l = new Label("Label" + i);
            // set l attributes

            hb.getChildren().addAll(b,l);
            components.add(hb);
        }

        root.getChildren().addAll(components);

        Scene s = new Scene(root, 600,400);
        ps.setScene(s);

        ps.show();

    }

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

}

<强>更新

提供代码后,您需要做的就是将其添加到代码中:

buttonArr[hboxCount].setOnAction(e->{ // add listener to your button at every index in your array of buttons
    flowPane.getChildren().remove(hboxArr[hboxCount]); // and when that button is pressed, remove the HBox at the same index in the array of HBoxs from the flowPane (that works as I said -> removing by Object Reference)
});