编辑并删除所有未使用的代码
使用JDK 8和eclipse的Javafx /场景构建器的新手。
Sql DB连接正常工作并拉到一个记录集,该记录集填充虚拟Tableview,system.out打印db记录等。我正在使用场景构建器并尝试在scenebuilder中填充FXML定义的Tableview
,这很有趣学习。
我无法将数据传送到tableview。
我向private static ObservableList<ObservableList<String>> data;
添加了静态,它已经停止了我的nullPointerException并添加了public void initialize(URL location, ResourceBundle resources)
,它告诉我ObservableList
数据有一些数据,并观看了许多YouTube视频。
我现在没有错误,但在定义的tableview中看不到任何数据。当我将一个列添加到没有id的scenebulder中时,我会得到不同颜色的行,这让我觉得它正在做一些,控制器附加在scenebuilder中。
我只是想拉出所有表格列,现在只是为了测试,然后我可以从那里继续。为凌乱的代码道歉,但也可以在第一周留下它。
我真的很感激任何帮助。
控制器,省略了进口
public class SoftwareController extends Application implements Initializable {
private static ObservableList<ObservableList<String>> data;
@FXML
public TableView<ObservableList<String>> tblSoftware;
public Statement st;
public Connection conn;
public static void main(String[] args) {
launch(args);
}
public void buildData() {
data = FXCollections.observableArrayList();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://IP;databaseName=CMDB";
conn = DriverManager.getConnection(url,"cmdbadmin","cmdbadmin!1");
System.out.print("connection successfulltttt");
String SQL = "SELECT * from Data_CMDB_Main";
ResultSet rs = conn.createStatement().executeQuery(SQL);
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++) {
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
System.out.println(col);
}
while (rs.next()){
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++) {
row.add(rs.getString(i));
}
data.add(row);
System.out.println(row); //shows records from database
}
}catch (Exception e) {
e.printStackTrace();
System.out.println("Error building data");
}
}
@Override
public void start(Stage stage) throws Exception {
buildData();
}
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Software.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage1 = new Stage();;
stage1.setScene(new Scene(root1));
stage1.show();
}
public Label lblTest;
public void btnSoftwarePressed(ActionEvent event) {
lblTest.setText("label working");
}
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println(data);
if(data !=null){
System.out.println("data is not null");
tblSoftware.getItems().addAll(data);
}
else System.out.println("data is null");
}
}
FMXL
<?import java.lang.*?>
<?import java .util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="368.0" prefWidth="433.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.SoftwareController">
<children>
<TableView fx:id="tblSoftware" layoutY="102.0" prefHeight="266.0" prefWidth="433.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="102.0">
<columns>
<TableColumn prefWidth="75.0" text="Column X" />
</columns>
</TableView>
<Button fx:id="btnSoftware" layoutY="63.0" mnemonicParsing="false" onAction="#btnSoftwarePressed" text="Button" />
<Label fx:id="lblTest" layoutX="226.0" layoutY="63.0" prefHeight="26.0" prefWidth="130.0" text="22" />
</children>
</AnchorPane>
答案 0 :(得分:0)
我想在不使用单独的控制器和应用程序文件的情况下尝试查看它是如何工作的。
请注意,我必须在James_D提到的initialize方法中调用buildData。
这预计你有H2作为库,但我认为你的连接没问题,所以只需删除H2部分并取消注释你的连接。
package fxml;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class SoftwareController extends Application implements Initializable {
private static ObservableList<ObservableList<String>> data;
@FXML public TableView<ObservableList<String>> tblSoftware;
public void buildData() throws Exception{
data = FXCollections.observableArrayList();
DriverManager.registerDriver(new org.h2.Driver());
Connection conn = DriverManager.getConnection("jdbc:h2:mem:");
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE Data_CMDB_Main (name VARCHAR(255), address VARCHAR(255))";
stmt.executeUpdate(sql);
for (int i = 0; i < 10; i++) {
sql = "INSERT INTO Data_CMDB_Main VALUES ("
+ "'1st string in row " + i + "','2nd string in row " + i + "')";
stmt.executeUpdate(sql);
}
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// String url = "jdbc:sqlserver://IP;databaseName=CMDB";
// Connection conn = DriverManager.getConnection(url, "cmdbadmin", "cmdbadmin!1");
// System.out.print("connection successfulltttt");
String SQL = "SELECT * from Data_CMDB_Main";
ResultSet rs = conn.createStatement().executeQuery(SQL);
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++) {
final int j = i;
TableColumn<ObservableList<String>, String> col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(p -> new ReadOnlyStringWrapper(p.getValue().get(j)));
tblSoftware.getColumns().add(col);
}
while (rs.next()) {
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
row.add(rs.getString(i));
}
data.add(row);
}
tblSoftware.setItems(data);
}
@Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Software.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
stage.setScene(new Scene(root1));
stage.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
buildData();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@FXML private void btnSoftwarePressed(){}
public static void main(String[] args) { launch(args);}
}