我制作了一个JavaFX Scenebuilder“应用程序”,我需要用数据库中的内容填充我的listView。问题是我不知道如何查看,也不知道去哪里查看。
有人可以帮助我吗?
这是我与数据库的连接的代码。这是我唯一拥有的东西。当然还有场景构建器“Sample.fxml”文件。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Connection Conn = null;
try {
Conn = DriverManager.getConnection("jdbc:mysql://localhost/hitdossier", "root", "");
System.out.println("Verbonden met de database");
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("Verbinding met de database is mislukt.");
}
Statement stmt = null;
ResultSet rs = null;
try {
stmt = Conn.createStatement();
rs = stmt.executeQuery("SELECT naam FROM artiest");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
}
primaryStage.setTitle("Eindopdracht Java Periode 4");
primaryStage.setScene(new Scene(root, 700, 650));
primaryStage.show();
}
这是我的Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class Controller {
@FXML
private ComboBox<String> cboWeek;
}
最佳答案
我会选择以下代码:
首先定义你的listView和一个可观察列表(假设你的fxml中有一个ID为“list”的ListView):
@FXML
ListView<String> list;
ObservableList<String> items = FXCollections.observableArrayList();
然后将 ListView 设置为项目列表:
list.setItems(items);
在 while 循环中只需将结果添加到项目列表中:
items.add(rs.getString(1));
关于JavaFX Scenebuilder从数据库填充listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484319/