2017-05-15 45 views
1

我創建了一個HBoxes,Buttons和Labels數組。每一個「添加」按鈕被按下時, 我設置:如何從JavaFX的流程窗格中刪除節點?

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

(其中計數從0開始和結束於5) 我然後添加按鈕和標籤到HBox中(使得每個HBox中包含一個按鈕和一個標籤),最後將5個HBox添加到流程窗格中。 如何通過單擊HBox內的按鈕從流程窗格中刪除HBox?enter image description here 這是Flow Panel中每個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++; 
    } 

} 
} 

回答

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); 
    } 

} 

UPDATE:

後,您提供的代碼,所有您需要做的就是將此添加到您的代碼中:

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) 
}); 
+0

你怎麼知道你要移除哪一個?當按下按鈕時,我需要刪除一個特定的hb –

+1

@HeshamSaleh Akhi它刪除了按下按鈕的HBox(即父項)..請將代碼複製粘貼到您的IDE中並檢查:) – Yahya

+0

好吧,您的代碼完全正確我需要的。你能幫我在上面的代碼中實現它嗎?非常感謝Yahya! –