user2130057
user2130057

Reputation: 406

Removing EventListener

I currently have an application which will create a textarea wherever the user clicks. However, I want the pane to only be editable when a certain condition is true. The clickable area never goes away, though. How can I change this so the area is only clickable if myAnchorPane.isVisible() is true?

double oldHeight = 0;
double oldWidth = 0;
@FXML
private void handleTextButton() {
    System.out.println("Text Clicked");
    TextHeaderTools.setVisible(false);
    BackgroundTools.setVisible(false);
    VideoTools.setVisible(false);
    PageTitleTools.setVisible(false);
    TemplateTools.setVisible(false);
    ImageTools.setVisible(false);
    TextTools.setVisible(true);


    workspace.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent me) {
            int x = (int) me.getX();
            int y = (int) me.getY();

            System.out.println("(" + x +", " + y +")");

            InlineCssTextArea newArea = new InlineCssTextArea();
            newArea.relocate(x, y);
            newArea.setStyle("-fx-background-color: transparent;");
            Text textHolder = new Text();
            newArea.setPrefSize(40,40);

            textHolder.textProperty().bind(newArea.textProperty());
            textHolder.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
                @Override
                public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
                    if (oldHeight != newValue.getHeight()) {
                        oldHeight = newValue.getHeight();
                        newArea.setPrefHeight(textHolder.getLayoutBounds().getHeight() + 30);
                    }
                    if (oldWidth != newValue.getWidth()){
                        oldWidth = newValue.getWidth();
                        newArea.setPrefWidth(textHolder.getLayoutBounds().getWidth() + 30); 
                    }
                }
            });             

            workspace.getChildren().addAll(newArea);


        } //end handle

    });

}

EDIT:

The condition is

myAnchorPane.isVisible()

Upvotes: 0

Views: 88

Answers (1)

Tomas Mikula
Tomas Mikula

Reputation: 6537

You can achieve this nicely with ReactFX.

Instead of workspace.addEventHandler(MOUSE_CLICKED, ...), do this:

EventStreams.eventsOf(workspace, MOUSE_CLICKED)
        .suppressWhen(myAnchorPane.visibleProperty().not())
        .subscribe((MouseEvent me) -> {
            // handle mouse click
        });

I see you use RichTextFX, so you already have ReactFX as a dependency anyway.

Upvotes: 1

Related Questions