为什么JavaFX HTMLEditor在对话框中显示的编辑器不显示文本?



我在 JavaFXDialog窗口中显示HTMLEditor文本时遇到问题。有关说明该问题的应用代码,请参阅消息末尾。

目标:在已设置为Dialog窗口中dialogPane内容的HTMLEditor中显示和编辑文本。

问题HTMLEditor中的文本不可见。它在那里(见下面的"观察"),但看不到。

测试应用说明:两个按钮具有相同的onAction值(应用代码中actionHandler)。单击任一按钮可初始化对话框,并在显示对话框之前dialogEditor。如果单击"Button#1",则使用appHtmlEditor中的htmlText初始化dialogEditorhtmlText。单击 Button#2 使用sampleText值(显示在按钮下方)初始化dialogEditor。如果通过单击"确定"关闭对话框,则返回dialogEditorhtmlText值并将其设置为appHtmlEditorhtmlText

观察:单击按钮#2将显示对话框,...但没有出现sampleText,尽管sampleText在那里,因为如果通过单击"确定"按钮立即关闭对话框,则返回来自dialogEditorhtmlText(再次 - 不可见)并设置为appHtmlEditorhtmlText值,这会导致该htmlText发生变化。

这个问题显然与同时使用HTMLEditorDialog有关。我在其他上下文中以相同的方式使用Dialog没有任何问题,演示应用程序本身中的HTMLEditor也可以正常运行。但出于某种原因,将HTMLEditor用作此处描述的Dialog子项是行不通的。FWIW,我尝试使用Stage代替Dialog,结果是一样的,这表明问题与Dialog没有直接关系,是由于HTMLEditorWindow之间的一些不兼容......但我把它留给那些忘记了比我所知道的更多东西的人。

public class HtmlEditorDialogTestApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
initializeButtonBoxAndButtons()
.initializeLabelBox()
.initializeSeparator()
.initializeHtmlEditor()
.initializeRoot();
display(stage);
}
private Button button1, button2;
private HBox buttonBox, labelBox;
private Label labelTag;
private Text sampleText;
private Separator separator;
private HTMLEditor appHtmlEditor, dialogEditor;
private Dialog<String> dialog;
private VBox root;
private EventHandler<ActionEvent> actionHandler = e -> {
// Create and initialize dialogEditor
if (Objects.isNull(dialogEditor))
dialogEditor = new HTMLEditor();
if (e.getSource() == button1)
dialogEditor.setHtmlText(appHtmlEditor.getHtmlText());
else if (e.getSource() == button2)
dialogEditor.setHtmlText(sampleText.getText());
else
throw new RuntimeException("how did this happen?!!");

// Create and initialize dialog
if (Objects.isNull(dialog))
dialog = new Dialog<>();
dialog.setHeaderText("HTMLEditor Dialog");
dialog.setResultConverter((buttonType) -> {
return buttonType.getButtonData() == ButtonData.OK_DONE
? dialogEditor.getHtmlText()
: null;
});

// Initialize dialog pane
dialog
.getDialogPane()
.setContent(dialogEditor);
dialog
.getDialogPane()
.getButtonTypes()
.addAll(ButtonType.OK, ButtonType.CANCEL);

// Display dialog and handle return
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String resultText = result.get().length() > 0
? result.get()
: "empty String ("")";
System.out.println("result text: " + resultText);
appHtmlEditor.setHtmlText(resultText);
}
else
System.out.println("nothing new from dialog editor!");

// Cleanup
dialog = null;
dialogEditor = null;
};

private HtmlEditorDialogTestApp initializeButtonBoxAndButtons() {
// Create and initialize buttonBox
buttonBox = new HBox();
buttonBox.setSpacing(10);
buttonBox.setAlignment(Pos.CENTER_LEFT);
// Create and initialize textFromEditorButton
button1 = new Button("Button#1: Open dialog with editor text");
button1.setOnAction(actionHandler);
// Create and initialize textFromLabelButton
button2 = new Button("Button#2: Open dialog with label text");
button2.setOnAction(actionHandler);
// Add buttons to buttonBox
buttonBox
.getChildren()
.addAll(button1, button2);
return this;
}
private HtmlEditorDialogTestApp initializeLabelBox() {
// Create and initialize labelBox, labelTag and sampleText
labelBox = new HBox();
labelBox.setSpacing(5);
labelTag = new Label("Sample text ->      ");
sampleText = new Text("Explicit text to initialize HTMLEditor in dialog");
labelBox
.getChildren()
.addAll(labelTag, sampleText);
return this;
}
private HtmlEditorDialogTestApp initializeSeparator() {
separator = new Separator();
VBox.setMargin(separator, new Insets(10, 0, 10, 0));
return this;
}
private HtmlEditorDialogTestApp initializeHtmlEditor() {
// Create & initialize htmlEditor
appHtmlEditor = new HTMLEditor();
appHtmlEditor.setPrefHeight(350);
appHtmlEditor.setPrefWidth(600);
appHtmlEditor.setHtmlText("Original text for htmlEditor");
VBox.setVgrow(appHtmlEditor, Priority.SOMETIMES);
return this;
}
private HtmlEditorDialogTestApp initializeRoot() {
// Create and initialize root
root = new VBox();
root.setPadding(new Insets(10, 10, 10, 10));
root.setSpacing(5);
root.setAlignment(Pos.CENTER_LEFT);
root.setStyle(
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: cornflowerblue;");
// Add children to root
root
.getChildren()
.addAll(
buttonBox,
labelBox,
separator,
appHtmlEditor);
return this;
}
private void display(Stage stage) {
// Create scene and display
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(600);
stage.setHeight(350);
stage.setTitle("HTMLEditor Dialog Application");
stage.show();
}
}

希望有一个解决方案,或者如果一个解决方案不可用(因为这是一个错误),那么也许是一个解决方法。谢谢!

问题很可能是一个错误。错误报告已提交,可以在以下位置进行跟踪: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8192059

最新更新