0

I made a NavigationBar fxml document, and linked that to a NavigationController class. I've managed to change scenes when pressing the different buttons, but I want to make the button of the current scene focused/different color. However, when I change the style nothing happens.

This is my NavigationBar:

    <HBox alignment="CENTER_LEFT" prefHeight="43.0" prefWidth="600.0" spacing="10.0" BorderPane.alignment="CENTER" fx:controller="com.kalkulator.snuskalkulator.NavigationController" xmlns="http://javafx.com/javafx"
          xmlns:fx="http://javafx.com/fxml" stylesheets="assets/focused.css">
        <children>
            <Button mnemonicParsing="false" fx:id="profileBtn" onAction="#handleProfileBtn" text="Profil"/>
            <Button mnemonicParsing="false" fx:id="snusBtn" onAction="#handleSnusBtn" text="Snus" />
            <Button mnemonicParsing="false" fx:id="taperBtn" onAction="#handleTaperBtn" text="Nedtrapning" />
            <Button mnemonicParsing="false" fx:id="settingsBtn" onAction="#handleSettingsBtn" text="Innstillinger" />
            <Button mnemonicParsing="false" fx:id="graphBtn" onAction="#handleGraphBtn" text="Graf" />
        </children>
        <BorderPane.margin>
            <Insets />
        </BorderPane.margin>
        <padding>
            <Insets left="20.0" />
        </padding>
    </HBox>

This is my NavigationController:

public class NavigationController {



    @FXML
    Button profileBtn, graphBtn, settingsBtn, taperBtn, snusBtn;

    public void handleSettingsBtn() throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Innstillinger.fxml"));
        Stage window = (Stage) settingsBtn.getScene().getWindow();
        settingsBtn.setStyle("-fx-background-color: red;");
        window.setScene(new Scene(root, 500, 500));
    }


}

Here I try to change the color of the settingsBtn, but nothing happens. Inside of "Innstillinger.fxml" I I include the fxml of the NavigationBar like this:

<fx:include source="NavigationBar.fxml"/>

I have no clue what the reason for the style not changing is. Any help would be appreciated.

Elias Knudsen
  • 315
  • 2
  • 9
  • @James_D So The button is still visible, but the style does not do anything. Where else should i change the style? – Elias Knudsen Apr 21 '22 at 15:37
  • 1
    The button is not still visible. The button is part of the scene which you have replaced. – James_D Apr 21 '22 at 15:41
  • 1
    OK, I think I see what you are trying to do. It looks like you are creating a new navigation bar and displaying it when you load `Innstillinger.fxml`? (By including `Navigation.fxml` in `Innstillinger.fxml`.) In that case you need to change the style of the *new* settings button that is created when you load `Innstillinger.fxml`. You need to do that in the controller for `Innstillinger.fxml` (by injecting the new instance of the `NavigationController` into that controller). Post a complete [mre] (complete but minimal, so probably just two scenes to navigate to) in the question. – James_D Apr 21 '22 at 15:49
  • @James_D Ok. So if i understood you correctly, when using fx:include, it makes a duplictate instance, and goes back to the default fxml file? I will post more of the code. – Elias Knudsen Apr 21 '22 at 15:53
  • 1
    `` will load the included FXML file and include the root node defined by it in the including FXML. (And, of course, loading an FXML file will create instances of all the classes according to any [class instance elements](https://openjfx.io/javadoc/17/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#class_instance_elements) in that file.) I don't really know what you mean by "goes back to the default fxml file". What is a "default fxml file"? – James_D Apr 21 '22 at 15:57
  • 1
    Also, perhaps it is better not to replace the entire scene, and just replace the main content you are displaying. – James_D Apr 21 '22 at 15:59
  • If you want buttons with a selected state, maybe you should use ToggleButtons and [style them](https://stackoverflow.com/questions/15819242/how-to-make-a-button-appear-to-have-been-clicked-or-selected-javafx2). Place them in a ToggleGroup. You might want to use [RadioButtons styled like toggle buttons](https://stackoverflow.com/questions/46835087/prevent-a-toggle-group-from-not-having-a-toggle-selected-java-fx/46840121#46840121) to get the behaviour you are after. – jewelsea Apr 21 '22 at 22:12

0 Answers0