This discussion is archived
1 Reply Latest reply: Jun 26, 2013 6:59 AM by shakir.gusaroff RSS

How to show tabs only when CheckMenuItem is selected

user10484841 Newbie
Currently Being Moderated

I have this very basic code which will be used to display tabs only when CheckMenuItem is selected:

 

CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
  toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
   {
   @Override
   public void handle(ActionEvent e)
   {
   // Show or hide tabs
   System.out.println("subsystem1 #1 Enabled!");
   }
   });

This is the code that I want to show or hide when the check box is selected:

 

TabPane tabPane = new TabPane();Tab tab0 = new Tab("blue");
tab.setContent(new Rectangle(200,200, Color.BLUE));Tab tab1 = new Tab("green");
tab.setContent(new Rectangle(200,200, Color.GREEN));
tabPane.getTabs().addAll(tab0, tab1);

Can you tell me how I can show the tabs only when the CheckMenuItem is true? And I want to do this dynamically.

  • 1. Re: How to show tabs only when CheckMenuItem is selected
    shakir.gusaroff Expert
    Currently Being Moderated

    An example using a bind statement:

     

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.CheckMenuItem;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    public class CheckMenuItemSample extends Application {
        @Override
        public void start(Stage primaryStage) {
            final    TabPane tabPane = new TabPane();
         final   CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
    toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent e) {
             
                }
            });
            final Menu menu1 = new Menu("File");
            final Menu menu2 = new Menu("Options");
            final Menu menu3 = new Menu("Help");
    menu1.getItems().add(toolbarSubMenuNavigation);
            MenuBar menuBar = new MenuBar();
    menuBar.getMenus().addAll(menu1, menu2, menu3);
            VBox root = new VBox();
       
         //   Tab tab = new Tab("blue");
            Tab tab0 = new Tab("blue");
    tab0.setContent(new Rectangle(200, 200, Color.BLUE));
            Tab tab1 = new Tab("green");
    tab1.setContent(new Rectangle(200, 200, Color.GREEN));
    tabPane.getTabs().addAll(tab0, tab1);
    root.getChildren().addAll(menuBar, tabPane);
            Scene scene = new Scene(root, 300, 250);
    tabPane.visibleProperty().bind(toolbarSubMenuNavigation.selectedProperty());
         
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
            primaryStage.show();
        }
        public static void main(String[] args) {
            launch(args);
        }
    }

     

     

    An example using a changelistener:

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.CheckMenuItem;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    public class CheckMenuItemSample extends Application {
        @Override
        public void start(Stage primaryStage) {
            final    TabPane tabPane = new TabPane();
         final   CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
            toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent e) {
           
                }
            });
            final Menu menu1 = new Menu("File");
            final Menu menu2 = new Menu("Options");
            final Menu menu3 = new Menu("Help");
    menu1.getItems().add(toolbarSubMenuNavigation);
            MenuBar menuBar = new MenuBar();
            menuBar.getMenus().addAll(menu1, menu2, menu3);
            VBox root = new VBox();
       
         //   Tab tab = new Tab("blue");
            Tab tab0 = new Tab("blue");
    tab0.setContent(new Rectangle(200, 200, Color.BLUE));
            Tab tab1 = new Tab("green");
            tab1.setContent(new Rectangle(200, 200, Color.GREEN));
    tabPane.getTabs().addAll(tab0, tab1);
    root.getChildren().addAll(menuBar, tabPane);
            Scene scene = new Scene(root, 300, 250);
           // tabPane.visibleProperty().bind(toolbarSubMenuNavigation.selectedProperty());
           
    toolbarSubMenuNavigation.selectedProperty().addListener(new ChangeListener<Boolean>() {
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if(newValue.booleanValue()) {
    tabPane.setVisible(true);
                    }
                  else { tabPane.setVisible(false);}
                }} );
           
           
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    tabPane.setVisible(false);
        }
      
        public static void main(String[] args) {
            launch(args);
        }
    }

     

    An example using setOnAction method:

     

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.CheckMenuItem;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    public class CheckMenuItemSample extends Application {
        @Override
        public void start(Stage primaryStage) {
            final    TabPane tabPane = new TabPane();
         final   CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
    toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent e) {
                   
                  if ( toolbarSubMenuNavigation.isSelected()) {
                      tabPane.setVisible(true);
                  }
    else {  tabPane.setVisible(false);}
                    // Show or hide tabs 
                   
    System.out.println("subsystem1 #1 Enabled!");
                   
                }
            });
            final Menu menu1 = new Menu("File");
            final Menu menu2 = new Menu("Options");
            final Menu menu3 = new Menu("Help");
    menu1.getItems().add(toolbarSubMenuNavigation);
            MenuBar menuBar = new MenuBar();
    menuBar.getMenus().addAll(menu1, menu2, menu3);
            VBox root = new VBox();
       
         //   Tab tab = new Tab("blue");
            Tab tab0 = new Tab("blue");
    tab0.setContent(new Rectangle(200, 200, Color.BLUE));
            Tab tab1 = new Tab("green");
    tab1.setContent(new Rectangle(200, 200, Color.GREEN));
    tabPane.getTabs().addAll(tab0, tab1);
    root.getChildren().addAll(menuBar, tabPane);
            Scene scene = new Scene(root, 300, 250);
          
           
           
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    tabPane.setVisible(false);
          
        }
      
        public static void main(String[] args) {
            launch(args);
        }
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points