Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX列表数据绑定_Java_Javafx_Data Binding - Fatal编程技术网

JavaFX列表数据绑定

JavaFX列表数据绑定,java,javafx,data-binding,Java,Javafx,Data Binding,我有一个运行在单独线程中的模型类(实现任务)。它有一个ArrayList,在无限循环期间更新 private List<ClientSession> clientSessions = new ArrayList<>(); 在控制器类中,我添加了: private ListProperty clientSessionListProperty=new SimpleListProperty(FXCollections.observearraylist()); clientSe

我有一个运行在单独线程中的模型类(实现任务)。它有一个
ArrayList
,在无限循环期间更新

private List<ClientSession> clientSessions = new ArrayList<>();
在控制器类中,我添加了:

private ListProperty clientSessionListProperty=new SimpleListProperty(FXCollections.observearraylist());
clientSessionListProperty.bindContent(commandCenterNio.clientSessions);

但这并不能解决tableview的问题。在本例中如何使用
表视图?

实际上,您不需要中间属性,默认情况下是“可绑定”的,因为每当对列表执行更改时,它都会告诉您:

允许侦听器在更改发生时跟踪更改的列表

您所要做的就是通过调用为listview直接提供模型的
ObservableList

我准备了一个例子:

该示例有一个实现
Runnable
但请注意,它在无限循环中更新其列表,这一事实在解决方案方面绝对没有什么区别,),它有一个
ToDo
对象的
可观测列表,它必须
属性
才能显示在
表视图
上。在
Main
中,模型填充了一些初始数据,并显示了一个带有数据的
ListView
。GUI还有一些控件可以通过其缓冲区向模型添加新项

SampleModel.java

public类SampleModel实现可运行{
//听这个列表
public ObservableList toDoList=FXCollections.observableArrayList();
//用于存储新元素直到线程唤醒的缓冲区
private BlockingQueue=new ArrayBlockingQueue(1000);
@凌驾
public void run(){
while(true){
//将缓冲区排空到ObservableList
queue.drainTo(托多利斯特);
//睡一会儿
试一试{
睡眠(5000);
}捕捉(中断异常e){
e、 printStackTrace();
}
} 
}
public void updateBuffer(ToDo newItem){
queue.offer(newItem);
}
}
ToDo.java

公共类待办事项{
私有StringProperty任务=新建SimpleStringProperty();
public StringProperty taskProperty(){return task;}
private ObjectProperty重要性=新的SimpleObjectProperty();
public ObjectProperty importanceProperty(){返回重要性;}
公共待办事项(字符串任务、重要性){
this.task.set(任务);
这个。重要性。设置(重要性);
}
枚举重要性{
DONTCARE,SHALL,MUST,FIRSTPRIO;
@凌驾
公共字符串toString(){
开关(本){
case DONTCARE:返回“我不在乎”;
案例应:返回“应完成”;
案例必须:返回“必须完成”;
案例一:返回“如果我不做,我会死”;
默认值:抛出新的IllegalArgumentException();
}
}
}
}
Main.java

