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

  • SEARCH
  • Home
  • 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 7718187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:06:15+00:00 2026-06-01T03:06:15+00:00

Trying to make a toggle button that hides/shows the right window/pane. Here is a

  • 0

Trying to make a toggle button that hides/shows the right window/pane. Here is a demo example :

package demoapp;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class DemoApp extends Application {

    private static final String styleHide = "-fx-skin: \"com.sun.javafx.scene.control.skin.ButtonSkin\"; -fx-base: gray; -fx-padding: 2; -fx-font-weight: bold; -fx-text-fill: #FFF; -fx-background-radius: 0 10 10 0;";
    private static final String styleShow = "-fx-skin: \"com.sun.javafx.scene.control.skin.ButtonSkin\"; -fx-base: gray; -fx-padding: 2; -fx-font-weight: bold; -fx-text-fill: #FFF; -fx-background-radius: 10 0 0 10;";
    private final double minWidth = 5;
    private final double maxWidth = 170;
    private boolean hidden = true;

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

    @Override
    public void start(Stage primaryStage) {
        BorderPane borderPane = new BorderPane();
        borderPane.setRight(getRightContent());

        VBox box = new VBox();
        box.setStyle("-fx-background-color: lightgray");
        box.setAlignment(Pos.CENTER_RIGHT);
        box.getChildren().add(new Label("Main Content"));
        box.getChildren().add(new Button("Button1"));
        borderPane.setCenter(box);

        StackPane root = new StackPane();
        root.getChildren().add(borderPane);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    private GridPane getRightContent() {

        final Button btn = new Button(" toggle button ");
        final GridPane pane = new GridPane();

        btn.setPrefHeight(40);
        btn.setFocusTraversable(false);
        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                toggleRightContent(btn, pane);
            }
        });

        toggleRightContent(btn, pane);

        Text txt = new Text("Right Content");
        txt.setWrappingWidth(maxWidth - 20);

        pane.setVgap(10);
        pane.setStyle("-fx-padding: 5; -fx-background-color: gray");
        pane.addColumn(0, btn, txt);

        return pane;
    }

    private void toggleRightContent(Button btn, Pane pane) {
        if (hidden) {
            btn.setTranslateX(-5);
            btn.setStyle(styleHide);
            pane.toBack();
            pane.setMaxWidth(maxWidth);
            pane.setMinWidth(maxWidth);
            pane.setPrefWidth(maxWidth);
        } else {
            btn.setTranslateX(-113);
            btn.setStyle(styleShow);
            pane.toFront();
            pane.setMaxWidth(minWidth);
            pane.setMinWidth(minWidth);
            pane.setPrefWidth(minWidth);
        }
        hidden = !hidden;
    }
}

The problem is, this toggle button is partially overlapping the Button1 in main content and preventing it from clicking on it. It is because of pane.toFront(); code above. If i remove this line then this time Button1 is preventing the toggle button to be clickable on it.
How can i solve this problem? Or are there other ways of accomplishing the toggle button functionality differently?
Thanks.

  • 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-01T03:06:16+00:00Added an answer on June 1, 2026 at 3:06 am

    What version of JavaFX do you use?

    With Java 2.1 (developers preview) your app looks next way: enter image description here

    UPDATE:

    You’ve putted Button to the right pane, so having it in front was preventing bottom pane from getting events.

    Better put Button to root, so it wouldn’t depend on content panes:

    public class AccordionStyleButton extends Application {
    
        private static final String styleHide = "-fx-skin: \"com.sun.javafx.scene.control.skin.ButtonSkin\"; -fx-base: gray; -fx-padding: 2; -fx-font-weight: bold; -fx-text-fill: #FFF; -fx-background-radius: 0 10 10 0;";
        private static final String styleShow = "-fx-skin: \"com.sun.javafx.scene.control.skin.ButtonSkin\"; -fx-base: gray; -fx-padding: 2; -fx-font-weight: bold; -fx-text-fill: #FFF; -fx-background-radius: 10 0 0 10;";
        private final double minWidth = 5;
        private final double maxWidth = 170;
        private boolean hidden = true;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        private Pane root;
    
        @Override
        public void start(Stage primaryStage) {
            BorderPane borderPane = new BorderPane();
            root = new Pane();
            root.getChildren().add(borderPane);
    
            borderPane.prefWidthProperty().bind(root.widthProperty());
            borderPane.prefHeightProperty().bind(root.heightProperty());
    
            final Pane rightPane = getRightContent();
            borderPane.setRight(rightPane);
    
            VBox box = new VBox();
            box.setStyle("-fx-background-color: lightgray;");
            box.setAlignment(Pos.CENTER_RIGHT);
            box.getChildren().add(new Label("Main Content"));
            box.getChildren().add(new Button("Button1"));
            box.getChildren().add(new Button("Button2"));
            borderPane.setCenter(box);
    
            primaryStage.setScene(new Scene(root, 300, 250));
    
            final Button btn = new Button(" toggle button ");
            btn.setPrefHeight(40);
            btn.setFocusTraversable(false);
            btn.setOnAction(new EventHandler<ActionEvent>() {
    
                public void handle(ActionEvent event) {
                    toggleRightContent(btn, rightPane);
                }
            });
    
            root.getChildren().add(btn);
            toggleRightContent(btn, rightPane);
    
            primaryStage.show();
        }
    
    
        private GridPane getRightContent() {
            GridPane pane = new GridPane();
    
            Text txt = new Text("Right Content");
            txt.setWrappingWidth(maxWidth - 20);
    
            pane.setVgap(10);
            pane.setStyle("-fx-padding: 5; -fx-background-color: gray;");
            pane.addColumn(0, txt);
    
            return pane;
        }
    
        private void toggleRightContent(Button btn, Pane pane) {
            if (hidden) {
                btn.setStyle(styleHide);
                pane.setMaxWidth(maxWidth);
                pane.setMinWidth(maxWidth);
                pane.setPrefWidth(maxWidth);
                btn.layoutXProperty().bind(root.widthProperty().subtract(maxWidth));
            } else {
                btn.setStyle(styleShow);
                pane.setMaxWidth(minWidth);
                pane.setMinWidth(minWidth);
                pane.setPrefWidth(minWidth);
                btn.layoutXProperty().bind(root.widthProperty().subtract(minWidth).subtract(btn.widthProperty()));
            }
            hidden = !hidden;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a regular button that functions like a toggle button, but
I am trying to make a simple toggle button in javascript. However, the button
I've been trying to make a button toggle a class, as well as show
I'm trying to basically make a toggle button to change the background color of
I'm trying to make a simple link that will toggle my status attribute in
I'm trying to make panel that opens when it's clicked on the button. I
I am trying to make a window that alternates between having an Aero/Glass and
I'm trying to make a custom jQuery toggle function. Right now, I have 2
Hi I have been trying to make a show/hide toggle button with jquery but
I'm trying to make it so that my dropdown menu shows when you click

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.