-1

I have an ordersFXML which has a tableview (ordersTable). When you click a button that pops up another paymentsFXML (payments)which contains a payButton. After clicking payButton paymentsFXML closes. My problem is here. I want ordersTable to be cleared up, emptied as soon as payButton is clicked.

Is there any way to do it? Here is my code below.

OrdersFXMLController.java

@FXML
private TableView<Orders> tableOrders;


@FXML
public void clearAll(){

    tableOrders.setItems(null);

}

PaymentsFXMLController.java

@FXML
private void finilizePayment(ActionEvent event){
// some code here
closeButtonAction();
}

@FXML
private void closeButtonAction(){

    FXMLLoader loader = new FXMLLoader();
    OrdersFXMLController orderController = (OrdersFXMLController)loader.getController();

    orderController.clearAll();
   }

And, here is the error code:

 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

Thanks in advance.

user1971804
  • 111
  • 1
  • 3
  • 11

1 Answers1

0

There is always a way; in fact, you have a few different options for this.

Since you did not post any of your code, I am going to make some assumptions with this answer and just provide possible concepts, not necessarily the best way to do it; it all depends on how you've implemented your data model and such.

One way is to include a public method in your ordersFXML controller (which I'll refer to as ordersController) that allows you to clear the table from your paymentsController class:

public void clearOrdersTable() {
    tableOrders.setItems(null);
}

After doing that, you will need to pass a reference for ordersController to your paymentsController class when calling its constructor. If creating the controller from your ordersController class:

PaymentsController paymentsController = new PaymentsController(this);

At this point, you have the ability to access your ordersController from your paymentsController and you've set up a method you can use to clear the list of orders in your TableView. Now you just need to call that method after processing the payment:

ordersController.clearOrdersTable();

Again, some assumptions are made. If you are populating your orders table with an actual underlying list (which you should be), you should clear that list instead of setting the table's items to null.

Without seeing your code, though, this may not work for you but will hopefully give you a good starting point for your own troubleshooting.

For further reading and to help yourself grasp the concept of passing values between different controllers, check out jewelsea's excellent answer to this question: Passing Parameters JavaFX FXML.

Zephyr
  • 9,885
  • 4
  • 28
  • 63