0

In my JavaFX application, I am looking to switch the table view's population method from an ObservableList of objects to using a HashMap of String/Object values. This project utilizes SQL commands and I would like to use the primary key as the key for the hashmap while saving the object as the value. From there, I would like to use the object value to populate the columns.

Currently, the parts of my project are:

Truck

public class Truck {
    private String id;
    private int capacity;

(constructor/getters)

}

ObservableList method

    public ObservableList<Truck> getTrucksList() {
        ObservableList<Truck> trucksList = FXCollections.observableArrayList();
        Connection conn = getConnection();
        String query = "SELECT * FROM trucks";
        Statement st;
        ResultSet rs;

        try {
            st = conn.createStatement(); // connect to database
            rs = st.executeQuery(query);
            Truck truck;
            while (rs.next()) {
                truck = new Truck(
                        rs.getString("truckID"),
                        rs.getInt("capacity")
                );
                trucksList.add(truck);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return trucksList;
    }

Table population:

    public void showTrucks() {
        ObservableList<Truck> list = getTrucksList();

        colTruckID.setCellValueFactory(new PropertyValueFactory<Truck, String>("id")); // col 1
        colCapacity.setCellValueFactory(new PropertyValueFactory<Truck, Integer>("capacity")); // col 2


        tvTrucks.setItems(list); // tvTrucks is the TableView object
    }

Here is what I tried for the ObservableMap-returning "getTrucksMap" function:

public ObservableMap<String, Truck> getTrucksMap() {
        ObservableMap<String, Truck> trucksMap = FXCollections.observableHashMap();
        Connection conn = getConnection();
        String query = "SELECT * FROM trucks";
        Statement st;
        ResultSet rs;

        try {
            st = conn.createStatement(); // connect to database
            rs = st.executeQuery(query);
            Truck truck;
            while (rs.next()) {
                truck = new Truck(
                        rs.getString("truckID"),
                        rs.getInt("capacity")
                );
                trucksMap.put(truck.getId(), truck);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return trucksMap;
    }

When trying to flip over to an ObservableMap, I had issues where it kept prompting me to cast to an ObservableList and it would produce an error when I took the recommended solution. I edited the "getTruckList" method to return an ObservableMap that was <String, Truck>, but the issue was in "showTrucks" where I could not understand how to populate the table and its columns to have the truck ID and capacity values. For example, the table would look like this:

ID Capacity
A 450
B 550
C 350

And the map would look like:

{ "A", Truck("A", 450) }, { "B", Truck("B", 550) }, { "C", Truck("C", 350) }

I believe adding a map would help associate data a little more strongly across scenes in my project. The hashmap could be a wrapper for the SQL data going in and out.


Ultimately — How can I make it so that my "getTrucksMap" function can populate a table?

jwald3
  • 3
  • 2

0 Answers0