简体   繁体   English

单击按钮添加字段

[英]Adding fields on button click

I need to add a bunch of Labels and TextFields on button click. 我需要在单击按钮时添加一堆Labels和TextFields。 In this case I need to add them as code right, not in the FXML? 在这种情况下,我需要将它们作为代码添加,而不是在FXML中?

I have Automobile class and I have to add like 10 labels and text fields when the user clicks a "Add Automobile" button. 我有汽车课,当用户单击“添加汽车”按钮时,我必须添加10个标签和文本字段。 Is there a better way than adding them like this: 有没有比这样添加它们更好的方法:

Label label = new Label("State registration number:");
TextField textField1 = new TextField();
Label label2 = new Label("Brand:");
TextField textField2 = new TextField();
Label label3 = new Label("Model:");
TextField textField3 = new TextField();
Label label4 = new Label("Year of production:");

And so on... And if I need to add to them some other attributes, I need to write like 30+ more lines. 依此类推...如果我需要向他们添加一些其他属性,则需要编写30余行。 Is there a better way of doing this? 有更好的方法吗? What is the best practice? 最佳做法是什么?

First what you need is an int variable which its value is the amount of labels and textfields you want to create, it would be: 首先,您需要一个int变量,其值是要创建的标签和文本字段的数量,它将是:

int amount = 10;

You should declare three arrays: One wich contains the text of the labels it would be: 您应该声明三个数组:其中一个包含标签文本:

String [] text_labels = new String [] {"State registration number:", "Brand:", "Model:", "..."};

Then you should declare the second array which may be: 然后,您应该声明第二个数组,它可能是:

Label [] labels = new Label[amount];

And the third one: 第三个:

TextField [] textfields = new Text field[amount];

Once you haved declared them you have to initialize the labels and textfields. 声明它们后,您必须初始化标签和文本字段。 In order to do so you can do: 为此,您可以执行以下操作:

for(int i = 0; i < amount; i ++) {
  Label label = new Label(text_labels[i]);
  TextField textField = new TextField();
  labels[i] = label;
  textfields[i] = textField;
}

So labels[0] would be the same as the first label you wrote in your code and the same with the textfields. 因此,labels [0]将与您在代码中编写的第一个标签相同,并且与文本字段相同。

It's not the greatest solution but a good base to start with 这不是最大的解决方案,而是一个良好的起点

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MultipleLabelTextFiledApp extends Application {
    private final ObservableList<CustomControl> customControls = FXCollections.observableArrayList();
    private final List<String> labels = Arrays.asList("label1", "label2", "label3", "label4", "label5");

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

    @Override
    public void start(Stage stage) throws Exception {

        labels.stream().forEach(label -> customControls.add(new CustomControl(label)));

        VBox vBox = new VBox();
        vBox.getChildren().setAll(customControls);

        stage.setScene(new Scene(vBox));
        stage.show();

        getCustomControl("label1").ifPresent(customControl -> {
            customControl.getTextField().textProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
                System.out.println("textField with label1 handler new text=" + newValue);
            });
        });
    }

    private Optional<CustomControl> getCustomControl(String labelText) {
        return customControls.stream()
        .filter(customControl -> labelText.equals(customControl.getLabel().getText()))
        .findFirst();
    }
}

class CustomControl extends HBox {

    private final Label label = new Label();
    private final TextField textField = new TextField();

    {
        getChildren().addAll(label, textField);
    }

    public CustomControl(String text) {
        label.setText(text);
    }

    public Label getLabel() {
        return label;
    }

    public TextField getTextField() {
        return textField;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM