Kev
Kev

Reputation: 11

adding data into JavaFX tableview

I'm trying to push my data that I'm getting from a RethinkDB into a JavaFx TableView, however, the changes do not appear in the tableview and I can't figure out why.

I'm pretty new to JavaFx so I hope you can help me.

Here are my classes : (I didn't include my memory classes where I save the data from the DB)

RethinkDBConnect class

public class RethinkDBConnect {

    public static final RethinkDB r = RethinkDB.r;
    GsonConverter con = new GsonConverter();    
    JiraTicketBody body = new JiraTicketBody();
    ViewController viewcon = new ViewController();
    TicketDataProperty tickprop = new TicketDataProperty(null, null, null, null, null, null);

    public void Connection(){

        viewcon.list.add(newTicketDataProperty
            ("test","test","test","test","test","test"));
    }
}

TicketDataProperty class

public class TicketDataProperty {

    private final SimpleStringProperty key;
    private final SimpleStringProperty prioritaet;
    private final SimpleStringProperty erstellt;
    private final SimpleStringProperty status;
    private final SimpleStringProperty zustand;
    private final SimpleStringProperty beschreibung;

    public TicketDataProperty(String key, String prioritaet, String erstellt,
            String status, String zustand, String beschreibung)
    {
        this.key = new SimpleStringProperty(key);
        this.prioritaet = new SimpleStringProperty(prioritaet);
        this.erstellt = new SimpleStringProperty(erstellt);
        this.status = new SimpleStringProperty(status);
        this.zustand = new SimpleStringProperty(zustand);
        this.beschreibung = new SimpleStringProperty(beschreibung);
    }

    public String getKey() {
        return key.get();
    }

    public void setKey(String value) {
        key.set(value);
    }

    public String getPrioritaet() {
        return prioritaet.get();
    }

    public void setPrioritaet(String value) {
        prioritaet.set(value);
    }

    public String getErstellt() {
        return erstellt.get();
    }

    public void setErstellt(String value) {
        erstellt.set(value);
    }

    public String getStatus() {
        return status.get();
    }

    public void setStatus(String value) {
        status.set(value);
    }

    public String getZustand() {
        return zustand.get();
    }

    public void setZustand(String value) {
        zustand.set(value);
    }

    public String getBeschreibung() {
        return beschreibung.get();
    }

    public void setBeschreibung(String value) {
        beschreibung.set(value);
    }   
}

ViewController class

public class ViewController implements Initializable {

    TicketDataProperty tickdat = new TicketDataProperty(null, null, null, null, null, null);

    @FXML private TableView <TicketDataProperty> table;
    @FXML private TableColumn <TicketDataProperty,String> key;
    @FXML private TableColumn <TicketDataProperty,String> prioritaet;
    @FXML private TableColumn <TicketDataProperty,String> erstellt;
    @FXML private TableColumn <TicketDataProperty,String> status;
    @FXML private TableColumn <TicketDataProperty,String> zustand;
    @FXML private TableColumn <TicketDataProperty,String> beschreibung;

    public ObservableList<TicketDataProperty> list = FXCollections.observableArrayList(
            new TicketDataProperty("example","example","example","example","example","example")
            );

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        key.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("key"));
        prioritaet.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("prioritaet"));
        erstellt.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("erstellt"));
        status.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("status"));
        zustand.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("zustand"));
        beschreibung.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("beschreibung"));
        table.setItems(list);
    }
}

GsonConverter class

public class GsonConverter {

    public JiraTicketBody gson(String json) 
    {
        Gson gson = new Gson();
        JiraTicketBody BodyObj = gson.fromJson(json,JiraTicketBody.class);
        return BodyObj;
    }
}

Main class

public class Main extends Application 
{

    //ViewXML
    @Override
    public void start(Stage primaryStage) throws IOException 
    {
        Parent root = FXMLLoader.load(getClass().getResource("/view/ViewXML.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("Ticket System Application");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    public static void main(String[] args) 
    {
        try {

            //ViewXML
            launch(args);

            RethinkDBConnect obj = new RethinkDBConnect();
            obj.Connection();
        } catch(Exception e) {

        }
    }
}

Upvotes: 1

Views: 1655

Answers (1)

James_D
James_D

Reputation: 209225

There are two issues with the code you have posted.

  1. As stated in the documentation, Application.launch() blocks until the application exits. So you don't even create the RethinkDBConnection class until the application is in the process of closing. You should consider the start() method to be the entry point to the application, and should have no code other than launch() in the main(...) method. Anything you do in a JavaFX application should be done in the start(...) or init(...) methods, or in methods invoked from there, etc.

    In this case, since you don't seem to need RethinkDBConnection from outside the controller, I see no reason not to create RethinkDBConnect from the controller itself.

  2. You need to update the list that belongs to the controller. Instead, you are creating a new object that happens to be the same class as the controller, and updating the list that belongs to that object. Obviously, that list has nothing at all to do with the table. You need to pass a reference to the actual list that is used as the backing list for the table to the RethinkDBController instance.

So your code should look like:

public class RethinkDBConnection {

    // public static final RethinkDB r = RethinkDB.r;
    // GsonConverter con = new GsonConverter();    
    // JiraTicketBody body = new JiraTicketBody();

    private final ObservableList<TicketDataProperty> dataList ;

    public RethinkDBConnection(ObservableList<TicketDataProperty> dataList) {
        this.dataList = dataList ;
    }

    public void connect(){

        dataList.add(new TicketDataProperty
            ("test","test","test","test","test","test"));



    }
}

Then in the controller you can do:

public class ViewController implements Initializable {


    @FXML private TableView <TicketDataProperty> table;
    @FXML private TableColumn <TicketDataProperty,String> key;
    @FXML private TableColumn <TicketDataProperty,String> prioritaet;
    @FXML private TableColumn <TicketDataProperty,String> erstellt;
    @FXML private TableColumn <TicketDataProperty,String> status;
    @FXML private TableColumn <TicketDataProperty,String> zustand;
    @FXML private TableColumn <TicketDataProperty,String> beschreibung;

    private ObservableList<TicketDataProperty> list = FXCollections.observableArrayList(
            new TicketDataProperty("example","example","example","example","example","example")
            );

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        key.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("key"));
        prioritaet.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("prioritaet"));
        erstellt.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("erstellt"));
        status.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("status"));
        zustand.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("zustand"));
        beschreibung.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("beschreibung"));
        table.setItems(list);

        RethinkDBConnection connection = new RethinkDBConnection(list);
        connection.connect();
    }
}

And your Main class should just be:

public class Main extends Application 
{

    //ViewXML
    @Override
    public void start(Stage primaryStage) throws IOException 
    {
        Parent root = FXMLLoader.load(getClass().getResource("/view/ViewXML.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("Ticket System Application");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    public static void main(String[] args) {

        launch(args);
        
    }
}

If you really do need access to your RethinkDBConnection instance outside the controller, then modify the controller as follows:

public class ViewController implements Initializable {

    @FXML private TableView <TicketDataProperty> table;
    @FXML private TableColumn <TicketDataProperty,String> key;
    @FXML private TableColumn <TicketDataProperty,String> prioritaet;
    @FXML private TableColumn <TicketDataProperty,String> erstellt;
    @FXML private TableColumn <TicketDataProperty,String> status;
    @FXML private TableColumn <TicketDataProperty,String> zustand;
    @FXML private TableColumn <TicketDataProperty,String> beschreibung;

    private ObservableList<TicketDataProperty> list = FXCollections.observableArrayList(
            new TicketDataProperty("example","example","example","example","example","example")
            );

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        key.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("key"));
        prioritaet.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("prioritaet"));
        erstellt.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("erstellt"));
        status.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("status"));
        zustand.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("zustand"));
        beschreibung.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("beschreibung"));
        table.setItems(list);

    }

    public ObservableList<TicketDataProperty> getDataList() {
        return list ;
    }
}

and use this version of Main:

public class Main extends Application 
{

    //ViewXML
    @Override
    public void start(Stage primaryStage) throws IOException 
    {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/ViewXML.fxml"));
        Parent root = loader.load();

        ViewController controller = loader.getController();
        RethinkDBConnection connection = new RethinkDBConnection(controller.getDataList());
        connection.connect();

        Scene scene = new Scene(root);
        primaryStage.setTitle("Ticket System Application");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    public static void main(String[] args) {

        launch(args);
        
    }
}

Note that I renamed some classes and methods to adhere to standard naming conventions.

Upvotes: 2

Related Questions