1

I have this problem I'm trying to use tabPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER); Inside AppSceneController.java

This code works very well but I want to try in FXML file ,

public class App extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        TabPane tabPane = new TabPane();
        tabPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);
        Tab tab1 = new Tab("Tab1", new Label("Show all Tab1 available"));
        Tab tab2 = new Tab("Tab2", new Label("Show all Tab2 available"));
        tabPane.getTabs().add(tab1);
        tabPane.getTabs().add(tab2);
        Scene scene = new Scene(tabPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

My Example Code In : AppSceneController.java

public class AppSceneController {

    @FXML
    private TabPane mainTabPane;

    @FXML
    void initialize() { 
        mainTabPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER);
    }
}

My Example Code in My Main : App.java

public class App extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
       
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("App.fxml"));
        Scene scene = new Scene(root);

        primaryStage.setTitle("Hello, World!");
        primaryStage.setMaximized(true);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

My FXML File Looks looks like, enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65
kalopseeia
  • 73
  • 2
  • 7
  • `TabPane` is defined in the FXML. By `TabPane tabPane = new TabPane()` you are constructing a new `TabPane` instead of configuring the one defined in the FXML. Get a reference to the FXML `TabPane` by `@FXML TabPane tp` where tp is the fx id. – c0der Feb 16 '22 at 06:36
  • @c0der can you please look on my edited question again,. Still not working but it compile without error. – kalopseeia Feb 16 '22 at 06:50
  • 2
    @c0der Thanks It was working now, Wrong fx id name, – kalopseeia Feb 16 '22 at 06:54
  • 1
    Consider posting an answer to your own question to make it useful for future readers. – c0der Feb 16 '22 at 06:57
  • a note for future questions: debugging problems (as this is) _require_ a [mcve] (your code is near, but missing the fxml - that's why nobody could notice the incorrect fx:id :). As to the question itself which changed over time: should be probably be closed - the version with instantiating an injected field is a duplicate of https://stackoverflow.com/questions/36186907/javafx-label-null-pointer-exception or the incorrect id is a typo. And the title seems to not really match any of problems - it's possible to set the policy in the fxml (hmm .. or not?) – kleopatra Feb 16 '22 at 12:36
  • @c0der _to make it useful for future readers_ I disagree that it can be useful, it's either a duplicate or a typo, IMO :) – kleopatra Feb 16 '22 at 12:37
  • @kleopatra closing it would certainly not be a big loss (0: – c0der Feb 16 '22 at 12:51

0 Answers0