public类主扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
试一试{
VBox root=新的VBox();
场景=新场景(根,400400);
scene.getStylesheets().add(getClass().getResource(“application.css”).toExternalForm());
SampleModel模型=新的SampleModel();
model.toDoList.addAll(新ToDo(“扫帚”,ToDo.Importance.DONTCARE),
新的待办事项(“小睡”,待办事项。重要性。第一优先级),
新的待办事项(“烹饪”,待办事项。重要性。必须),
新的待办事项(“洗车”,待办事项。重要性。待办事项),
新的ToDo(“付账单”,ToDo.Importance.SHALL));
TableView TableView=新建TableView();
TableColumn colTask=新建TableColumn();
colTask.setCellValueFactory(新属性ValueFactory(“任务”);
TableColumn colImportance=新建TableColumn();
colImportance.setCellValueFactory(cellData->new SimpleStringProperty(cellData.getValue().importanceProperty().get().toString());
tableView.getColumns().addAll(colTask,colImportance);
tableView.setItems(model.toDoList);
HBox HBox=新的HBox();
TextArea TextArea=新建TextArea();
textArea.setPrefSize(180,15);
ComboBox cb=新ComboBox();
cb.setItems(FXCollections.observearraylist(ToDo.Importance.FIRSTPRIO、ToDo.Importance.DONTCARE、ToDo.Importance.MUST));
按钮btnAdd=新按钮(“添加”);
setOnAction(e->model.updateBuffer(新ToDo(textArea.getText(),cb.getValue());
hbox.getChildren().addAll(textArea、cb、btnAdd);
root.getChildren().addAll(hbox,tableView);
螺纹=新螺纹(型号);
setDaemon(true);
thread.start();
初级阶段。场景(场景);
primaryStage.show();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
发射(args);
}
}
public ObservableList<ClientSession> clientSessions = FXCollections.observableArrayList();
private ListProperty<ClientSession> clientSessionListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
clientSessionListProperty.bindContent(commandCenterNio.clientSessions);
public class SampleModel implements Runnable{

    // Listen to this list
    public ObservableList<ToDo> toDoList = FXCollections.observableArrayList();

    // Buffer to be used to store new elements until the thread wakes up
    private BlockingQueue<ToDo> queue = new ArrayBlockingQueue<ToDo>(1000);

    @Override
    public void run() { 
        while(true){
            // Drain the buffer to the ObservableList
            queue.drainTo(toDoList);

            // Sleep a bit
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } 
        }

    public void updateBuffer(ToDo newItem){
        queue.offer(newItem);
    }
}
public class ToDo {

    private StringProperty task = new SimpleStringProperty();
    public StringProperty taskProperty() {return task;}

    private ObjectProperty<Importance> importance = new SimpleObjectProperty<Importance>();
    public ObjectProperty<Importance> importanceProperty() {return importance;}

    public ToDo(String task, Importance importance){
        this.task.set(task);
        this.importance.set(importance);
    }


    enum Importance {
        DONTCARE, SHALL, MUST, FIRSTPRIO;

          @Override
          public String toString() {
            switch(this) {
              case DONTCARE: return "I don't care";
              case SHALL: return "It shall be done";
              case MUST: return "It must be done";
              case FIRSTPRIO: return "I will die if I do not do it";
              default: throw new IllegalArgumentException();
            }
          }
    }

}
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            VBox root = new VBox();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            SampleModel model = new SampleModel();
            model.toDoList.addAll(new ToDo("Brooming", ToDo.Importance.DONTCARE),
                    new ToDo("Taking a nap", ToDo.Importance.FIRSTPRIO),
                    new ToDo("Cooking", ToDo.Importance.MUST),
                    new ToDo("Wash the car", ToDo.Importance.DONTCARE),
                    new ToDo("Pay the bills", ToDo.Importance.SHALL));

            TableView<ToDo> tableView = new TableView<ToDo>();

            TableColumn<ToDo, String> colTask = new TableColumn<ToDo, String>();
            colTask.setCellValueFactory(new PropertyValueFactory<>("task"));

            TableColumn<ToDo, String> colImportance = new TableColumn<ToDo, String>();
            colImportance.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().importanceProperty().get().toString()));

            tableView.getColumns().addAll(colTask, colImportance);

            tableView.setItems(model.toDoList);

            HBox hbox = new HBox();
            TextArea textArea = new TextArea();
            textArea.setPrefSize(180, 15);
            ComboBox<ToDo.Importance> cb = new ComboBox<ToDo.Importance>();
            cb.setItems(FXCollections.observableArrayList(ToDo.Importance.FIRSTPRIO, ToDo.Importance.DONTCARE, ToDo.Importance.MUST));

            Button btnAdd = new Button("Add");
            btnAdd.setOnAction(e -> model.updateBuffer(new ToDo(textArea.getText(), cb.getValue())));

            hbox.getChildren().addAll(textArea, cb, btnAdd);
            root.getChildren().addAll(hbox, tableView);

            Thread thread = new Thread(model);
            thread.setDaemon(true);
            thread.start();


            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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