当前位置: 动力学知识库 > 问答 > 编程问答 >

button - Styleclass not being applied when the previous style class is added in FXML

问题描述:

I assign styleclass of ToggleButton in FXML file as follows:

<ToggleButton fx:id="Button" styleClass="defaultStyle">

Later, in my code I change the style classes as follows:

@FXML private ToggleButton Button;

Button.getStyleClass().remove("defaultStyle");

Button.getStyleClass().add("newStyle");

The CSS file is defined as:

 .defaultStyle { -fx-background-color: black;}

.newStyle { -fx-background-color: red;}

EDITED:

The new style is applied when done in the Controller, but the new style is not being applied when done somewhere else. When I debug, I see the correct style-class being added & removed to the button.

Anyone got a workaround for this problem? I appreciate your help in advance.

网友答案:

Style class removing and adding are working as expected. I guess your problem is the ToggleButton has not been injected correctly, it should be:

@FXML private ToggleButton Button;
...
Button.getStyleClass().remove("defaultStyle");
Button.getStyleClass().add("newStyle");

in the controller class. Note the capital b of "Button" since you have defined fx:id="Button" in FXML file. Also note that you don't need to instantiate the ToggleButton Button (like new ToggleButton()) yourself.

EDIT:
Here is code example for changing the styleclass. As I said it is working as expected. Compare it with yours.

Sample.fxml:

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="somepackage.SampleController">
    <stylesheets>
        <String fx:value="somepackage/style.css" />
    </stylesheets>
    <children>
        <ToggleButton layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="mybutton" styleClass="defaultStyle" />
    </children>
</AnchorPane>

SampleController.java:

package somepackage;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ToggleButton;

public class SampleController implements Initializable {

    @FXML
    private ToggleButton mybutton;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("before :" + mybutton.getStyleClass());
        mybutton.getStyleClass().remove("defaultStyle");
        mybutton.getStyleClass().add("newStyle");
        System.out.println("after  :" + mybutton.getStyleClass());
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
}

MainDemo.java:

package somepackage;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainDemo extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        System.out.println("version: " + com.sun.javafx.runtime.VersionInfo.getRuntimeVersion());
        Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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

The css file includes selectors of yours.

网友答案:

Since you said it is only done when the controller does it.

Use the FXMLLoader to load your controller. then you change the StyleClass to the newStyle.

分享给朋友:
您可能感兴趣的文章:
随机阅读: