I've already asked similar question here. The answer was correct, but still it seems like it's not applicable to my case. Have a look at my code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane();
root.setPadding(new Insets(40, 40, 40, 40));
Button btn = new Button("NEW BUTTON");
btn.setId("button");
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
btn.arm();
}
});
root.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
btn.disarm();
}
});
root.getChildren().addAll(btn);
primaryStage.setScene(new Scene(root, 310, 200));
primaryStage.getScene().getStylesheets().add("style.css");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Then I specified stylesheet:
#button {
-fx-background-color: #403e40;
-fx-border-width: 0;
-fx-font-size: 30px;
-fx-text-fill: #b3b1b3;
-fx-background-radius: 0;
}
#button:hover {
-fx-background-color: #595759;
}
#button:pressed {
-fx-background-color: #ffffff;
-fx-text-fill: #403e40;
}
And in the end, it doesn't work. I'm absolutely frustrated. It works just fine if .css file isn't linked and doesn't work otherwise. So, is it possible at all to change the appearance of the button if a key pressed with customized view?