user4205741
user4205741

Reputation:

disabling a node without disable its children in javafx

Is there any solution to disable a node in javaFX without disabling its children ? I want to disable all nodes in a pane except one of them dynamically. I tried this solution and other solutions like this but doesn't work and i also think it has bad performance!

 node.getParent().requestFocus();
   for(int i=0 ; i<pane.getChildren().size() ; i++){
       if( !pane.getChildren().get(i).isFocused()){
           pane.getChildren().get(i).setDisable(true);

       }

   }

Edited:

also i tried this solution: I added a transparent pane to main pain and then add special node to it . But it doesb't work for complex components because I should keep sizes and locations of it's children !

I exactly want that user interact with one node of whole scene and other nodes should be disable .

Upvotes: 4

Views: 5693

Answers (2)

Hareesh
Hareesh

Reputation: 734

Below is an example, how we can do that. Its just small sample. If you entered into any of the pane,then other than this all panes are disabled. So the pane which is interacting with user is active.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @version 1.0 Jun 29, 2015
 */
public class EnableAndDisableTest extends Application{

    private ObservableList<Pane> rootPanes = FXCollections.observableArrayList();

    /**
     * @see javafx.application.Application#start(javafx.stage.Stage)
     * @param primaryStage
     * @throws Exception
     */
    @Override
    public void start( Stage primaryStage ) throws Exception{

        StackPane rootPane = new StackPane();

        VBox pane1 = new VBox();
        pane1.setPadding( new Insets( 20 ) );
        Button button = new Button( "Button on Pane_1" );
        pane1.getChildren().add( button );
        pane1.addEventHandler( MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
            @Override
            public void handle( MouseEvent event ){
                Pane pane = (Pane) event.getSource();
                enablePane( pane );
            }
        } );

        VBox pane2 = new VBox();
        pane2.setPadding( new Insets( 20 ) );
        Button button2 = new Button( "Button on Pane_2" );

        pane2.getChildren().add( button2 );
        pane2.addEventHandler( MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
            @Override
            public void handle( MouseEvent event ){
                Pane pane = (Pane) event.getSource();
                enablePane( pane );
            }
        } );

        VBox pane3 = new VBox();
        Button button3 = new Button( "Button on Pane_3" );
        pane3.setPadding( new Insets( 20 ) );
        pane3.getChildren().add( button3 );
        pane3.addEventHandler( MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
            @Override
            public void handle( MouseEvent event ){
                Pane pane = (Pane) event.getSource();
                enablePane( pane );
            }
        } );

        rootPanes.addAll( pane1, pane2, pane3 );

        HBox horizontalPane = new HBox();
        horizontalPane.getChildren().addAll( rootPanes );

        rootPane.getChildren().add( horizontalPane );

        Scene scene = new Scene( rootPane );
        primaryStage.setScene( scene );

        primaryStage.show();

    }

    private void enablePane( Pane pane ){
        for( Pane rootPane : rootPanes ){
            if( rootPane.equals( pane ) ){
                for( Node child : rootPane.getChildren() ){
                    child.setDisable( false );
                }
                rootPane.setStyle( "-fx-background-color: blue;" );
            }
            else{
                for( Node child : rootPane.getChildren() ){
                    child.setDisable( true );
                }
                rootPane.setStyle( "-fx-background-color: white;" );
            }
        }
    }

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

}

Upvotes: 0

gecharita
gecharita

Reputation: 57

The current javadoc of BooleanProperty disableProperty() of Node says: "...... Setting disable to true will cause this Node and any subnodes to become disabled.......

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#setDisable-boolean-

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#disableProperty

So you cannot disable one Node without its children.

Upvotes: 2

Related Questions