JavaFX 场景构建器 table 不会使用 Java 数据库填充
JavaFX Scene builder table wont fill up using Java DB
我一直在努力弄清楚如何让列表显示在我的 table 视图中。因为输出是我的列表,它很棒,但我可以在通过场景生成器制作的 table 中看到它。 table 具有相同的 fx:id,所以不是这样。数据显示正常并且正在编译,但 table.
上仍然没有数据
public class FXMLDocument_PopulationDatabase_AllieBeckmanController implements Initializable {
@FXML
private ChoiceBox GetBox;
@FXML
private ChoiceBox SortBox;
@FXML
private TableView CityTable;
@FXML
private TableColumn COne;
@FXML
private TableColumn CTwo;
private final ObservableList dta = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
try{
// call the driver used to connect to the database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// connect to the city data base
Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/CityDB;create=true", "AllieBeckman", "1122");
// create a statement used to sort through the data in the database
Statement stmt = c.createStatement();
// create a result that will be used to get the data and translate it into variables
ResultSet result = stmt.executeQuery("SELECT * FROM city");
// get number of columns
ResultSetMetaData rsmd = result.getMetaData();
// continue sorting through and translating data until there are no more rows of data
while (result.next()){
// the 25 and 10 stand for two columns each is translated int its respective data type using
// the result set
System.out.println(result.getString("cityname") + result.getDouble("population"));
// adds one column
dta.add(result.getString("cityname"));
}
System.out.println(dta);
CityTable.setItems(dta);
// close the connection
c.close();
}catch(Exception e){
System.out.println("Failed");
System.out.println(e);
}
}
输出:
Executing C:\Users\Allie\Documents\NetBeansProjects\PopulationDatabase_AllieBeckman\dist\run1111906738\PopulationDatabase_AllieBeckman.jar using platform C:\Users\Allie\Desktop\Programs_and_Components\jdk1.8.0_101\jre/bin/java
New York 8.5505405E7
Los Angeles 3971883.0
Chicago 2720546.0
Huston 2296224.0
Philladelphia 1567442.0
Phoenix 1563025.0
San Antonio 1469845.0
San Diego 1394928.0
Dallas 1300092.0
San Jose 1026908.0
Austin 931830.0
Jacksonville 868031.0
San Francisco 864816.0
Indianapolis 853173.0
Columbus 850106.0
Fort Worth 833319.0
Charlotte 827097.0
Seattle 684451.0
Denver 682545.0
El Paso 681124.0
[New York , Los Angeles , Chicago , Huston , Philladelphia , Phoenix , San Antonio , San Diego , Dallas , San Jose , Austin , Jacksonville , San Francisco , Indianapolis , Columbus , Fort Worth , Charlotte , Seattle , Denver , El Paso ]
Deleting directory C:\Users\Allie\Documents\NetBeansProjects\PopulationDatabase_AllieBeckman\dist\run1111906738
感谢 INFOSYS link 它帮助我进行了这些更改并找到了我的答案 (http://code.makery.ch/library/javafx-8-tutorial/part2/)
我通过更改 FXML 文件中的一些内容来修复连接,并在代码中添加了一些新行,为了清楚起见我注释了这些代码。我还需要一个 class 用于我没有意识到的对象,现在它可以完美地从我创建的数据库中读取。
更改代码如下:
public class FXMLDocument_PopulationDatabase_AllieBeckmanController implements Initializable {
// connect the TableView from the xml file
@FXML
private TableView<City> CityTable;
// connect the table columns from the xml file
@FXML
private TableColumn<City, String> CityName;
@FXML
private TableColumn<City, String> Population;
// create an observable list to fill with data from DB and populate TableView
private final ObservableList<City> dta = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
try{
// call the driver used to connect to the database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// connect to the city data base
Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/CityDB;create=true", "AllieBeckman", "1122");
// create a statement used to sort through the data in the database
Statement stmt = c.createStatement();
// create a result that will be used to get the data and translate it into variables
ResultSet result = stmt.executeQuery("SELECT * FROM city");
// continue sorting through and translating data until there are no more rows of data
while (result.next()){
// print each line of data to check if it's right
System.out.println(result.getString("cityname") + result.getDouble("population"));
// make a new city object with each row in the database CityDB
City cty = new City(result.getString("cityname"), "" + result.getDouble("population"));
// add the new city object to the observable list
dta.add(cty);
}
// print the observable list to make sure it's full
System.out.println(dta);
// assign the columns in your tableview to the proper data in the city object
CityName.setCellValueFactory(cellData -> cellData.getValue().cityname);
Population.setCellValueFactory(cellData -> cellData.getValue().population);
// populate the tableview with the observable list
CityTable.setItems(dta);
// close the connection
c.close();
}catch(Exception e){
System.out.println("Failed");
System.out.println(e);
}
}
}
这是连接它所必需的 FXML 代码,我错过了大部分。
// I was forgetting to add the fx:id here (citytable)
<TableView fx:id="CityTable" layoutX="39.0" layoutY="64.0" prefHeight="200.0 prefWidth="200.0">
<columns>
// I was forgetting to add the fx:id here for the columns
<TableColumn prefWidth="75.0" text="City" fx:id="CityName" />
<TableColumn prefWidth="75.0" text="Population" fx:id="Population" />
</columns>
<items>
<FXCollections fx:factory="observableArrayList" />
</items>
</TableView>
新的class 使数据库值有意义
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* Model class for a City.
*/
public class City {
public final SimpleStringProperty cityname;
public final SimpleStringProperty population;
/**
* Default constructor.
*/
public City() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param cityname
* @param population
*/
public City(String cityname, String population) {
this.cityname = new SimpleStringProperty(cityname);
this.population = new SimpleStringProperty(population);
}
public String getCityName() {
return cityname.get();
}
public void setCityName(String cityname) {
this.cityname.set(cityname);
}
public StringProperty cityNameProperty() {
return cityname;
}
public String getPopulation() {
return population.get();
}
public void setPopulation(String population) {
this.population.set(population);
}
public StringProperty populationProperty() {
return population;
}
}
我一直在努力弄清楚如何让列表显示在我的 table 视图中。因为输出是我的列表,它很棒,但我可以在通过场景生成器制作的 table 中看到它。 table 具有相同的 fx:id,所以不是这样。数据显示正常并且正在编译,但 table.
上仍然没有数据public class FXMLDocument_PopulationDatabase_AllieBeckmanController implements Initializable {
@FXML
private ChoiceBox GetBox;
@FXML
private ChoiceBox SortBox;
@FXML
private TableView CityTable;
@FXML
private TableColumn COne;
@FXML
private TableColumn CTwo;
private final ObservableList dta = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
try{
// call the driver used to connect to the database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// connect to the city data base
Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/CityDB;create=true", "AllieBeckman", "1122");
// create a statement used to sort through the data in the database
Statement stmt = c.createStatement();
// create a result that will be used to get the data and translate it into variables
ResultSet result = stmt.executeQuery("SELECT * FROM city");
// get number of columns
ResultSetMetaData rsmd = result.getMetaData();
// continue sorting through and translating data until there are no more rows of data
while (result.next()){
// the 25 and 10 stand for two columns each is translated int its respective data type using
// the result set
System.out.println(result.getString("cityname") + result.getDouble("population"));
// adds one column
dta.add(result.getString("cityname"));
}
System.out.println(dta);
CityTable.setItems(dta);
// close the connection
c.close();
}catch(Exception e){
System.out.println("Failed");
System.out.println(e);
}
}
输出:
Executing C:\Users\Allie\Documents\NetBeansProjects\PopulationDatabase_AllieBeckman\dist\run1111906738\PopulationDatabase_AllieBeckman.jar using platform C:\Users\Allie\Desktop\Programs_and_Components\jdk1.8.0_101\jre/bin/java
New York 8.5505405E7
Los Angeles 3971883.0
Chicago 2720546.0
Huston 2296224.0
Philladelphia 1567442.0
Phoenix 1563025.0
San Antonio 1469845.0
San Diego 1394928.0
Dallas 1300092.0
San Jose 1026908.0
Austin 931830.0
Jacksonville 868031.0
San Francisco 864816.0
Indianapolis 853173.0
Columbus 850106.0
Fort Worth 833319.0
Charlotte 827097.0
Seattle 684451.0
Denver 682545.0
El Paso 681124.0
[New York , Los Angeles , Chicago , Huston , Philladelphia , Phoenix , San Antonio , San Diego , Dallas , San Jose , Austin , Jacksonville , San Francisco , Indianapolis , Columbus , Fort Worth , Charlotte , Seattle , Denver , El Paso ]
Deleting directory C:\Users\Allie\Documents\NetBeansProjects\PopulationDatabase_AllieBeckman\dist\run1111906738
感谢 INFOSYS link 它帮助我进行了这些更改并找到了我的答案 (http://code.makery.ch/library/javafx-8-tutorial/part2/)
我通过更改 FXML 文件中的一些内容来修复连接,并在代码中添加了一些新行,为了清楚起见我注释了这些代码。我还需要一个 class 用于我没有意识到的对象,现在它可以完美地从我创建的数据库中读取。
更改代码如下:
public class FXMLDocument_PopulationDatabase_AllieBeckmanController implements Initializable {
// connect the TableView from the xml file
@FXML
private TableView<City> CityTable;
// connect the table columns from the xml file
@FXML
private TableColumn<City, String> CityName;
@FXML
private TableColumn<City, String> Population;
// create an observable list to fill with data from DB and populate TableView
private final ObservableList<City> dta = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
try{
// call the driver used to connect to the database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// connect to the city data base
Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/CityDB;create=true", "AllieBeckman", "1122");
// create a statement used to sort through the data in the database
Statement stmt = c.createStatement();
// create a result that will be used to get the data and translate it into variables
ResultSet result = stmt.executeQuery("SELECT * FROM city");
// continue sorting through and translating data until there are no more rows of data
while (result.next()){
// print each line of data to check if it's right
System.out.println(result.getString("cityname") + result.getDouble("population"));
// make a new city object with each row in the database CityDB
City cty = new City(result.getString("cityname"), "" + result.getDouble("population"));
// add the new city object to the observable list
dta.add(cty);
}
// print the observable list to make sure it's full
System.out.println(dta);
// assign the columns in your tableview to the proper data in the city object
CityName.setCellValueFactory(cellData -> cellData.getValue().cityname);
Population.setCellValueFactory(cellData -> cellData.getValue().population);
// populate the tableview with the observable list
CityTable.setItems(dta);
// close the connection
c.close();
}catch(Exception e){
System.out.println("Failed");
System.out.println(e);
}
}
}
这是连接它所必需的 FXML 代码,我错过了大部分。
// I was forgetting to add the fx:id here (citytable)
<TableView fx:id="CityTable" layoutX="39.0" layoutY="64.0" prefHeight="200.0 prefWidth="200.0">
<columns>
// I was forgetting to add the fx:id here for the columns
<TableColumn prefWidth="75.0" text="City" fx:id="CityName" />
<TableColumn prefWidth="75.0" text="Population" fx:id="Population" />
</columns>
<items>
<FXCollections fx:factory="observableArrayList" />
</items>
</TableView>
新的class 使数据库值有意义
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* Model class for a City.
*/
public class City {
public final SimpleStringProperty cityname;
public final SimpleStringProperty population;
/**
* Default constructor.
*/
public City() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param cityname
* @param population
*/
public City(String cityname, String population) {
this.cityname = new SimpleStringProperty(cityname);
this.population = new SimpleStringProperty(population);
}
public String getCityName() {
return cityname.get();
}
public void setCityName(String cityname) {
this.cityname.set(cityname);
}
public StringProperty cityNameProperty() {
return cityname;
}
public String getPopulation() {
return population.get();
}
public void setPopulation(String population) {
this.population.set(population);
}
public StringProperty populationProperty() {
return population;
}
}