【发布时间】:2016-11-12 06:11:39
【问题描述】:
我一直在努力弄清楚如何让列表显示在我的表格视图中。因为它的输出是我的列表,这很棒,但我可以在我通过场景生成器制作的表格中看到它。该表具有相同的 fx:id,所以不是这样。数据显示正常,正在编译,但表格上仍然没有数据。
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
【问题讨论】:
-
你用什么
cellValueFactorys 来处理这些列,你如何访问给定城市名称的人口? -
我正在使用预填充的 javaDB 来获取值并尝试传输它们。我找到了答案,并开始使用 CellValueFactory cellData.getValue 从该数据库拉到 TableView 列,谢谢:)
标签: javafx scenebuilder javadb observablelist