RichTextFx:如何将绑定添加到 StyleClassedTextArea

RichTextFx: How to add binding to StyleClassedTextArea

如果我问的很明显但我不知道如何使用 RichTextFx 在 StyleClassedTextArea 中创建绑定,请原谅我。我想要做的是拥有该区域和一个按钮(这将触发某种文本处理)并在该区域为空时禁用该按钮。 我的代码如下

Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));             
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

Controller.java

package application;

import java.net.URL;
import java.util.ResourceBundle;
import org.fxmisc.richtext.StyleClassedTextArea;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ObservableStringValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

public class Controller implements Initializable {
    @FXML private StyleClassedTextArea mainArea;
    @FXML private Button processButton;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        processButton.disableProperty().bind(Bindings.isEmpty((ObservableStringValue) mainArea.textProperty()));
    }   
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.richtext.StyleClassedTextArea?>


<AnchorPane prefHeight="308.0" prefWidth="291.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.Controller">
   <children>
      <VBox layoutY="6.0" prefHeight="308.0" prefWidth="291.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <StyleClassedTextArea fx:id="mainArea" />
            <Button fx:id="processButton" mnemonicParsing="false" text="process" />
         </children>
      </VBox>
   </children>
</AnchorPane>

这是因为 mainTextArea.textProperty() returns ObservableValue<String> 当我施放它时会抛出异常,并具有以下堆栈跟踪

javafx.fxml.LoadException: 
/C:/Users/dcg601/workspace/javafx/bin/application/Main.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: org.reactfx.value.SuspendableValWrapper cannot be cast to javafx.beans.value.ObservableStringValue
    at application.Controller.initialize(Controller.java:20)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more

这很疯狂但很管用:

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    // TODO Auto-generated method stub
    ObservableValue<String> qwer = mainArea.textProperty();

    ObservableStringValue qwerQ = new ObservableStringValue() {
        public String get() { return qwer.getValue(); }
        public String getValue() { return qwer.getValue(); }
        public void addListener( InvalidationListener listener ) {
            qwer.addListener( listener );
        }
        public void removeListener( InvalidationListener listener ) {
            qwer.removeListener( listener );
        }
        public void addListener( ChangeListener<? super String>  listener ) {
            qwer.addListener( listener );
        }
        public void removeListener( ChangeListener<? super String> listener ) {
            qwer.removeListener( listener );
        }
    };

    processButton.disableProperty().bind(Bindings.isEmpty( qwerQ ) );
}

但添加一个简单的监听器可能更好:

mainArea.textProperty().addListener( ( ov, oldv, newv ) -> {
     processButton.setDisable( newv.isEmpty() );
});