Change the JavaFX ToggleButton's color and revert back to its original color

Can you tell me what the default background and border color is for a toggle button? Here’s an example of code that changes the background color of a toggle button:

i.toggle11.setOnAction(e->{
        if(i.toggle11.isSelected()){
            i.toggle11.setStyle("-fx-background-color:red");
             i.toggle12.setStyle("-fx-background-color:white");
             i.toggle13.setStyle("-fx-background-color:white");
        }
        else {
             i.toggle11.setStyle("-fx-background-color:white");
        }
    });

After this action, I would like to reset the other toggles to their original colors (the same as when they were first created).

Answer №1

Discovering how to modify styles

If you're looking to change the appearance of your ToggleButtons using CSS style classes, a simple method is to add the desired styles and then remove them when they are no longer needed. This way, the button will retain its original formatting.

For Instance

Code for adding styles

i.toggle11.setOnAction(e->{
        if(i.toggle11.isSelected()){
            i.toggle11.getStyleClass().add("red-button");
            i.toggle12.getStyleClass().add("white-button");
            i.toggle13.getStyleClass().add("white-button");
        }
        else{
             i.toggle13.getStyleClass().add("white-button");
        }
    });

Subsequently, code for removing styles

i.toggle11.getStyleClass().removeAll("red-button", "white-button");

The CSS Styles

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

.white-button {
    -fx-background-color: white;
}

Be sure to include the stylesheet with your desired CSS styles like:

scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

Alternatively,

If your goal is simply to change the color of selected ToggleButtons, you can override .toggle-button:selected in your stylesheet without adding extra styling:

.toggle-button:selected {
    -fx-background-color: red;
}

Overwriting existing CSS classes and pseudoclasses is usually the most straightforward approach. For more guidance on styling and customizing JavaFX components like ToggleButtons, refer to resources such as Using JavaFX UI Controls.

To explore available style classes for .toggle-button, check out this resource. Additionally, refer to the CSS reference guide for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adapting padding based on the height of the browser to ensure optimal layout

I'm interested in adjusting the padding value of a button element based on the height of the browser window, not its width. I want to make sure that the padding adjusts proportionally to the browser window's height. Is this achievable using CSS? ...

Customizing the color of a select dropdown in Bootstrap using CSS styling

I am having difficulty changing the default blue color for Bootstrap select dropdowns. Despite trying :hover and :active selectors on both option and select, I have not been successful in making the desired changes. Could anyone provide insight regarding ...

Customize Individual Rows in a React DataGrid

I am exploring the world of Material UI React data grid for the first time, and I am looking to add a unique background color to only the initial three rows. Each row should have a different color scheme that remains static, without any interactions trigge ...

Automatically change div background image based on window resizing

.step-1-4 { background:url('../images/gadget-4-sprite.png') no-repeat; width:950px; height:70px; margin-left: 15px; } The code above sets the CSS for a div containing a background image. The dimensions of the div match that of the imag ...

Is there a way to insert a record upon the user clicking on the Add Record button?

// Here is my Component code // I want to figure out how to add a new row to the table with fresh values. import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-uom', templateUrl: './uom.component.html ...

Issue arises when attempting to compile .less file with variables sourced from a separate file

I encountered an issue with my .less file where it contains definitions that rely on variables from another file. For example: body { font-family: @baseFontFamily; font-size: @baseFontSize; color: @textColor; } At first, IntelliJ displayed t ...

Enhance the spacing between the UL-LI navigation menu and the currently selected item

I'm struggling with adjusting the spacing between items in my navigation menu, specifically the li elements that are currently too close together. I've attempted to increase the margin-left within the .main_menu li CSS rule, but it doesn't s ...

Is there a way to spin these hexagons around?

Hey there, I need some assistance with a problem. I have to rotate these three hexagons slightly, about 15 degrees or so. The catch is, it needs to work in Internet Explorer only. I've been struggling with this all day and it's getting ...

Having difficulty resolving issues with the chat box (div) scroll bar staying fixed at the bottom

I am currently working on a chat application and I am facing an issue with fixing the scroll bar at the bottom of a div when loading my PHP file. I have already written a script to achieve this, but despite accessing it using CSS Id, I am having trouble ge ...

unwelcome tab spacing in CSS

I am facing an issue with unwanted spacing in my lists. I have a code that generates three-column lists, each containing about eight rows. However, the first list item of the last row is causing an unwanted space. It seems to shift entirely to the next col ...

Achieve full height in Grid component of material UI by eliminating margins

In this codesandbox example, I have successfully set the grid container height to full using 100vh. However, there is an issue with a margin of 8px being added by the user agent to the body element, and I'm struggling to find a solution to remove it. ...

Aligning a rotated paragraph in the middle of its parent container

Here is my current progress: http://jsfiddle.net/2Zrx7/2/ .events{ height:100px; position: relative; } .tt_username{ position: absolute; top:0px; height: 100%; width: 30px; background: #ccc; text-align: center; } .tt_usern ...

Tips for making a hide and reveal FAQ section

Currently working on creating a FAQ page for my website, I ran into a challenge. My goal is to have a setup where clicking on a question will reveal the answer while hiding any previously open answers. So far, I have managed to achieve this functionality p ...

The compilation of SCSS CSS is encountering an error with the provided CSS code

Upon compiling SCSS, I encountered an error with the following CSS: span.icon-hcp { filter: progid: DXImageTransform.Microsoft.AlphaImageLoader(src='../images/icon-hc.jpg', sizingMethod='scale') !important; -ms-filter: ...

Running Selenium tests through Jenkins on Ubuntu server 16.04 in headless mode, completely bypassing the need for a graphical user interface

I am currently engaged in Selenium Automation Testing with Jenkins. After successfully setting up Jenkins on my computer, I have been able to run tests as a Maven project without any issues. The Jenkins setup is on a Ubuntu server that operates without a ...

Configuration options for content division in a table cell

Please see the following fiddle which I have attempted: http://jsfiddle.net/9LdEc/ code: html: <div id="CorpDealerSearch"> <div class="row"> <div class="left"> Quarter To Date </div&g ...

Creating responsive divs on my webpage

Although I have browsed through various posts with similar questions, none of the solutions seem to work for me. I will make sure to include all relevant information related to my issue and explain why I am unable to resolve it on my own. Currently, I am ...

Unable to create Extent Reports while executing classes simultaneously using TestNG

While running classes in parallel with TestNG, the Extent Report is not generated, but the TestNG report is being updated. Below is a sample code and the versions I am currently using: If we run only one class (TestClass1.java), then the Extent Report will ...

Submit image and transfer it to server using AngularJS and Jersey

I need help with my form that contains the following fields: <div class="form-group"> <label>Username</label> <input type="text" ng-model="username" class="form-control"> </div> <div class="from-group"> < ...

Struggling with eclipse, cucumber, and selenium throwing a java.lang.ClassNotFoundException for gherkin.lexer.Encoding?

I'm new to using cucumber and I came across an example that seems to be working fine for the most part, but I keep encountering the following exception: java.lang.NoClassDefFoundError: gherkin/lexer/Encoding at cucumber.runtime.FeatureBuilder.read(Fe ...