我做了一个自定义的htmleditor扩展了htmleditor。
我去掉了一些按钮,又加了一些。
现在,我想添加几个按钮,将键入的文本更改为1种特定颜色。
但是我找不到一种方法来改变按钮的setOnAction中的文本颜色。
我试过两种方法。
1:在光标位置插入的HTML
2:设置HTML编辑器颜色的颜色选取器
(jscodeinserthtml字符串允许在光标处使用HTML)
String jsCodeInsertHtml = "function insertHtmlAtCursor(html) {\n" +
" var range, node;\n" +
" if (window.getSelection && window.getSelection().getRangeAt) {\n" +
" range = window.getSelection().getRangeAt(0);\n" +
" node = range.createContextualFragment(html);\n" +
" range.insertNode(node);\n" +
" } else if (document.selection && document.selection.createRange) {\n" +
" document.selection.createRange().pasteHTML(html);\n" +
" }\n" +
"}insertHtmlAtCursor('####html####')";
// add a custom button to the top toolbar.
Node nodetop = editor.lookup(".top-toolbar");
if (nodetop instanceof ToolBar) {
ToolBar topbar = (ToolBar) nodetop;
//var1 color
ImageView graphic = new ImageView(var1pic);
Button colorVar1Button = new Button("", graphic);
colorVar1Button.setFocusTraversable(false);
colorVar1Button.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent arg0) {
WebView webView=(WebView)editor.lookup("WebView");
WebEngine engine= webView.getEngine();
//try 1
engine.executeScript(jsCodeInsertHtml.replace("####html####","
"));
//the HTML is inserted , but no colorchange. i tryed some htmlvariants aswel
//try 2
Node color = editor.lookup(".html-editor-foreground");
((ColorPicker)color).setValue(Color.rgb(200,200,200));
// i notice the button change in the colorpicker ,
// but no change in color happens
topbar.getItems().addAll(colorVar1Button);