首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有办法在tableView (javaFX)中迭代地打印(或在一秒后)表记录

有没有办法在tableView (javaFX)中迭代地打印(或在一秒后)表记录
EN

Stack Overflow用户
提问于 2019-04-14 04:19:40
回答 1查看 39关注 0票数 1

我已经创建了一些person类对象,我使用ObserveableList并在initialize方法中调用它来打印它们的变量属性,问题是我没有得到它如何才能单独打印每个对象,这些对象是作为整体打印的,我想逐个迭代

代码语言:javascript
运行
复制
    Person p1 = new Person("1001", "person1", "11");
    Person p2 = new Person("1002", "person2", "12");
    Person p3 = new Person("1003", "person3", "13");
    Person p4 = new Person("1004", "person4", "14");

    ObservableList<Person> personDataTable = FXCollections.observableArrayList(p1, p2, p3, p4);


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

    public void printTable() {
            tableID.setCellValueFactory(new PropertyValueFactory<Person, String>("id"));
            tableName.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
            tableAge.setCellValueFactory(new PropertyValueFactory<Person, String>("age"));
            tableView.setItems(personDataTable);
    }

为了清楚起见,您想要向表中添加一个人,等待一秒钟,添加下一个人,等待另一个人,添加下一个人,依此类推,对吗?是的,完全正确(我无法回复评论)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-14 05:20:54

实现这一点的关键是Timeline。基本上,使用计数器并确保在计数器大于或等于保存person对象的列表的大小时停止Timeline。MCVE如下:

代码语言:javascript
运行
复制
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication86 extends Application {
    Timeline timeline;
    AtomicInteger counter;

    @Override
    public void start(Stage primaryStage) {
        Person p1 = new Person("1001", "person1", "11");
        Person p2 = new Person("1002", "person2", "12");
        Person p3 = new Person("1003", "person3", "13");
        Person p4 = new Person("1004", "person4", "14");
        List<Person> personList = new ArrayList();
        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        personList.add(p4);        

        counter = new AtomicInteger();
        TableView<Person> tableView = new TableView();
        TableColumn<Person, String> tableId = new TableColumn("ID");
        TableColumn<Person, String> tableName = new TableColumn("Name");
        TableColumn<Person, String> tableAge = new TableColumn("Age");

        ObservableList<Person> personDataTable = FXCollections.observableArrayList();
        tableView.setItems(personDataTable);

        tableId.setCellValueFactory(new PropertyValueFactory("id"));
        tableName.setCellValueFactory(new PropertyValueFactory("name"));
        tableAge.setCellValueFactory(new PropertyValueFactory("age"));

        timeline = new Timeline(new KeyFrame(Duration.seconds(1), event ->{
            if(counter.get() >= personList.size())
            {
                timeline.stop();
            }else{
                personDataTable.add(personList.get(counter.getAndIncrement()));
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();




        tableView.getColumns().addAll(tableId, tableName, tableAge);


        StackPane root = new StackPane(tableView);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55669503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档