Java FX button highlight












0















enter image description here



What I want to do is, Please see attached screen shot. Once I click on between buttons 1-4 it should be highlighted with red and
stay as highlighted until I select any other button between button 1 and button 4 and then highlight the selected button should be
highlighted. I can do this with focused property. But I have other buttons on my scene such that button 5,6 and 7. Once I click on any other button or click on another control
focus and red color goes away. But I want the clicked button stay as highlighted, or a sign that will show which button(between button 1 and button 4) is selected.










share|improve this question




















  • 3





    Did you look at ToggleButton?

    – user1803551
    Nov 25 '18 at 4:18











  • Yes, I tried togglebutton. But it did not work out. Once I click on any other buttons(button5,6,7) which I do not want to highlight it loses the focused property and not being highlighted anymore.

    – Akansha
    Nov 25 '18 at 4:54











  • If by "highlight" you mean the focus indicator then you shouldn't set that by yourself because it confuses the user. The toggle button stays in its pressed state until another of its group is pressed. You will have to edit your question to provide context of what you're trying to do and what highlight you want.

    – user1803551
    Nov 25 '18 at 5:36






  • 1





    Usually RadioButtons (and a ToggleGroup) are used for this kind of behaviour.

    – fabian
    Nov 25 '18 at 11:15











  • Sorry for the confusion. Basically, What I wan to do is, that I want to show which menu(button) is selected. I am thinking putting label next to the each button and when the button is clicked painting that particular button.

    – Akansha
    Nov 25 '18 at 12:58
















0















enter image description here



What I want to do is, Please see attached screen shot. Once I click on between buttons 1-4 it should be highlighted with red and
stay as highlighted until I select any other button between button 1 and button 4 and then highlight the selected button should be
highlighted. I can do this with focused property. But I have other buttons on my scene such that button 5,6 and 7. Once I click on any other button or click on another control
focus and red color goes away. But I want the clicked button stay as highlighted, or a sign that will show which button(between button 1 and button 4) is selected.










share|improve this question




















  • 3





    Did you look at ToggleButton?

    – user1803551
    Nov 25 '18 at 4:18











  • Yes, I tried togglebutton. But it did not work out. Once I click on any other buttons(button5,6,7) which I do not want to highlight it loses the focused property and not being highlighted anymore.

    – Akansha
    Nov 25 '18 at 4:54











  • If by "highlight" you mean the focus indicator then you shouldn't set that by yourself because it confuses the user. The toggle button stays in its pressed state until another of its group is pressed. You will have to edit your question to provide context of what you're trying to do and what highlight you want.

    – user1803551
    Nov 25 '18 at 5:36






  • 1





    Usually RadioButtons (and a ToggleGroup) are used for this kind of behaviour.

    – fabian
    Nov 25 '18 at 11:15











  • Sorry for the confusion. Basically, What I wan to do is, that I want to show which menu(button) is selected. I am thinking putting label next to the each button and when the button is clicked painting that particular button.

    – Akansha
    Nov 25 '18 at 12:58














0












0








0








enter image description here



What I want to do is, Please see attached screen shot. Once I click on between buttons 1-4 it should be highlighted with red and
stay as highlighted until I select any other button between button 1 and button 4 and then highlight the selected button should be
highlighted. I can do this with focused property. But I have other buttons on my scene such that button 5,6 and 7. Once I click on any other button or click on another control
focus and red color goes away. But I want the clicked button stay as highlighted, or a sign that will show which button(between button 1 and button 4) is selected.










share|improve this question
















enter image description here



What I want to do is, Please see attached screen shot. Once I click on between buttons 1-4 it should be highlighted with red and
stay as highlighted until I select any other button between button 1 and button 4 and then highlight the selected button should be
highlighted. I can do this with focused property. But I have other buttons on my scene such that button 5,6 and 7. Once I click on any other button or click on another control
focus and red color goes away. But I want the clicked button stay as highlighted, or a sign that will show which button(between button 1 and button 4) is selected.







java javafx javafx-8 javafx-2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 4:16









user1803551

9,78252951




9,78252951










asked Nov 25 '18 at 4:07









AkanshaAkansha

104




