Home > other >  JavaFX resets my colors when adding buttons
JavaFX resets my colors when adding buttons

Time:10-12

I am using JavaFx to create a front-end to the cli application called spicetify. I am not using an fxml file for the layout instead I am using different classes for layout purposes.

One such class is the Sidebar class. In it I define how the sidebar should look and then create an object of it on the window/page that I need. Whenever I add buttons to the sidebar The colors of the window where the Sidebar object is created go blank/white.

I am unable to find anything by googling and hope that the information provided is enough.

Screenshot of the window without buttons

Screenshot of the window with buttons

Project Structure

Sidebar class:

package spicetify;

import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

public class Sidebar {
    BaseWindow baseWindow = new BaseWindow();
    private String[] buttonIconLocation = {
        "file:src/main/resources/spicetify/images/buttons/home.png",
        "file:src/main/resources/spicetify/images/buttons/theme.png",
        "file:src/main/resources/spicetify/images/buttons/extension.png",
        "file:src/main/resources/spicetify/images/buttons/edit.png",
    };
    private ImageView[] buttonIcon = new ImageView[4];
    private VBox sidebar;
    private Button[] buttons = new Button[4];
    
    public Sidebar(int height, int width, String html, Parent root){
        this.sidebar = new VBox();
        this.sidebar.setPrefHeight(height);
        this.sidebar.setPrefWidth(width);
        this.sidebar.setStyle("-fx-background-color: "   html);
        
        for (int i = 0; i < buttonIcon.length; i  ){
            buttonIcon[i] = new ImageView(buttonIconLocation[i]);
            baseWindow.transform(buttonIcon[i], 50, 50, true);
        }        

        for (int i = 0; i < buttons.length; i  ){
            buttons[i] = new Button("", buttonIcon[i]);
            sidebar.getChildren().add(buttons[i]);
            buttons[i].setTranslateX(20);
            buttons[i].setTranslateY(i * 100);
            buttons[i].setStyle("-fx-background-color: "   html);
        }
    }

    public String[] getButtonIconLocation() {
        return buttonIconLocation;
    }

    public void setButtonIconLocation(String[] buttonIconLocation) {
        this.buttonIconLocation = buttonIconLocation;
    }

    public ImageView[] getButtonIcon() {
        return buttonIcon;
    }

    public void setButtonIcon(ImageView[] buttonIcon) {
        this.buttonIcon = buttonIcon;
    }

    public VBox getSidebar() {
        return sidebar;
    }

    public void setSidebar(VBox sidebar) {
        this.sidebar = sidebar;
    }
}

BaseWindow class:

package spicetify;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class BaseWindow{
    private Parent root;
    private Scene scene;
    private Stage stage;
    private String title;
    private int width;
    private int height;
    private int minWidth;
    private int minHeight;
    private String htmlColor;

    public int getMinWidth() {
        return minWidth;
    }

    public void setMinWidth(int minWidth) {
        this.minWidth = minWidth;
    }

    public int getMinHeight() {
        return minHeight;
    }

    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    public String getHtmlColor() {
        return htmlColor;
    }

    public void setHtmlColor(String htmlColor) {
        this.htmlColor = htmlColor;
    }

    public Parent getRoot() {
        return root;
    }

    public void setRoot(Parent root) {
        this.root = root;
    }

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void transform(ImageView view, int width, int height, boolean preserveRatio){
        view.setFitWidth(width);
        view.setFitHeight(height);
        view.setPreserveRatio(preserveRatio);
    }

    public void start(Stage stage){
        stage.setScene(scene);
        stage.setTitle(title);
        stage.show();
    }
}


HomeWindow class:

package spicetify;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class HomeWindow extends BaseWindow {
    private ImageView logoView;
    private ImageView preview;
    private Parent root;
    private Scene scene;
    BaseWindow baseWindow = new BaseWindow();
    Sidebar sidebar = new Sidebar(baseWindow.getHeight(), 100, "#282828", root);
    
    public HomeWindow(int width, int height, String html){
        this.logoView = new ImageView(new Image("file:src/main/resources/spicetify/images/essentials/logo.png"));
        this.preview = new ImageView(new Image("file:src/main/resources/spicetify/images/essentials/Preview.png"));
        this.root = new BorderPane();
        baseWindow.setWidth(width);
        baseWindow.setMinWidth(width);
        baseWindow.setHeight(height);
        baseWindow.setMinHeight(height);
        baseWindow.setHtmlColor(html);
    }

    public void start(Stage stage){
        ((BorderPane) root).setLeft(sidebar.getSidebar());
        baseWindow.transform(logoView, 600, 700, true);
        ((BorderPane) root).setCenter(logoView);
        baseWindow.setStage(stage);
        baseWindow.setTitle("Spicetify");
        baseWindow.setRoot(root);
        baseWindow.setScene(new Scene(baseWindow.getRoot(), baseWindow.getWidth(), baseWindow.getHeight(), Color.web(baseWindow.getHtmlColor())));
        baseWindow.getStage().setScene(baseWindow.getScene());
        baseWindow.getStage().setMinWidth(baseWindow.getMinWidth());
        baseWindow.getStage().setMinHeight(baseWindow.getMinHeight());
        baseWindow.getStage().setTitle(baseWindow.getTitle());
        baseWindow.getStage().show();
    }
}

CodePudding user response:

Default Modena CSS has a gray background in panes.

When controls are loaded CSS will be applied to the entire scene, as controls use CSS.

Without any controls, CSS (for performance in straight rendering of graphics primitives) will only be applied to the scene if you specifically apply it.

For more information, and steps you can take to remove the default color from pane backgrounds, see the related question:

  • Related