Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:15:03+00:00 2026-06-03T16:15:03+00:00

I would like to create a custom button, that has two states pressed or

  • 0

I would like to create a custom button, that has two states pressed or not, like a toggle button. I have got two images to do this (pressed and not pressed), so how can i create the button and display it with my images ? The button must take the size of the image.
I am not using FXML.
Thank you for helping.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:15:05+00:00Added an answer on June 3, 2026 at 4:15 pm

    There are a few different ways to accomplish this, I’ll outline my favourites.

    This question asks about toggle behaviour so the answer focuses on that. If you don’t need the toggle behaviour, then just use a standard Button, which can be styled the same as the ToggleButton referenced in this answer.

    See also:

    • Styling a JavaFX 2 button using FXML only – How to add an image to a button?

    Use a ToggleButton and apply a custom style to it. I suggest this because your required control is "like a toggle button" but just looks different from the default toggle button styling.

    My preferred method is to define a graphic for the button in css:

    .toggle-button {
      -fx-graphic: url('http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Pizza-icon.png');
    }
    
    .toggle-button:selected {
      -fx-graphic: url('http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Piece-of-cake-icon.png');
    }
    

    OR use the attached css to define a background image.

    // file imagetogglebutton.css deployed in the same package as ToggleButtonImage.class
    .toggle-button {
      -fx-background-image: url('http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Pizza-icon.png');
      -fx-background-repeat: no-repeat;
      -fx-background-position: center;
    }
    
    .toggle-button:selected {
      -fx-background-image: url('http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Piece-of-cake-icon.png');
    }
    

    I prefer the -fx-graphic specification over the -fx-background-* specifications as the rules for styling background images are tricky and setting the background does not automatically size the button to the image, whereas setting the graphic does.

    And some sample code:

    NOTE: the StackPaneBuilder used in this example has been deprecated and removed from JavaFX, replace that code with new StackPane() and call methods on the resultant stack pane to set stack pane properties rather than using the builder.

    import javafx.application.Application;
    import javafx.scene.*;
    import javafx.scene.control.ToggleButton;
    import javafx.scene.layout.StackPaneBuilder;
    import javafx.stage.Stage;
    
    public class ToggleButtonImage extends Application {
      public static void main(String[] args) throws Exception { launch(args); }
      @Override public void start(final Stage stage) throws Exception {
        final ToggleButton toggle = new ToggleButton();
        toggle.getStylesheets().add(this.getClass().getResource(
          "imagetogglebutton.css"
        ).toExternalForm());
        toggle.setMinSize(148, 148); toggle.setMaxSize(148, 148);
        stage.setScene(new Scene(
          StackPaneBuilder.create()
            .children(toggle)
            .style("-fx-padding:10; -fx-background-color: cornsilk;")
            .build()
        ));
        stage.show();
      }
    }
    

    Some advantages of doing this are:

    1. You get the default toggle button behavior and don’t have to re-implement it yourself by adding your own focus styling, mouse and key handlers etc.
    2. If your app gets ported to different platform such as a mobile device, it will work out of the box responding to touch events rather than mouse events, etc.
    3. Your styling is separated from your application logic so it is easier to restyle your application.

    An alternate is to not use css and still use a ToggleButton, but set the image graphic in code:

    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.scene.*;
    import javafx.scene.control.ToggleButton;
    import javafx.scene.image.*;
    import javafx.scene.layout.StackPaneBuilder;
    import javafx.stage.Stage;
    
    public class ToggleButtonImageViaGraphic extends Application {
      public static void main(String[] args) throws Exception { launch(args); }
      @Override public void start(final Stage stage) throws Exception {
        final ToggleButton toggle      = new ToggleButton();
        final Image        unselected  = new Image(
          "http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Pizza-icon.png"
        );
        final Image        selected    = new Image(
          "http://icons.iconarchive.com/icons/aha-soft/desktop-buffet/128/Piece-of-cake-icon.png"
        );
        final ImageView    toggleImage = new ImageView();
        toggle.setGraphic(toggleImage);
        toggleImage.imageProperty().bind(Bindings
          .when(toggle.selectedProperty())
            .then(selected)
            .otherwise(unselected)
        );
        
        stage.setScene(new Scene(
          StackPaneBuilder.create()
            .children(toggle)
            .style("-fx-padding:10; -fx-background-color: cornsilk;")
            .build()
        ));
        stage.show();
      }
    }
    

    The code based approach has the advantage that you don’t have to use css if you are unfamilar with it.

    For best performance and ease of porting to unsigned applet and webstart sandboxes, bundle the images with your app and reference them by relative path urls rather than downloading them off the net.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have authored some custom classes that I would like to create using XAML:
I would like to create a UIBarButtonItem on my iPhone application which has two
I would like to create a custom Android button from 9patches, but without using
I would like to create a set of custom controls that are basically image
I would like to re-create the animation of the delete confirmation button that Apple
I would like to make a custom button, that will display 3 data bound
On Android, I would like to create a button that contains some other Views.
I am trying to create a button that has a custom shape (hexagon), but
My company has developed two pages that we would like to publish on a
I would like to create a custom button with framed caption in right top

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.