从TextField动态更新ListView(JavaFX) [英] Dynamically Updating a ListView from a TextField (JavaFX)

查看:289
本文介绍了从TextField动态更新ListView(JavaFX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用按钮上的简单事件处理程序将名称(字符串)应用到JavaFX中的ListView中,从TextFields接收输入。所有字段似乎都正常工作,它只是没有更新到列表视图中

I'm trying to apply names(strings) into a ListView within JavaFX using a simple event handler on a button, receiving input from TextFields. All the fields seem to be working and correct, it just doesnt update into the list view

    private ListView<String> manifestList;
    manifestList = new ListView<>();
    manifestList.setId("List");
    manifestList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

ListView添加方法:

ListView add method:

   public void addtoList(String manifestlist){
    manifestList.getItems().add(manifestlist);
   }

用于生成输入文本框的循环(我已将其预设为name1, name2等测试它)

For loop which generates text boxes for input (I have preset it to name1, name2 etc to test it)

    for( i = 0;i < editname.length;i++){
    editname[i] = new TextField("Name" + i);    
    }

将文本字段中的文本添加到列表视图的按钮处理程序(有16个文本方框)

Button handler to add the text from textfields to the listview (There is 16 text boxes)

 Button add = new Button("add");

    add.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent event){

            for(i=0; i<16; i++){
            test.addtoList(editname[i].getText());
        }       
    }
});

test是我调用add方法的类的变量名,列表视图是in。调用add方法的按钮位于不同的场景/类中。

test is the variable name for the class I am calling the add method from, which the list view is in. The button which is calling the add method, is in a different scene/class.

我无法弄清楚这里出了什么问题,我打印过文本字段的值到控制台,它们打印正常 - 但是它们不会更新到ListView。

I can't quite figure out what's going wrong here, i've printed the value of the textfields to console and they print fine - however they dont update into the ListView.

我尝试在列表中使用带有预设字符串的add方法查看,并添加正常。

I've tried using the add method with a preset string in the list view and that is added fine.

任何帮助/提示都将非常感谢。

Any help/tips would be great thanks.

推荐答案

创建一个全局 ObservableList 将其命名为例如 items

Make a global ObservableList name it for example items:

//create an ObservableList
ObservableList<String> items = FXCollections.observableArrayList();






然后以每次更新的方式使用它 ObservableList 会反映回 ListView< String> manifestList;

 manifestList.setItems(items); 

这篇关于从TextField动态更新ListView(JavaFX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