Is there a way to change the images displayed in a JavaFXML Button?

I'm attempting to create a button that toggles between displaying an image of a red circle and an image of a blue circle when clicked. In my CSS, I've set up styles for the different states of the button:

#button-debit {
    -fx-background-image: url("images/redButton.jpg");     
}

#button-credit {
    -fx-background-image: url("images/blueButton.png");
}

In order to test this functionality, I've tried simply transforming the button from red to blue like this:

@FXML
private void handledborcrBtn() {
    dborcrBtn.setId("button-credit");
}

Unfortunately, the result is not what I expected. The red button image remains in place with the blue one underneath it, but the blue image appears split into several parts.

I vaguely recall a 'repaint' method from javax Swing, however, JavaFXML seems more complex and I'm struggling to get it working correctly.

Answer №1

Experiment using the ToggleButton element

.toggle-button {
  -fx-graphic: url('icons.jpg');
}

.toggle-button:selected {
  -fx-graphic: url('othericon.png');
}

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

Exploring the application of the PUT method specific to a card ID in vue.js

A dashboard on my interface showcases various cards containing data retrieved from the backend API and stored in an array called notes[]. When I click on a specific card, a pop-up named updatecard should appear based on its id. However, I am facing issues ...

Animating ng-switch transitions within a div element

Utilizing bootstrap 3 panel along with ng-switch for a sliding animation: Check out the Plunker here I am facing an issue where the animation extends past the borders of the panel instead of staying within it. How can I fix this in my Plunker? The desire ...

Maintain the aspect ratio of an image when placing it in a square container

Looking for a way to create a CSS grid with non-square random sized images? I need help filling all the space inside a fixed size div. Any suggestions? Check out my example: (blue represents the fixed size div and red shows the image inside it) ...

Inject environment variable into SCSS file while using webpack

I'm new to webpack and I need help with reading a specific value, which is the env variable from the webpack.config.js file, in a sass file. This will allow me to have different CSS styles based on the environment. For example: If the env is set to ...

How about showcasing bigger images in the main body of the page?

When it comes to using CSS sprites for images, the typical advice is to reserve it for smaller images like icons. Why should larger content images only be represented through <img> elements? Are there not benefits to utilizing spriting for all types ...

Enhancing your grasp on CSS grid columns - mastering columns through float usage

Struggling to grasp the concepts of using http://semantic.gs alongside LESS.js. The documentation provided isn't quite clear, and I haven't delved deep into understanding LESS.js. However, I have gone through the Semantic Grid System website&apos ...

Tips for displaying sorting order when the column width is narrower than the caption in free jqgrid

Sorting columns in jqgrid can be done by clicking on the column header, with the ability to set a default sort order upon loading. Icons for sorting are specified using: $grid.jqGrid({ viewsortcols: [false,'vertical',true], The sort icon an ...

Confirm that the CSS syntax is valid

Is there a way to validate CSS syntax by checking for specific characters like "{", "}", ";", and ":"? I have come up with four checks to ensure the code is valid, such as verifying that the number of opening and closing curly braces match, as well as the ...

Tips for properly positioning the ko validation message in case it is lengthy

I have implemented ko validation messages in order to validate input fields. Can anyone provide guidance on how to align the validation message correctly within the specified location? <input id="personName" class="form-control placeholder has-error" t ...

The performance of ASP.net text-boxes is inconsistent

After days of searching for a solution, I am still unable to resolve my issue. My website features a simple web-form with text-boxes that are supposed to appear when the user selects the "custom search" option. While testing locally, everything works fine. ...

The basic CSS container fails to properly align its elements as intended

Recently, I created a CSS div using Bootstrap and added some custom styling. You can check out the fiddle for a live demo of the code: https://jsfiddle.net/w9gyc0t5/. Here's the actual code: <!-- Bootstrap docs: https://getbootstrap.com/docs -- ...

Ways to emphasize the currently active tabs on a navigation bar

How can I highlight the active tabs and shift it when clicking on another link? $(function() { $('.nav ul li').on('click', function() { $(this).parent().find('li.active').removeClass('active'); $(this).a ...

Uncovering the source of glitchy Angular Animations - could it be caused by CSS, code, or ng-directives? Plus, a bonus XKCD comic for some light-hearted humor

Just finished creating an XKCD app for a MEAN stack class I'm enrolled in, and I'm almost done with it. However, there's this annoying visual bug that's bothering me, especially with the angular animations. Here is the link to my deploy ...

Reposition buttons using JavaScript

I attempted to modify the button that appears at the top of an HTML page. <div class='play'> <input id="play" type="button" value="Play" onclick="mode('play');startSlideshow();" /> </div> <div class='pause ...

how to style a page with a CSS arrow shape

Can someone help me figure out how to create an arrow similar to the one in this button using CSS? I've been able to create triangle-like arrows like the one below #triangle_arrow { top: 3pt; content: ""; display: inline-block; wid ...

Is it feasible to generate a fixed lighting effect overlay with HTML and CSS?

Is it possible to incorporate a static lighting effect overlay using HTML/CSS? My project consists of an HTML5/JS app with a top navigation bar and a series of cards that are transitioned through using swipe gestures. These cards are displayed in gray ove ...

Conceal a div element after redirecting to a different HTML page

I have a dilemma with my two HTML pages - index.html and register.html. I am trying to navigate from index.html to register.html, but I want to skip the select region step and go straight to the login page. Here's the code snippet I've been attem ...

I encountered a SyntaxError indicating a missing ")" right before utilizing jQuery in my code

When trying to run the code below, I encountered an error in the console stating SyntaxError: missing ) after argument list. $(document).ready(function() { $(".blog-sidebar-form form").before("<p class="blog-sidebar-inform>Dummy Text</ ...

Adjust the appearance of an element in a different parent when hovering over a specific element with a matching index

I have implemented a menu on my site in two different ways - one using text and the other using pictures. Users can click on both types of menus. Now, I am looking to create an interactive feature where hovering over a specific item in the text menu (such ...

The method window.getComputedStyle retrieves the CSS max-height property containing an unresolved calc() function

Has anyone encountered a challenge with a function related to Angular in their project? Here is a snippet of the code causing issues: console.log(window.getComputedStyle(this.frontLayer.nativeElement).maxHeight); And within the component template: <di ...