Javafx 2 JavaFXHTML编辑器

Javafx 2 JavaFXHTML编辑器,javafx-2,javafx,fxml,Javafx 2,Javafx,Fxml,实际上,我正在寻找与此线程非常相似的东西: 因此,基本上我尝试向JavaFXHTML编辑器添加一个自定义按钮,但不同之处在于它是通过FXML实现的 所以我的问题是: 当html编辑器通过FXML实现时,是否有一种“变通方法”可以将自定义按钮添加到html编辑器中 以下是一些自定义HTMLEditor并向其添加自定义按钮的方法。示例代码不使用fxml,但如果使用fxml,它实际上非常类似。您可以在fxml中定义HTMLEditor,并使用标准的@fxml注释将其注入控制器中。一旦有了对编辑器的引

实际上,我正在寻找与此线程非常相似的东西:

因此,基本上我尝试向JavaFXHTML编辑器添加一个自定义按钮,但不同之处在于它是通过FXML实现的

所以我的问题是:

当html编辑器通过FXML实现时,是否有一种“变通方法”可以将自定义按钮添加到html编辑器中

以下是一些自定义HTMLEditor并向其添加自定义按钮的方法。示例代码不使用fxml,但如果使用fxml,它实际上非常类似。您可以在fxml中定义HTMLEditor,并使用标准的
@fxml
注释将其注入
控制器中。一旦有了对编辑器的引用,就可以使用示例代码的适当变体在Java代码中对其进行自定义。对于添加的按钮,只需使用Java而不是fxml创建它,这样会更简单。

示例解决方案是:

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

    });

我已经修改了javaFX9的@jewelsea答案

我还添加了一些自定义来移动工具栏。其主要思想是通过css选择器获取所有组件,然后修改或隐藏它们。阅读类HTMLEditorSkin以获得CSS类名称,如对齐按钮的“.html编辑器对齐中心”

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

}
import java.util.Iterator;
导入java.util.List;
导入java.util.Set;
导入java.util.concurrent.AtomicBoolean;
导入java.util.regex.Pattern;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.Node;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.MenuButton;
导入javafx.scene.control.MenuItem;
导入javafx.scene.control.RadioMenuItem;
导入javafx.scene.control.ToolBar;
导入javafx.scene.effect.DropShadow;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.GridPane;
导入javafx.scene.web.HTMLEditor;
导入javafx.stage.stage;
公共类HTMLEditorCustomizationSample2扩展了应用程序{
//限制用户可以在html编辑器中选择的字体。
私有静态最终ObservableList limitedFonts=FXCollections.observableArrayList(“Arial”,
“泰晤士报新罗马”、“信使新”、“漫画无MS”);
私有HTMLEditor HTMLEditor;
公共静态void main(字符串[]args){
发射(args);
}
@抑制警告(“未选中”)
@凌驾
公众假期开始(阶段){
htmlEditor=新的htmlEditor();
stage.setScene(新场景(htmlEditor));
stage.show();
定制编辑器(htmlEditor);
}
私有void自定义编辑器(HTMLEditor HTMLEditor){
//隐藏我们不需要的控件。
节点分隔符=htmlEditor.lookup(“.separator”);
separator.setVisible(假);
separator.setManaged(false);
hideByClass(htmlEditor,“.separator”);
hideByClass(htmlEditor,“.html编辑器剪切”、“.html编辑器复制”、“.html编辑器粘贴”、“.html编辑器删除”,
“.html编辑器hr”);
hideByClass(htmlEditor,“.html编辑器左对齐”
,“.html编辑器对齐中心”
,“.html编辑器右对齐”
,“.html编辑器对齐对齐对齐”,“.html编辑器输出”
,“.html编辑器缩进”,“.html编辑器项目符号”
,“.html编辑器编号”);
//移动工具栏
Node top=htmlEditor.lookup(“.top工具栏”);
设置约束(top,1,0,1,1);
Node bottom=htmlEditor.lookup(“.bottom toolbar”);
setConstraints(底部,0,0,1,1);
Node web=htmlEditor.lookup(“WebView”);
设置约束(web,0,1,2,1);
//修改字体选择。
int i=0;
设置字体=htmlEditor.lookupAll(“.font菜单按钮”);
迭代器fontsIterator=fonts.Iterator();
fontsIterator.next();
ComboBox formatComboBox=(ComboBox)fontsIterator.next();
formatComboBox.itemsProperty().addListener((obs,旧,值)->{
if(value.size()!=limitedFonts.size()){//应在数组上循环以获得相等性
Platform.runLater(()->{
value.clear();
//停止。设置(true);
value.addAll(limitedFonts);
formatComboBox.setValue(limitedFonts.get(0));
});
}
});
//将自定义按钮添加到顶部工具栏。
Node=htmlEditor.lookup(“.top工具栏”);
if(工具栏的节点实例){
工具栏栏=(工具栏)节点;
ImageView图形=新建ImageView(
新图像(“http://bluebuddies.com/gallery/title/jpg/Smurf_Fun_100x100.jpg“,16,16,真的,真的”);
graphic.setEffect(新的DropShadow());
按钮蓝精灵按钮=新按钮(“,图形);
bar.getItems().add(smurfButton);
smurfButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent arg0){
setHtmlEditor.setHtmlText(“蓝精灵玩得很开心:-”);
}
});
}
}
私有void hideByClass(HTMLEditor HTMLEditor、字符串…选择器){
用于(字符串选择器:选择器){
Set nodes=htmlEditor.lookupAll(选择器);
用于(节点:节点){
node.setVisible(false);
node.setManaged(false);
}
}
}
@凌驾
public void stop()引发异常{
super.stop();
System.out.println(htmlEditor.getHtmlText());
}
}

谢谢,现在我有了线索!:)这给我带来了另一个与此相关的问题-是否可以在顶部工具栏和底部工具栏下面添加另一个工具栏