JavaFX 2.0 TableView在FXML中与数据库中的数据 [英] JavaFX 2.0 TableView in FXML with Data from Database

查看:1655
本文介绍了JavaFX 2.0 TableView在FXML中与数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy大家,我想知道如何我从一个mysql数据库检索数据到一个tableview是一个fxml文件。我必须检索数据并将其放入一个位于fxml文件中的tableview。我有想法,我将连接到数据库,但我不知道如何检索数据,并将其放入tableview。



表视图是在fxml文件。如果我仍然在这里,任何人知道我导入数据后如何编辑这个表格视图中的单元格?



我工作的应用程序是一个桌面的!

非常感谢我的英语不好。

解决方案

有几种方法可以做到这一点,一个是使用来自 DataFX 的JDBC适配器。



更新



FXP本身只是定义了一个基于xml的UI结构,它不能从数据库加载数据。 / p>

当数据来自数据库时,创建包含数据值的静态fxml文件在大多数情况下可能是一个坏主意。每当数据库中的数据更改时,您将需要生成和加载一个新的fxml文件,为此您将需要手动创建新文件或写一个脆弱的程序来生成新的fxml文件基于数据库。



建议的方法:


  1. 使用fxml仅定义您的TableView

  2. 在您的JavaFX Java代码连接到mysql数据库。

  3. 使用DataFX JDBC数据源适配器从数据库中检索数据并填充您的TableView。

您可以编写自己的代码来执行步骤3.使用自己的代码如果你希望使用JPA(我认为DataFX目前不直接支持)是一个好主意。



要了解如何在TableView中进行编辑,请阅读此 TableView教程。同样,DataFX将通过提供控制单元工厂进行编辑来帮助您 - 因此我会推荐使用DataFX这个,而不是手工编码的TableView编辑处理。



我不确定DataFX代码是否会帮助你,当你需要提交编辑数据库;在这种情况下,您可能需要添加自己的JDBC或JPA代码来提交更改。


Hy everyone, i am wondering how can i retrieve data from a mysql database into a tableview which is into a fxml file. I must retrieve the data and place it into a tableview which is located in a fxml file. I have the idea of how i will connect to the database, but i don't know how to retrieve the data and place it into tableview.

The tableview is in a fxml file. If i am still here, anyone know after i import the data how can i edit the cells in this tableview?

The application i work on is a desktop one!

Thanks a lot and sorry for my bad english!

解决方案

There are a few ways to do this, one is to use the JDBC adapter from DataFX.

Update

FXML itself just defines an xml based UI structure, it cannot load data from a database.

When the data originates from the database, creating a static fxml file containing the data values is likely a bad idea in most cases. Anytime the data in the database changes, you will need to generate and load a new fxml file and for that you will need to either create the new file by hand or write a fragile program to generate the new fxml file based upon the data in the database.

Suggested approach:

  1. Use the fxml only to define your TableView structure but not to contain actual data values.
  2. In your JavaFX Java code connect to the mysql database.
  3. Use the DataFX JDBC data source adapter to retrieve the data from the database and populate your TableView.

You could write your own code to do step 3. Using your own code might be a good idea if you wish to use JPA (which I think DataFX does not currently directly support). Otherwise you will probably end up with better results quicker using the DataFX implementation.

To find out how to do edits in TableView, read this TableView tutorial. Again, DataFX will help you by providing Control Cell Factories for edits - so I'd recommend using DataFX for this rather than handcoding the TableView edit handling.

I'm not sure if the DataFX code will help you when you need to commit edits to a database; in that case you will probably need to add your own JDBC or JPA code to commit the changes.

这篇关于JavaFX 2.0 TableView在FXML中与数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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