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

Establishing a connection to the existing Selenium browser window using the session ID

I'm exploring ways to connect to an already existing browser window using Selenium RC. I came across a post on Exposing the selenium browser session id, which suggests that by obtaining the current session in Selenium RC, this can be achieved. Has an ...

Adding dashes as padding in HTML/CSS

I am currently working on updating the user management block on my website and I want to showcase it with some visual examples. I believe that using images is the most effective way to demonstrate changes. This is the current appearance of the block: ...

Enhancing a Linked List with New Elements

Trying to grasp the concept of how elements from a String[] contentsOfBag are added into a linkedbag called csc220bag for this programming scenario. First question: Is my understanding correct that each element of String[] contentsOfBag is added in such a ...

Using Java to patiently anticipate the completion of a file download based on a partial filename

Currently, I am using fluent wait to monitor the download of a file. The issue arises because the file is downloaded with varying date formats. To address this, I need to validate the "Partial filename" of the file when using fluentWait. For example: cance ...

What are the buttons featured in the header of this mobile-friendly bootstrap panel

Can anyone offer advice on how to make a bootstrap panel header responsive? The panel has a title with several buttons on the right side. While it looks good on a large screen, it becomes messy on smaller browser sizes. You can find my complete code here ...

Break the page after generating specific charts with JQuery

I have a task to create multiple charts, where the first page needs to include a header and the first 8 charts. How can I insert a page break after the 8th chart and move on to a second page without a header? Each student has different subjects and score ...

Having trouble interpreting JSON attribute "null"

I encountered a problem while attempting to parse a JSON "null" property. Can someone help me understand what the issue might be? The JSON I used was as follows: { "properties" : { "null" : { "value" : false } } } I validated th ...

Getting mixed up with utilizing swt canvas.redraw

As a fresh face in SWT, I have encountered a puzzling behavior in the following code snippet. When I click the test button, the console outputs "start", "end", and "in canvas". Why does it not print "start", "in canvas", "end"? If this is not a problem, is ...

Tips on creating a horizontal scrolling effect using the mouse

Is there a way to enable horizontal scrolling by holding down the mouse click, instead of relying on the horizontal scroll bar? And if possible, can the scroll bar be hidden? In essence, I am looking to replicate the functionality of the horizontal scroll ...

What is the best way to center a div vertically within a bootstrap 5 carousel across all screen sizes?

Looking to center the text div within a Bootstrap 5 carousel both vertically on large screens and small (mobile) screens. To view the site, visit... Below is the HTML for the div. The targeted div has the CSS class #hero .carousel-container { display ...

Merge adjacent CSS blocks seamlessly without the need for JavaScript

I am facing an issue with styling div elements that have editable content through a WYSIWYG inline editor on my website. Users often do not understand the importance of enclosing similar blocks of code in containing DIVs for proper formatting, resulting in ...

Challenge encountered in handling AJAX response data for presentation

Currently, I am immersed in a project and decided to implement AJAX for smoother form submissions. My initial attempt was trying to display text from a .txt file below the "submit" button, but unfortunately, that approach didn't yield the desired resu ...

Switching the background image when hovering over a list element

Looking at the screenshot, it's clear that there is an unordered list with a background image set on the div. What I am trying to achieve is to have the background image change whenever I hover over a list item. Each list item should trigger a differe ...

Expand the range of input movement using a unique and oversized custom thumb graphic

In my project, I am creating a personalized input range feature by utilizing a 40px x 80px image as the background for the thumb. Typically, the thumb is limited to moving within the length of the track from edge to edge. However, I am aiming for the thu ...

Using the <blink> tag on Internet Explorer

Unfortunately, Internet Explorer does not support the <blink> tag or the text-decoration:blink; style in CSS. Are there any alternative methods for creating blinking text in IE? ...

Implement a fadeout effect by using a timeout function that adds a class to the div element in

I implemented a loader on my webpage that animates for 3 seconds before a function kicks in to modify the styles and make it invisible. I am looking to add a fadeout effect when the 'waa' style is applied to the 'load' div. setTimeou ...

Using $window.print() in angularjs results in the print preview displaying an empty page

Encountering a strange issue where using $window.print in angularjs results in only the date, page name, page number, and url appearing on the printed page. The rest of the content is missing even though the original page has plenty of it. I suspect the p ...

The CSS and JS codes are not successfully integrating into the webpage

I am encountering an issue with loading CSS and JS files onto my page. My project involves PHP and Xampp. The file structure is as follows: My Site - CSS - index.css - JS - index.js - Index.php (Apologies for the lack of a folder tre ...

If a user types in Korean characters in the input field and then clicks away to any other part of the screen, the input field will automatically regain focus

jsfiddle instructions for testing: type Korean characters in the input field scroll down until the input field is no longer visible Click anywhere on the screen Expected result: the input field will come into focus with (Korean text) If the user enters K ...

Ways to overlook concealed elements within a Bootstrap Button Group

Within my button group, there are 10 buttons. On certain screen widths, using media queries for responsiveness, I hide some buttons. The issue arises when the last button is hidden - the rounded edges of the succeeding visible button are not maintained. ...