【问题标题】:Add the content of a TextField to a list with listener将 TextField 的内容添加到带有侦听器的列表中
【发布时间】:2015-12-14 07:23:23
【问题描述】:

我编写了一个小的 JavaFX 应用程序来说明我的问题。我有一组复选框和一个 TextField,所有这些都包含在一个 Vbox 中,该 Vbox 在应用程序启动时构建。我希望将选中的复选框添加到列表中,我设法做到了,但是对于 TextField 我没有得到它。当我在文本字段中键入文本时,所有字符都添加到列表中,但是我希望一次添加整个文本。 这是我的代码来说明我的意思。

package sample;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.*;

public class Controller implements Initializable {

    public VBox mainVb;
    List<String> categories = new ArrayList<>();
    List<String> checked = new ArrayList<>();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        categories();
    }

    public void categories() {
        categories.add("Condition 1");
        categories.add("Condition 2");
        categories.add("Condition 3");
        categories.add("Condition 4");
        categories.add("Condition 5");
        categories.add("Condition 6");
        categories.add("Condition 7");
        categories.add("Condition 8");
        checked.add("Condition 2");
        checked.add("Condition 3");
        checked.add("Condition 6");

        for (int i = 0; i < categories.size(); i++) {
            CheckBox checkBox = new CheckBox();
            checkBox.setText(categories.get(i));
            if (checked.contains(categories.get(i))) checkBox.setSelected(true);
            checkBox.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    if (checkBox.isSelected()) checked.add(checkBox.getText());
                    if (!checkBox.isSelected()) checked.remove(checkBox.getText());
                }
            });
            mainVb.getChildren().add(checkBox);
        }
        TextField textArea = new TextField();
        textArea.setPromptText("Other...");
        textArea.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                checked.add(textArea.getText());
            }
        });
        mainVb.getChildren().add(textArea);
    }

    public void seeChecked(ActionEvent event) {
        System.out.println(checked);
    }
}

如果我在 TextField 中输入“hello”,它会在我的列表中添加“h”“he”“hel”“hell”“hello”。如何一次添加“你好”。

非常感谢。 皮埃尔。

【问题讨论】:

  • 这就是“改变”事件的本质。在 TextField 中键入或删除的任何内容都会触发此事件。您可能想尝试的是 keyPress 事件,并在按下回车键时启动,指示数据输入到 TextField 的结束。

标签: java javafx


【解决方案1】:

使用 setOnAction 代替 textProperty 改变

 textArea.setOnAction(e ->  checked.add(textArea.getText()));

输入完“hello”后,在文本字段中按回车

【讨论】:

    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2013-02-17
    • 2011-09-28
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多