栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用数据库中的数据填充TableView。无法设置行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用数据库中的数据填充TableView。无法设置行

如果要使此工作正常,则需要设置一些工厂。

在下面的示例中,我没有从SQL Server获取元素,但是我相信足以让您了解如何完成此任务。

请注意,它仅填充了顺序值,但仍然具有

header
与方法中的一样多的列
createTable()

package table;import com.sun.javafx.collections.ObservableListWrapper;import javafx.application.Application;import javafx.beans.property.SimpleIntegerProperty;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.scene.Scene;import javafx.scene.control.TableCell;import javafx.scene.control.TableColumn;import javafx.scene.control.TableRow;import javafx.scene.control.TableView;import javafx.scene.layout.FlowPane;import javafx.stage.Stage;import javafx.util.Callback;import java.util.Collections;import java.util.List;public class TableDemo extends Application {    private int rowIndex = 0;    private int elementIndex = 0;    final TableView tableView = new TableView();    ObservableList<String[]> dataRows = FXCollections.observableArrayList();    public static void main(String[] args) {        launch(args);    }    @Override    public void start(Stage stage) {        FlowPane pane = new FlowPane();        tableView.setRowFactory(new CountingRowFactory());        createTable();        pane.getChildren().add(tableView);        List items = Collections.singletonList(new Object());        tableView.setItems(dataRows);        stage.setScene(new Scene(pane));        stage.show();    }    private TableColumn createColumn(String header) {        TableColumn column = new TableColumn(header);        column.setCellFactory(new CellFactory());        column.setCellValueFactory(new CountingValueFactory());        return column;    }    private static class CellFactory implements Callback {        @Override        public Object call(Object column) { return new TableCell() {     @Override     protected void updateItem(Object o, boolean b) {         super.updateItem(o, b);         setText(String.valueOf(o));     } };        }    }    private class CountingRowFactory implements Callback {        @Override        public Object call(Object o) { TableRow row = new TableRow(); row.setUserData(rowIndex); rowIndex++; return row;        }    }    private class CountingValueFactory implements Callback {        @Override        public Object call(Object o) { SimpleIntegerProperty property = new SimpleIntegerProperty(elementIndex); elementIndex++; return property;        }    }    public void createTable(){        String[] header  = {"a","b","c","d","e","f","g","h"};        for(String s: header) tableView.getColumns().add(createColumn(s));        for(int i=0;i<30;i++){ String[] temp  = header; for(int j=0;j<temp.length;j++)     temp[j] += + i ;  dataRows.add(temp);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号