![](/img/trans.png)
[英]How to get specific instance of controller from parent controller at JavaFX2?
[英]how get Only text from Javafx2 HTMLEditor
我有一個HTMLEditor,我輸入了“我的簡單文字”。
@FXML
public HTMLEditor htmlEditor;
什么時候說
htmlEditor.getHtmlText();
這回歸
<html><head></head><body contenteditable="true"><p style="text-align: left;"><font face="'Segoe UI'">My Simple Text</font></p></body></html>
但想要沒有html標簽的文本即
My Simple Text
how can i do it?
這對Jsoup來說實際上很簡單。
public static String html2text(String html) {
return Jsoup.parse(html).text();
}
source: 從String中刪除HTML標記
在HtmlEditor上應用getHtmlText()后,將生成的html代碼字符串傳遞給以下方法:
public static String getText(String htmlText) {
String result = "";
Pattern pattern = Pattern.compile("<[^>]*>");
Matcher matcher = pattern.matcher(htmlText);
final StringBuffer text = new StringBuffer(htmlText.length());
while (matcher.find()) {
matcher.appendReplacement(
text,
" ");
}
matcher.appendTail(text);
result = text.toString().trim();
return result;
}
您應該從您從此HTMLEditor獲取的Html Text中刪除所有html標記。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class HTMLEditorDemo extends Application {
@Override
public void start(Stage primaryStage) {
HTMLEditor editor = new HTMLEditor();
Button b = new Button("Show Text");
b.setOnAction((ActionEvent e) -> {
String htmlText = editor.getHtmlText();
stripHTMLTags(htmlText);
});
VBox vBox = new VBox(b, editor);
Scene scene = new Scene(vBox, 800, 600);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void stripHTMLTags(String htmlText) {
Pattern pattern = Pattern.compile("<[^>]*>");
Matcher matcher = pattern.matcher(htmlText);
final StringBuffer sb = new StringBuffer(htmlText.length());
while(matcher.find()) {
matcher.appendReplacement(sb, " ");
}
matcher.appendTail(sb);
System.out.println(sb.toString().trim());
}
}
帕特里克
試試這個
WebView webView = (WebView) htmlEditor.lookup("WebView");
((HTMLBodyElementImpl) ((NodeListImpl) webView.getEngine().getDocument().getElementsByTagName("body")).item(0)).getTextContent();
您還可以設置內容文本或關注webview
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.