Rainier laan
Rainier laan

Reputation: 1130

JavaFX Scenebuilder fill listView from database

I made a JavaFX Scenebuilder "Application" and I need to fill my listView with things from my database. The problem is that I don't know how and I don't know where to look.

Is there someone who can help me out?

Here is the code with my connection to the database. That's the only thing I have. And the scene builder "Sample.fxml" file of course.

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();
    }

This is my 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;

}

Upvotes: 2

Views: 11257

Answers (2)

Maurice
Maurice

Reputation: 357

You could do the following (code not tested):

Main.java:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        primaryStage.setTitle("Eindopdracht Java Periode 4");
        primaryStage.setScene(new Scene(root, 700, 650));
        primaryStage.show();
    }
}

Controller.java (associated with a fxml that contains a ListView with id=list):

public class Controller implements Initializable{
   @FXML
   private ListView<String> list;

   private ObservableList<String> items = FXCollections.observableArrayList();

   @Override
   public void initialize(URL location, ResourceBundle resources) {
       list.setItems(items);

       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()) {
               items.add(rs.getString(1));
               System.out.println(rs.getString(1));
           }
       } catch (SQLException e) {

       }

   }

}

By implementing the initialize method you could load the contents whenever the scene is shown.

Upvotes: 0

Maurice
Maurice

Reputation: 357

I would go for the following code:

First define your listView and an observable list (assuming that you have a ListView in your fxml with the id "list"):

@FXML
ListView<String> list;

ObservableList<String> items = FXCollections.observableArrayList();

Then set the list view to the items list:

list.setItems(items);

in your while loop simply add the results to the items list:

items.add(rs.getString(1));

Upvotes: 4

Related Questions