2017-01-18 16 views
2

既存のjavafx HTMLエディタでSuperscriptとSubscriptコントロールを追加する回避策を提案できますか? Bold、Italics、Superscript、Subscript、およびフォントセレクタをコントロールとして持つFormulaフィールドエディタを開発しようとしています。javafxの上付き文字と下付き文字

答えて

2

これは、APIのアクセスしない部分にアクセスすることを含む、かなり深刻なハッキングがないと実行できません(AFAIK)。次の多かれ少なかれの作品。私はHTMLEditorSkinsource codeに基づいています。関連パッケージにアクセスできるようにIDEを説得する必要があるかもしれません。これは特に、推奨されていない、そしてそれはほぼ確実にあなたが数式エディタを作成するための強力なアプローチが必要な場合は、私はおそらくあなた自身を構築し、代わりに使用して/ HTMLEditorをハッキング検討する9.

import com.sun.javafx.webkit.Accessor; 
import com.sun.webkit.WebPage; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.ToggleButton; 
import javafx.scene.control.ToggleGroup; 
import javafx.scene.control.ToolBar; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.web.HTMLEditor; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 

public class HTMLEditorHack extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     HTMLEditor editor = new HTMLEditor(); 
     Scene scene = new Scene(editor); 
     editor.applyCss(); 
     editor.layout(); 

     WebView webView = (WebView) editor.lookup(".web-view"); 
     ToolBar toolbar = (ToolBar) editor.lookup(".bottom-toolbar"); 
     ToggleGroup toggleGroup = new ToggleGroup(); 

     createToggleButton("superscript", "Super", toggleGroup, webView, toolbar); 
     createToggleButton("subscript", "Sub", toggleGroup, webView, toolbar); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private void createToggleButton(String command, String label, ToggleGroup toggleGroup, WebView webView, ToolBar toolbar) { 
     ToggleButton button = new ToggleButton(label); 
     button.setFocusTraversable(false); 
     button.selectedProperty().addListener((obs, wasSelected, isSelected) -> { 
      WebPage page = Accessor.getPageFor(webView.getEngine()); 
      if (page.queryCommandState(command) != isSelected) { 
       page.executeCommand(command, null); 
      } 
     }); 
     button.setToggleGroup(toggleGroup); 
     toolbar.getItems().add(button); 

     EventHandler<Event> updateState = e -> { 
      Platform.runLater(() -> { 
       WebPage page = Accessor.getPageFor(webView.getEngine()); 
       button.setDisable(! page.queryCommandEnabled(command)); 
       button.setSelected(page.queryCommandState(command)); 
      }); 
     }; 
     webView.addEventHandler(KeyEvent.ANY, updateState); 
     webView.addEventHandler(MouseEvent.MOUSE_PRESSED, updateState); 
     webView.addEventHandler(MouseEvent.MOUSE_RELEASED, updateState); 
    } 

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

Javaで動作しません。 。サードパーティのライブラリRichTextFXがあり、さまざまなスタイルの編集可能なテキスト領域を作成するために使用できます。そこから始めて、スタイリング用の独自のコントロールを追加します。

関連する問題