0

after I changed my code with lead of this helpful post (PASSING PARAMETERS JAVAFX FXML) my setTab function works not really. Its gets called but doesn't do what it's supposed to do.

Main.java

public class Main extends Application {

public static void main(String[] args) {
    launch(args);
}
@Override
public void start(Stage primaryStage){
        Controller1 controller1 = new Controller1();
        controller1.showStage();
  }
}

Controller1.java

public class Controller1{

    private final Stage thisStage;
    private Controller2 controller2 = new Controller2(this);

    public Controller1() {
    thisStage = new Stage();

    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Steuerung.fxml"));
        loader.setController(this);
        thisStage.setScene(new Scene(loader.load()));
        thisStage.setTitle("Steuerung");
    }catch (IOException e){
        e.printStackTrace();
     }
    }

    public void showStage(){ thisStage.show();}

    @FXML private void initialize() {
        b_AnzeigeÖffnen.setOnAction(e -> openAnzeige());
        b_Ecke.setOnMouseClicked(e -> controller2.setTab(2));
    }

    private void openAnzeige(){
        Controller2 controller2 = new Controller2(this);
        controller2.showStage();
    }

    @FXML
    private Label b_Ecke;

    @FXML
    private MenuItem b_AnzeigeÖffnen

}

Controller2.java

public class Controller2 {

private Stage thisStage;

private final Controller1 controller1;

public Controller2(Controller1 controller1) {
    this.controller1 = controller1;
    thisStage = new Stage();
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Anzeige.fxml"));
        loader.setController(this);
        thisStage.setScene(new Scene(loader.load()));
        thisStage.setTitle("Spielstand");

        }catch (IOException e){
            e.printStackTrace();
        }
}

public void showStage() { thisStage.show();}


@FXML
private TabPane TP_A;

@FXML
private Tab tA_Home;

@FXML
private Tab tA_EckePreview;

@FXML
private Tab tA_EckVerhältnis;

@FXML
private Tab tA_Spielverlauf;

@FXML
private Tab tA_Spielstatistik;

@FXML
private Tab tA_GelbeKarte;

@FXML
private Tab tA_RoteKarte;

@FXML
private Tab tA_Elfmeter;

@FXML
private Tab tA_Auswechselung;


@FXML public void setTab(Integer i) {
   System.out.println("TABSWITCH");
    switch(i) {
        case 1: TP_A.getSelectionModel().select(tA_Home); break;
        case 2: TP_A.getSelectionModel().select(tA_EckePreview); break;
        case 3: TP_A.getSelectionModel().select(tA_EckVerhältnis); break;
        case 4: TP_A.getSelectionModel().select(tA_Spielverlauf); break;
        case 5: TP_A.getSelectionModel().select(tA_Spielstatistik); break;
        case 6: TP_A.getSelectionModel().select(tA_GelbeKarte); break;
        case 7: TP_A.getSelectionModel().select(tA_RoteKarte); break;
        case 8: TP_A.getSelectionModel().select(tA_Elfmeter); break;
        case 9: TP_A.getSelectionModel().select(tA_Auswechselung); break;
        //case 10: TP_A.getSelectionModel().select(tA_Nachspielzeit/Verlängerung); break; -> TODO
        default: System.out.println("Fehler: Tab in TP_A nicht vorhanden!"); break;
        }
    }
}

!packages and imports are removed bc of Body length!

I know that I have to rewrite the last few functions as they could easy be one.

Steuerung.fxml and Anzeige.fxml would be to long. If u want to see them tho, here is the complete GitLab project. GitLab Project

Sorry for the German comments, function/var names and or objects

Thans for ur advice!

Edit: Grammar

James_D
  • 201,275
  • 16
  • 291
  • 322
t0mbrn
  • 3
  • 3
  • 1
    [mcve] please .. mind the __M__, just a minimal ui, a single button that should be triggered and still complete, must be reproducible that is contain controller, fxml, boilerplate app. And unrelated: java naming conventions please! – kleopatra May 14 '20 at 11:10
  • The `Controller2` instance on which you call `setTab()` is not the instance on which you called `showStage()`, so you won't see any effect. – James_D May 14 '20 at 11:57
  • Count you please tell me, what i should change then? Im kinda new in JavaFX. – t0mbrn May 14 '20 at 12:12
  • *Could you please tell me – t0mbrn May 14 '20 at 12:29
  • Well, it depends what behavior you want. If you want a new `Controller2` instance every time the `b_AnzeigeÖffnen` button is pressed, then you need to create a new instance (as you currently do), but save it in the instance variable. If you just want to show the instance you already created (i.e. show or re-show the same one each time), then don't create a new instance in that event handler. – James_D May 14 '20 at 12:33
  • could you give me a code example how to do that if I don't want to create a new instance every time? or maybe a link? – t0mbrn May 14 '20 at 12:51

1 Answers1

0

In Controller1 you create a Controller2 instance as an instance variable:

private Controller2 controller2 = new Controller2(this);

and when the user clicks on the b_Ecke label, you change the tab in that instance:

b_Ecke.setOnMouseClicked(e -> controller2.setTab(2));

However, there is nowhere in your code where you ever call showStage on that instance of Controller2, so you are changing the tab selection in something that is not displayed.

When the user selects the b_AnzeigeÖffnen menu item, you create a new Controller2 instance, and display that instance:

b_AnzeigeÖffnen.setOnAction(e -> openAnzeige());

private void openAnzeige(){
    Controller2 controller2 = new Controller2(this);
    controller2.showStage();
}

Instead, just display the Controller2 instance you have already created:

b_AnzeigeÖffnen.setOnAction(e -> openAnzeige());

private void openAnzeige(){
    // Controller2 controller2 = new Controller2(this);
    controller2.showStage();
}
James_D
  • 201,275
  • 16
  • 291
  • 322