2012-11-19 78 views
2

實際上我正在尋找非常相似,這個線程的東西:JavaFX的HTML編輯器

How to hide the controls of HTMLEditor?

所以基本上我嘗試自定義按鈕添加到JavaFX的HTML編輯器,但不同之處在於它是通過實施FXML。

所以我的問題是:

是否有一個「變通」,當它通過FXML實現的自定義按鈕添加到HTML編輯器?

回答

2

這裏是一些sample code定製HTMLEditor並添加一個自定義按鈕。示例代碼不使用fxml,但如果使用fxml,它確實非常相似。您可以在fxml中定義HTMLEditor,並使用標準@FXML註釋將其注入到Controller中。一旦你有了對編輯器的引用,就可以使用適當的示例代碼變體在Java代碼中定製它。對於添加的按鈕,只需使用Java創建而不是使用fxml,這將更簡單。

+0

謝謝,現在我得到了線索! :) – fraggle

+0

這給我帶來了與此相關的另一個問題 - 是否可以在頂部工具欄和底部工具欄下添加另一個工具欄? – fraggle

+0

我已經使它適用於javaFX9,看到我的答案,如果你想更新你的要點。感謝你爲我提供了很多幫助,特別是字體。請給 – pdem

-1

樣品溶液:

htmlEditor.setVisible(false); 
    Platform.runLater(new Runnable() { 

     @Override 
     public void run() { 
      Node[] nodes = htmlEditor.lookupAll(".tool-bar").toArray(new Node[0]); 
      for (Node node : nodes) { 
       node.setVisible(false); 
       node.setManaged(false); 
      } 
      htmlEditor.setVisible(true); 
     } 

    }); 
+2

添加一些解釋給你的代碼。 –

0

我已經修改了javaFX9的@jewelsea答案。

我也添加了一些自定義來移動工具欄。主要想法是通過CSS選擇器獲取所有組件,然後修改或隱藏它們。閱讀HTMLEditorSkin類以獲取CSS類名稱,例如對齊按鈕的「.html-editor-align-center」。

import java.util.Iterator; 
import java.util.List; 
import java.util.Set; 
import java.util.concurrent.atomic.AtomicBoolean; 
import java.util.regex.Pattern; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.MenuButton; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.RadioMenuItem; 
import javafx.scene.control.ToolBar; 
import javafx.scene.effect.DropShadow; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.GridPane; 
import javafx.scene.web.HTMLEditor; 
import javafx.stage.Stage; 

public class HTMLEditorCustomizationSample2 extends Application { 
    // limits the fonts a user can select from in the html editor. 
    private static final ObservableList<String> limitedFonts = FXCollections.observableArrayList("Arial", 
      "Times New Roman", "Courier New", "Comic Sans MS"); 
    private HTMLEditor htmlEditor; 

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

    @SuppressWarnings("unchecked") 
    @Override 
    public void start(Stage stage) { 
     htmlEditor = new HTMLEditor(); 
     stage.setScene(new Scene(htmlEditor)); 
     stage.show(); 

     customizeEditor(htmlEditor); 

    } 

    private void customizeEditor(HTMLEditor htmlEditor) { 
     // hide controls we don't need. 
     Node seperator = htmlEditor.lookup(".separator"); 
     seperator.setVisible(false); 
     seperator.setManaged(false); 
     hideByClass(htmlEditor, ".separator"); 
     hideByClass(htmlEditor, ".html-editor-cut", ".html-editor-copy", ".html-editor-paste", ".html-editor-strike", 
       ".html-editor-hr"); 
     hideByClass(htmlEditor, ".html-editor-align-left" 
       , ".html-editor-align-center" 
       , ".html-editor-align-right" 
       , ".html-editor-align-justify", ".html-editor-outdent" 
       , ".html-editor-indent", ".html-editor-bullets" 
       , ".html-editor-numbers"); 
     // Move the toolbars 
     Node top= htmlEditor.lookup(".top-toolbar"); 
     GridPane.setConstraints(top,1,0,1,1); 
     Node bottom= htmlEditor.lookup(".bottom-toolbar"); 
     GridPane.setConstraints(bottom,0,0,1,1); 
     Node web= htmlEditor.lookup("WebView"); 
     GridPane.setConstraints(web,0,1,2,1); 

     // modify font selections. 
     int i = 0; 
     Set<Node> fonts = htmlEditor.lookupAll(".font-menu-button"); 
     Iterator<Node> fontsIterator = fonts.iterator(); 
     fontsIterator.next(); 
     ComboBox<String> formatComboBox = (ComboBox<String>) fontsIterator.next(); 

     formatComboBox.itemsProperty().addListener((obs, old, value) -> { 
      if (value.size() != limitedFonts.size()) {// should loop on array for equality 
       Platform.runLater(() -> { 
        value.clear(); 
        // stop.set(true); 
        value.addAll(limitedFonts); 
        formatComboBox.setValue(limitedFonts.get(0)); 
       }); 

      } 
     }); 

     // add a custom button to the top toolbar. 
     Node node = htmlEditor.lookup(".top-toolbar"); 
     if (node instanceof ToolBar) { 
      ToolBar bar = (ToolBar) node; 
      ImageView graphic = new ImageView(
        new Image("http://bluebuddies.com/gallery/title/jpg/Smurf_Fun_100x100.jpg", 16 , 16, true, true)); 
      graphic.setEffect(new DropShadow()); 
      Button smurfButton = new Button("", graphic); 
      bar.getItems().add(smurfButton); 
      smurfButton.setOnAction(new EventHandler<ActionEvent>() { 
       @Override 
       public void handle(ActionEvent arg0) { 
        htmlEditor.setHtmlText("<font face='Comic Sans MS' color='blue'>Smurfs are having fun :-)</font>"); 
       } 
      }); 
     } 
    } 

    private void hideByClass(HTMLEditor htmlEditor, String... selectors) { 
     for (String selector : selectors) { 
      Set<Node> nodes = htmlEditor.lookupAll(selector); 
      for (Node node : nodes) { 
       node.setVisible(false); 
       node.setManaged(false); 
      } 
     } 
    } 



    @Override 
    public void stop() throws Exception { 

     super.stop(); 
     System.out.println(htmlEditor.getHtmlText()); 
    } 

}