禁用节点而不在javafx中禁用其子节点

时间:2015-06-26 10:31:43

标签: java javafx javafx-2 javafx-8

是否有解决方案在javaFX中禁用节点而不禁用其子节点? 我想禁用窗格中的所有节点,除了其中一个节点动态。 我尝试过这个解决方案和其他类似的解决方案,但不起作用,我也认为它有糟糕的性能!

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

       }

   }

编辑:

我也试过这个解决方案: 我为主要疼痛添加了透明窗格,然后为其添加特殊节点。但它对复杂的组件不起作用,因为我应该保留它的孩子的大小和位置!

我确实希望用户与整个场景的一个节点进行交互,其他节点应该被禁用。

2 个答案:

答案 0 :(得分:1)

Node的 BooleanProperty disableProperty()的当前javadoc说:“...... 将disable设置为true将导致此节点和任何子节点被禁用 .......

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

所以无法禁用没有子节点的节点。

答案 1 :(得分:0)

下面是一个例子,我们如何做到这一点。它只是一个小样本。 如果您进入任何窗格,则禁用此窗格以外的所有窗格。因此,与用户交互的窗格处于活动状态。

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 );
    }

}
相关问题