我正在尝试使用桌面GUI应用程序比较HIVE和Oracle表数据,因此我使用JavaFX并构建应用程序,能够从表单中获取用户输入的数据并在后端执行它并获取结果集但是 我无法在应用程序的ResultSet选项卡中打印结果集 我已经尝试了使用here的方法并浏览了一些其他的堆栈问题,但作为JFX的新手我无法获得如何将结果集打印到场景中
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1100.0" tabClosingPolicy="UNAVAILABLE" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40"
fx:controller="application.Controller">
<tabs>
<Tab text="Query Tab">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextArea layoutX="34.0" layoutY="143.0" prefHeight="506.0" prefWidth="360.0" promptText="Hive Query" fx:id="HiveQuery" />
<TextArea layoutX="506.0" layoutY="141.0" prefHeight="508.0" prefWidth="384.0" promptText="Oracle Query" fx:id="OracleQuery" />
<TextField layoutX="34.0" layoutY="27.0" promptText="Hive Server Name" fx:id="HiveServerName" />
<TextField layoutX="34.0" layoutY="79.0" promptText="Hive Username" fx:id="HiveUserName" />
<PasswordField layoutX="204.0" layoutY="79.0" promptText="Hive Password" fx:id="HivePassword" />
<PasswordField layoutX="698.0" layoutY="79.0" promptText="Oracle Password" fx:id="OraclePassword" />
<TextField layoutX="521.0" layoutY="79.0" promptText="Oracle Username" fx:id="OracleUserName" />
<TextField layoutX="521.0" layoutY="27.0" promptText="Oracle HostName" fx:id="OracleHostName" />
<Button layoutX="420.0" layoutY="666.0" mnemonicParsing="false" text="Compare" fx:id="CompareButton" onAction="#CompareDataSets" />
</children></AnchorPane>
</content>
</Tab>
<Tab text="Result Set">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView layoutX="8.0" layoutY="19.0" prefHeight="671.0" prefWidth="433.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
</TableView>
<TableView layoutX="463.0" layoutY="19.0" prefHeight="671.0" prefWidth="448.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
</TableView>
</children></AnchorPane>
</content>
</Tab>
答案 0 :(得分:1)
按照上面链接的示例,您可以执行相同的逻辑,只需稍微调整一下......首先在FXML中命名tableView,然后删除存在的列,以便只有tableView
<Tab text="Result Set">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tableView" layoutX="8.0" layoutY="19.0" prefHeight="671.0" prefWidth="433.0">
<columns>
</columns>
</TableView>
......
</children></AnchorPane>
</content>
</Tab>
然后更新您的控制器:
//Name your table view in your FXML file and wire it like this.
@FXML
private TableView tableview;
//Then in your method where you process the data you can add columns dynamically....
/**********************************
* TABLE COLUMN ADDED DYNAMICALLY *
**********************************/
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
System.out.println("Column ["+i+"] ");
}
//Etc....
这是您可以动态地将列添加到FXML中列出的tableview。