-1

I have a file named Home.fxml which is a BorderPane. This component has buttons on the left and an empty TabPane called mainTabPane in the centre. I would like, when a user clicks on any button on the left, to add a tab to mainTabPane. I am able to add one tab when one button is clicked. But when I try to do the same for the second button, I get this error:

java.lang.IllegalStateException: Location not set

Here is the code for HomeController.java, where all is done:

public void initialize(URL loc, Resource Rse) {
    createReportsTab();
    loadCreateItemTab();
}

/*this is the one which works well*/
@FXML
void CreateReportTab() { 
    FXMLLoader loader = new FXMLLoader(); 
    loader.setLocation(ReportsController.class.getResource("gui/Reports.fxml"));
    Parent parent = loader.load(); 
    Tab reportsTab = new Tab("Reporting Engine");
    reportsTab.setClosable(true);
    reportsTab.setcontent(parent);
    mainTabPane.getTabs().add(reportsTab),
}

/*this is the one which produces the error*/
@FXML
void loadCreateItemTab() { 
    FXMLLoader loader = new FXMLLoader(); 
    loader.setLocation(AddNewItemController.class.getResource("gui/addNewItem.fxml"));
    Parent parent = loader.load(); 
    Tab newItemTab= new Tab("New Item");
    newItemTab.setClosable(true);
    newItemTab.setcontent(parent);
    mainTabPane.getTabs().add(newItemTab),
}

I am confused; why the Reporting Engine loads well but the second one is producing that error above?

Here is my project structure in IntelliJ IDEA:

enter image description here

All controller classes are in the controllers package and all FXML files are in the GUI package. The main class is in Companion package.

0009laH
  • 1,960
  • 13
  • 27
okimait
  • 17
  • 9

1 Answers1

0

Error suggests that addNewItem.fxml is not read. Change:

AddNewItemController.class.getResources("gui/addNewItem.fxml");

to

ReportsController.class.getResources("gui/addNewItem.fxml");

, since this path worked for you already.

0009laH
  • 1,960
  • 13
  • 27
  • This is what someone pointed me to `URL url = getclass.getResource("gui/addNewItem.fxml"); Parent root = FXMLLoader.load(url)`that worked well. but `addNewTab.setClosable(true)` is not working, am not able to close a tab once it is opened. any idea please @Andrzej Pazurkiewicz – okimait Oct 20 '20 at 12:27
  • Two ways to do it, either in your FXM where is your TabPane tag add tabClosingPolicy="SELECTED_TAB", or within code yourTabPane.setClosingPolicy(TabPane.TabClosingPolicy.SELECTED_TAB); – Andrzej Pazurkiewicz Oct 20 '20 at 12:57