104








  • 3





    Did you look at ToggleButton?

    – user1803551
    Nov 25 '18 at 4:18











  • Yes, I tried togglebutton. But it did not work out. Once I click on any other buttons(button5,6,7) which I do not want to highlight it loses the focused property and not being highlighted anymore.

    – Akansha
    Nov 25 '18 at 4:54











  • If by "highlight" you mean the focus indicator then you shouldn't set that by yourself because it confuses the user. The toggle button stays in its pressed state until another of its group is pressed. You will have to edit your question to provide context of what you're trying to do and what highlight you want.

    – user1803551
    Nov 25 '18 at 5:36






  • 1





    Usually RadioButtons (and a ToggleGroup) are used for this kind of behaviour.

    – fabian
    Nov 25 '18 at 11:15











  • Sorry for the confusion. Basically, What I wan to do is, that I want to show which menu(button) is selected. I am thinking putting label next to the each button and when the button is clicked painting that particular button.

    – Akansha
    Nov 25 '18 at 12:58














  • 3





    Did you look at ToggleButton?

    – user1803551
    Nov 25 '18 at 4:18











  • Yes, I tried togglebutton. But it did not work out. Once I click on any other buttons(button5,6,7) which I do not want to highlight it loses the focused property and not being highlighted anymore.

    – Akansha
    Nov 25 '18 at 4:54











  • If by "highlight" you mean the focus indicator then you shouldn't set that by yourself because it confuses the user. The toggle button stays in its pressed state until another of its group is pressed. You will have to edit your question to provide context of what you're trying to do and what highlight you want.

    – user1803551
    Nov 25 '18 at 5:36






  • 1





    Usually RadioButtons (and a ToggleGroup) are used for this kind of behaviour.

    – fabian
    Nov 25 '18 at 11:15











  • Sorry for the confusion. Basically, What I wan to do is, that I want to show which menu(button) is selected. I am thinking putting label next to the each button and when the button is clicked painting that particular button.

    – Akansha
    Nov 25 '18 at 12:58








3




3





Did you look at ToggleButton?

– user1803551
Nov 25 '18 at 4:18





Did you look at ToggleButton?

– user1803551
Nov 25 '18 at 4:18













Yes, I tried togglebutton. But it did not work out. Once I click on any other buttons(button5,6,7) which I do not want to highlight it loses the focused property and not being highlighted anymore.

– Akansha
Nov 25 '18 at 4:54





Yes, I tried togglebutton. But it did not work out. Once I click on any other buttons(button5,6,7) which I do not want to highlight it loses the focused property and not being highlighted anymore.

– Akansha
Nov 25 '18 at 4:54













If by "highlight" you mean the focus indicator then you shouldn't set that by yourself because it confuses the user. The toggle button stays in its pressed state until another of its group is pressed. You will have to edit your question to provide context of what you're trying to do and what highlight you want.

– user1803551
Nov 25 '18 at 5:36





If by "highlight" you mean the focus indicator then you shouldn't set that by yourself because it confuses the user. The toggle button stays in its pressed state until another of its group is pressed. You will have to edit your question to provide context of what you're trying to do and what highlight you want.

– user1803551
Nov 25 '18 at 5:36




1




1





Usually RadioButtons (and a ToggleGroup) are used for this kind of behaviour.

– fabian
Nov 25 '18 at 11:15





Usually RadioButtons (and a ToggleGroup) are used for this kind of behaviour.

– fabian
Nov 25 '18 at 11:15













Sorry for the confusion. Basically, What I wan to do is, that I want to show which menu(button) is selected. I am thinking putting label next to the each button and when the button is clicked painting that particular button.

– Akansha
Nov 25 '18 at 12:58





Sorry for the confusion. Basically, What I wan to do is, that I want to show which menu(button) is selected. I am thinking putting label next to the each button and when the button is clicked painting that particular button.

– Akansha
Nov 25 '18 at 12:58












1 Answer
1






active

oldest

votes


















0














I recommend using a ToggleGroup and ToggleButton for this. The ToggleGroup allows your user to only select one button at a time. When the button is selected, you can then set the style you want.



In the sample program below, I've got 6 ToggleButtons in the group and only one may be selected at any given time. The selected button will have a red background (highlight). Any buttons you create that do not have this styling will be unaffected.



The code below is commented as well:



