如何将数据从mongoDB显示到TableView中

时间:2019-07-17 08:55:40

标签: java mongodb javafx

我有一个要使用JAVAFX在tableView中显示的mongoDB数据库 问题是,当我运行时,返回的tableView为空(无显示) 我要在数据库中显示的字段名称为“日期”

这是Object类

 public class Attendees {
private final SimpleStringProperty firstname;
public Attendees(String firstname) {
    this.firstname = new SimpleStringProperty(firstname);
}
}

控制器

public class AttendanceList implements Initializable{
private final static String HOST = "localhost";
private final static int PORT = 27017;
private String fname;

@FXML
private TableView<Attendees> table;
@FXML
private TableColumn<Attendees, String> firstname;
//  create an observable list to hold the Attendees object in the Attendees class
public ObservableList<Attendees> list;

public List attend = new ArrayList();

static MongoDBManager mongoDBManager = new MongoDBManager();


@Override
public void initialize(URL location, ResourceBundle resources)  {
    list=FXCollections.observableArrayList();
    try {
        DB mongoDatabase = mongoDBManager.getDatabase("Data");
        DBCollection collection = mongoDatabase.getCollection("InfoClimatData");
        DBCursor d = collection.find();
        table.setEditable(true);
        try {
            while (d.hasNext()){
                DBObject doc = d.next();

                attend.add(new Attendees(doc.get("Date").toString()));
            }
            firstname.setCellValueFactory(new PropertyValueFactory<Attendees, String>("firstname"));
            table.setItems(list);
            System.out.println(list.toString());

        } finally {
            d.close();
        }

    }catch (Exception E){
    }
}

}

和fxml文件

<AnchorPane fx:id="attendance" prefHeight="400.0" prefWidth="747.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="altran.collection.sources.FX.AttendanceList">
<children>
    <TableView fx:id="table" layoutX="24.0" layoutY="53.0" prefHeight="293.0" prefWidth="702.0">
        <columns>
            <TableColumn fx:id="firstname" editable="true" minWidth="50.0" prefWidth="100.0" text="Firstname" />
        </columns>
    </TableView>



    <Label fx:id="status" alignment="CENTER" layoutX="24.0" layoutY="14.0" prefHeight="21.0" prefWidth="702.0" textAlignment="CENTER" textFill="#dd1313">
        <font>
            <Font size="18.0" />
        </font>
    </Label>
</children>

返回的结果是一个空的TableView

0 个答案:

没有答案