为什么我的归档组件无法在javafx中动态删除

时间:2018-08-11 10:51:19

标签: javafx javafx-8

我想在单击按钮时动态添加文本框和组合框,但不能这样做。下面是我尝试的代码。在此代码中,我已成功添加了该字段,但单击按钮后无法一次删除该项目。多次添加字段后,只有最后一项被删除。所以请检查我的代码在哪里出错。

package application;

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

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import com.sun.javafx.scene.layout.region.Margins;

import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

public class CustomerController  implements Initializable {

    public JFXTextField 
    acc_no,ifsc_code,micr_code,acc_no1,ifsc_code1,micr_code1;
    public JFXComboBox<String> bank_name,bank_name1;
    public JFXButton add_row,rmv_row;
    public GridPane grid_component;
    public VBox vBox2_component, vbox1_component;

    public AnchorPane anchor_pane;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        bank_name.getItems().removeAll(bank_name.getItems());
        bank_name.getItems().addAll("Bank of India", "Dena Bank", "HDFC Bank");
         new AutoCompleteComboBoxListener(bank_name);

    }

// add button functionality

    public void AddBankDetails(ActionEvent event) {
        /*if(count == max_row-1){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("Maximum of 10 rows can be added\",\"Failed!!");
            alert.showAndWait();
            return;
        }

         count++;*/

        bank_name1 = new  JFXComboBox();
        bank_name1.getStyleClass().add("textfield_margin");
        bank_name1.setLabelFloat(true);
        bank_name1.setFocusColor(javafx.scene.paint.Color.valueOf("#2196F3"));
        bank_name1.setPromptText("Bank Name");
        bank_name1.setUnFocusColor(javafx.scene.paint.Color.valueOf("939393"));
        vbox1_component.setMargin(bank_name1, new Insets(20, 10, 10, 10));

        ifsc_code1 = new JFXTextField();
        ifsc_code1.getStyleClass().add("textfield_margin");
        ifsc_code1.setLabelFloat(true);
        ifsc_code1.setFocusColor(javafx.scene.paint.Color.valueOf("#2196F3"));
        ifsc_code1.setPromptText("IFSC Code");
        ifsc_code1.setUnFocusColor(javafx.scene.paint.Color.valueOf("939393"));
        vbox1_component.setMargin(ifsc_code1, new Insets(20, 10, 10, 10));

         acc_no1 = new JFXTextField();
        acc_no1.getStyleClass().add("textfield_margin");
        acc_no1.setLabelFloat(true);
        acc_no1.setFocusColor(javafx.scene.paint.Color.valueOf("#2196F3"));
        acc_no1.setPromptText("Account number");
        acc_no1.setUnFocusColor(javafx.scene.paint.Color.valueOf("939393"));
        vBox2_component.setMargin(acc_no1, new Insets(20, 10, 10, 10));

         micr_code1 = new JFXTextField();
        micr_code1.getStyleClass().add("textfield_margin");
        micr_code1.setLabelFloat(true);
        micr_code1.setFocusColor(javafx.scene.paint.Color.valueOf("#2196F3"));
        micr_code1.setPromptText("MICR Code");
        micr_code1.setUnFocusColor(javafx.scene.paint.Color.valueOf("939393"));
        vBox2_component.setMargin(micr_code1, new Insets(20, 10, 10, 10));
        vbox1_component.getChildren().addAll(bank_name1,ifsc_code1);
        vBox2_component.getChildren().addAll(acc_no1,micr_code1);
    }

  //remove button functionality

    public void rmvBankDetails(ActionEvent events) {

        vbox1_component.getChildren().removeAll(bank_name1,ifsc_code1);
        vBox2_component.getChildren().removeAll(acc_no1,micr_code1);

    }

}

1 个答案:

答案 0 :(得分:0)

您仅存储对为AddBankDetails的上一次调用创建的控件的引用。这意味着如果您多次单击添加按钮,然后多次单击删除按钮。您只能成功VBox一次删除操作。

假设这是唯一修改VBox es且VBox es不包含其他子代的代码,则只需从每个列表中删除最后两个子代即可:

private static void removeElements(List<?> list, int count) {
    int size = list.size();
    if (size >= count) {
        list.subList(size - count, size).clear();
    }
}
public void rmvBankDetails(ActionEvent events) {
    removeElements(vbox1_component.getChildren(), 2);
    removeElements(vBox2_component.getChildren(), 2);
}

其他说明:

  • 取决于使用GridPane的布局,可以选择。 TableView也许也值得一看。
  • 您可能需要访问所有控件,因此可能希望将新添加的控件列表保留在列表中。使用这些数据,您还可以检索要删除的控件。

    private static class Container {
        final JFXComboBox bank_name1;
        final JFXTextField ifsc_code1;
        final JFXTextField acc_no1;
        final JFXTextField micr_code1;
    
        Container(JFXComboBox bank_name1, JFXTextField ifsc_code1, JFXTextField acc_no1, JFXTextField micr_code1) {
            this.bank_name1 = bank_name1;
            this.ifsc_code1 = ifsc_code1;
            this.acc_no1 = acc_no1;
            this.micr_code1 = micr_code1;
        }
    
    }
    
    private final List<Container> bankDetailsContainers = new ArrayList<>();
    
    public void AddBankDetails(ActionEvent event) {
        ...
        bankDetailsContainers.add(new Container(bank_name1, ifsc_code1, acc_no1, micr_code1));
    }
    
    public void rmvBankDetails(ActionEvent events) {
        if (!bankDetailsContainers.isEmpty()) {
            Container c = bankDetailsContainers.remove(bankDetailsContainers.size() - 1);
            vbox1_component.getChildren().removeAll(c.bank_name1, c.ifsc_code1);
            vBox2_component.getChildren().removeAll(c.acc_no1, c.micr_code1);
        }
    
    }