如何从 JavaFX 的流程窗格中删除节点?
How to remove a node from a Flow Pane in JavaFX?
我创建了一个 HBoxes、Buttons 和 Labels 数组。每次按下 'Add' 按钮时,
我设置:
hbox[count] = new HBox();
buttons[count] = new Button();
labels[count] = new Label();
(其中计数从 0 开始到 5 结束)
然后我将按钮和标签添加到 HBox(这样每个 HBox 都包含一个按钮和一个标签),最后将 5 个 HBox 添加到 Flow Pane。
如何通过单击 HBox 内的按钮从 Flow Pane 中删除 HBox?
这是流程窗格中每个 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++;
}
}
}
这是一个示例...您可以将实现的想法应用到您的程序中:
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)
});
我创建了一个 HBoxes、Buttons 和 Labels 数组。每次按下 'Add' 按钮时, 我设置:
hbox[count] = new HBox();
buttons[count] = new Button();
labels[count] = new Label();
(其中计数从 0 开始到 5 结束)
然后我将按钮和标签添加到 HBox(这样每个 HBox 都包含一个按钮和一个标签),最后将 5 个 HBox 添加到 Flow Pane。
如何通过单击 HBox 内的按钮从 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++;
}
}
}
这是一个示例...您可以将实现的想法应用到您的程序中:
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)
});