import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonHighlights extends Application {

public static void main(String args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// Create a ToggleGroup to hold the list of ToggleButtons. This will allow us to allow the selection of only one
// ToggleButton at a time
ToggleGroup toggleGroup = new ToggleGroup();

// Create our 6 ToggleButtons. For this sample, I will use a for loop to add them to the ToggleGroup. This is
// not necessary for the main functionality to work, but is used here to save time and space
for (int i = 0; i < 6; i++) {
ToggleButton button = new ToggleButton("Button #" + i);

// If you want different styling for the button when it's selected other than the default, you can either
// use an external CSS stylesheet, or apply the style in a listener like this:
button.selectedProperty().addListener((observable, oldValue, newValue) -> {

// If selected, color the background red
if (newValue) {
button.setStyle(
"-fx-background-color: red;" +
"-fx-text-fill: white");
} else {
button.setStyle(null);
}
});

// Add the button to our ToggleGroup
toggleGroup.getToggles().add(button);
}

// Add all our buttons to the scene
for (Toggle button :
toggleGroup.getToggles()) {
root.getChildren().add((ToggleButton) button);
}

// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}



The Result:




screenshot






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464570%2fjava-fx-button-highlight%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I recommend using a ToggleGroup and ToggleButton for this. The ToggleGroup allows your user to only select one button at a time. When the button is selected, you can then set the style you want.



    In the sample program below, I've got 6 ToggleButtons in the group and only one may be selected at any given time. The selected button will have a red background (highlight). Any buttons you create that do not have this styling will be unaffected.



    The code below is commented as well:



    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Toggle;
    import javafx.scene.control.ToggleButton;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class ButtonHighlights extends Application {

    public static void main(String args) {
    launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

    // Simple interface
    VBox root = new VBox(5);
    root.setPadding(new Insets(10));
    root.setAlignment(Pos.CENTER);

    // Create a ToggleGroup to hold the list of ToggleButtons. This will allow us to allow the selection of only one
    // ToggleButton at a time
    ToggleGroup toggleGroup = new ToggleGroup();

    // Create our 6 ToggleButtons. For this sample, I will use a for loop to add them to the ToggleGroup. This is
    // not necessary for the main functionality to work, but is used here to save time and space
    for (int i = 0; i < 6; i++) {
    ToggleButton button = new ToggleButton("Button #" + i);

    // If you want different styling for the button when it's selected other than the default, you can either
    // use an external CSS stylesheet, or apply the style in a listener like this:
    button.selectedProperty().addListener((observable, oldValue, newValue) -> {

    // If selected, color the background red
    if (newValue) {
    button.setStyle(
    "-fx-background-color: red;" +
    "-fx-text-fill: white");
    } else {
    button.setStyle(null);
    }
    });

    // Add the button to our ToggleGroup
    toggleGroup.getToggles().add(button);
    }

    // Add all our buttons to the scene
    for (Toggle button :
    toggleGroup.getToggles()) {
    root.getChildren().add((ToggleButton) button);
    }

    // Show the Stage
    primaryStage.setWidth(300);
    primaryStage.setHeight(300);
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
    }
    }



    The Result:




    screenshot






    share|improve this answer




























      0














      I recommend using a ToggleGroup and ToggleButton for this. The ToggleGroup allows your user to only select one button at a time. When the button is selected, you can then set the style you want.



      In the sample program below, I've got 6 ToggleButtons in the group and only one may be selected at any given time. The selected button will have a red background (highlight). Any buttons you create that do not have this styling will be unaffected.



      The code below is commented as well:



      import javafx.application.Application;
      import javafx.geometry.Insets;
      import javafx.geometry.Pos;
      import javafx.scene.Scene;
      import javafx.scene.control.Toggle;
      import javafx.scene.control.ToggleButton;
      import javafx.scene.control.ToggleGroup;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class ButtonHighlights extends Application {

      public static void main(String args) {
      launch(args);
      }

      @Override
      public void start(Stage primaryStage) {

      // Simple interface
      VBox root = new VBox(5);
      root.setPadding(new Insets(10));
      root.setAlignment(Pos.CENTER);

      // Create a ToggleGroup to hold the list of ToggleButtons. This will allow us to allow the selection of only one
      // ToggleButton at a time
      ToggleGroup toggleGroup = new ToggleGroup();

      // Create our 6 ToggleButtons. For this sample, I will use a for loop to add them to the ToggleGroup. This is
      // not necessary for the main functionality to work, but is used here to save time and space
      for (int i = 0; i < 6; i++) {
      ToggleButton button = new ToggleButton("Button #" + i);

      // If you want different styling for the button when it's selected other than the default, you can either
      // use an external CSS stylesheet, or apply the style in a listener like this:
      button.selectedProperty().addListener((observable, oldValue, newValue) -> {

      // If selected, color the background red
      if (newValue) {
      button.setStyle(
      "-fx-background-color: red;" +
      "-fx-text-fill: white");
      } else {
      button.setStyle(null);
      }
      });

      // Add the button to our ToggleGroup
      toggleGroup.getToggles().add(button);
      }

      // Add all our buttons to the scene
      for (Toggle button :
      toggleGroup.getToggles()) {
      root.getChildren().add((ToggleButton) button);
      }

      // Show the Stage
      primaryStage.setWidth(300);
      primaryStage.setHeight(300);
      primaryStage.setScene(new Scene(root));
      primaryStage.show();
      }
      }



      The Result:




      screenshot






      share|improve this answer


























        0












        0








        0







        I recommend using a ToggleGroup and ToggleButton for this. The ToggleGroup allows your user to only select one button at a time. When the button is selected, you can then set the style you want.



        In the sample program below, I've got 6 ToggleButtons in the group and only one may be selected at any given time. The selected button will have a red background (highlight). Any buttons you create that do not have this styling will be unaffected.



        The code below is commented as well:



        import javafx.application.Application;
        import javafx.geometry.Insets;
        import javafx.geometry.Pos;
        import javafx.scene.Scene;
        import javafx.scene.control.Toggle;
        import javafx.scene.control.ToggleButton;
        import javafx.scene.control.ToggleGroup;
        import javafx.scene.layout.VBox;
        import javafx.stage.Stage;

        public class ButtonHighlights extends Application {

        public static void main(String args) {
        launch(args);
        }

        @Override
        public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Create a ToggleGroup to hold the list of ToggleButtons. This will allow us to allow the selection of only one
        // ToggleButton at a time
        ToggleGroup toggleGroup = new ToggleGroup();

        // Create our 6 ToggleButtons. For this sample, I will use a for loop to add them to the ToggleGroup. This is
        // not necessary for the main functionality to work, but is used here to save time and space
        for (int i = 0; i < 6; i++) {
        ToggleButton button = new ToggleButton("Button #" + i);

        // If you want different styling for the button when it's selected other than the default, you can either
        // use an external CSS stylesheet, or apply the style in a listener like this:
        button.selectedProperty().addListener((observable, oldValue, newValue) -> {

        // If selected, color the background red
        if (newValue) {
        button.setStyle(
        "-fx-background-color: red;" +
        "-fx-text-fill: white");
        } else {
        button.setStyle(null);
        }
        });

        // Add the button to our ToggleGroup
        toggleGroup.getToggles().add(button);
        }

        // Add all our buttons to the scene
        for (Toggle button :
        toggleGroup.getToggles()) {
        root.getChildren().add((ToggleButton) button);
        }

        // Show the Stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
        }
        }



        The Result:




        screenshot






        share|improve this answer













        I recommend using a ToggleGroup and ToggleButton for this. The ToggleGroup allows your user to only select one button at a time. When the button is selected, you can then set the style you want.



        In the sample program below, I've got 6 ToggleButtons in the group and only one may be selected at any given time. The selected button will have a red background (highlight). Any buttons you create that do not have this styling will be unaffected.



        The code below is commented as well:



        import javafx.application.Application;
        import javafx.geometry.Insets;
        import javafx.geometry.Pos;
        import javafx.scene.Scene;
        import javafx.scene.control.Toggle;
        import javafx.scene.control.ToggleButton;
        import javafx.scene.control.ToggleGroup;
        import javafx.scene.layout.VBox;
        import javafx.stage.Stage;

        public class ButtonHighlights extends Application {

        public static void main(String args) {
        launch(args);
        }

        @Override
        public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Create a ToggleGroup to hold the list of ToggleButtons. This will allow us to allow the selection of only one
        // ToggleButton at a time
        ToggleGroup toggleGroup = new ToggleGroup();

        // Create our 6 ToggleButtons. For this sample, I will use a for loop to add them to the ToggleGroup. This is
        // not necessary for the main functionality to work, but is used here to save time and space
        for (int i = 0; i < 6; i++) {
        ToggleButton button = new ToggleButton("Button #" + i);

        // If you want different styling for the button when it's selected other than the default, you can either
        // use an external CSS stylesheet, or apply the style in a listener like this:
        button.selectedProperty().addListener((observable, oldValue, newValue) -> {

        // If selected, color the background red
        if (newValue) {
        button.setStyle(
        "-fx-background-color: red;" +
        "-fx-text-fill: white");
        } else {
        button.setStyle(null);
        }
        });

        // Add the button to our ToggleGroup
        toggleGroup.getToggles().add(button);
        }

        // Add all our buttons to the scene
        for (Toggle button :
        toggleGroup.getToggles()) {
        root.getChildren().add((ToggleButton) button);
        }

        // Show the Stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
        }
        }



        The Result:




        screenshot







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 18:56









        ZephyrZephyr

        3,6162931




        3,6162931






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464570%2fjava-fx-button-highlight%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks