我在尝试获取文字(在他们的位置使用换行符)来自具有内容类型" text / html&#34的JTextPane组件,我做错了什么? ; ?
textPane.getText()
返回HTML但空白 head
和body
。
textPane.getDocument().getText()
返回空字符串。
P.S。:我在相同的代码中也没有换行和其他问题,我应该用相同的代码将它们作为独立问题提出来吗?
代码:
package test;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
//import javax.swing.text.StyledEditorKit;
public class TextPaneTester extends javax.swing.JFrame {
public TextPaneTester() {
initComponents();
myInitComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
contentScrollPane = new javax.swing.JScrollPane();
content = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
contentScrollPane.setViewportView(content);
// layout code by NetBeans
}// </editor-fold>
private void myInitComponents(){
//content.setEditorKit(new StyledEditorKit());
contentScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
private void fetchURL(String url){
try{
// URL(URL baseURL[, String relativeURL])
URL helpURL = new URL(url);
System.out.println(helpURL);
this.content.setPage(helpURL);
try {
System.out.println("This far!");
//System.out.println(this.content.getDocument());
System.out.println("getDocument() on JTextPane gives " + this.content.getDocument());
System.out.println("getText() on JTextPane gives " + this.content.getText());
// Why this line retuens an empty string?
String text = this.content.getDocument().getText(0, this.content.getDocument().getLength());
System.out.println("getDocument.getText() on JTextPane gives " + text);
System.out.println("This far 2!");
// this.translation.setPage(helpURL);
} catch (BadLocationException ex) {
System.out.println("Why this far!");
Logger.getLogger(TextPaneTester.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}
// return this.content;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
String url = "https://en.wikipedia.org/wiki/Virtaal";
TextPaneTester reader = new TextPaneTester();
reader.fetchURL(url);
reader.setVisible(true);
}
});
}
private javax.swing.JTextPane content;
private javax.swing.JScrollPane contentScrollPane;
// End of variables declaration
}
输出:
run:
https://en.wikipedia.org/wiki/Virtaal
This far!
getDocument() on JTextPane gives javax.swing.text.html.HTMLDocument@1767743f
getText() on JTextPane gives <html>
<head>
</head>
<body>
<p style="margin-top: 0">
</p>
</body>
</html>
getDocument.getText() on JTextPane gives
This far 2!
<html>
<head>
</head>
<body>
<p style="margin-top: 0">
</p>
</body>
</html>
Let's see what the parser gives:
BUILD SUCCESSFUL (total time: 55 seconds)
答案 0 :(得分:3)
像往常一样,documentation是你应该看的第一个地方:
如果文档是异步加载的,文档将立即通过调用
setDocument
安装到编辑器中,这将调用文档属性更改事件,然后将创建一个将开始执行实际加载的线程。在这种情况下,直接调用此方法不会触发页面属性更改事件,而是在执行加载的线程完成时触发。它也将在事件派发线程上触发。
您正在页面加载完成之前阅读文档。通过监视page
属性等待加载:
this.content.addPropertyChangeListener("page",
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
System.out.println("getText() on JTextPane gives "
+ content.getText());
}
});
this.content.setPage(helpURL);