是否有一种"简单"的方式通过JEditorPane或JTextPane以彩色/突出显示的方式向用户显示xml数据?
我有一个JEditorPane显示以编程方式生成的HTML(在运行时).到目前为止,当我添加"行"时,我在字符串缓冲区中重新创建整个HTML文本,然后将其传递给JEditorPane.setText方法.
现在创建的HTML变得非常大(可以达到几MB),我只想在最后添加我的新行,而不是重新生成所有HTML文本.
我试图在最后追加的原因是为了避免Swing(或套件?)必须再次渲染/解析整个文本.因为即使HTML生成不是在EDT中执行,而是在另一个swingworker线程中执行,"渲染"也需要很长时间.或者最好的是有一个显示渲染进度的进度条,这是不可能的(是吗?).
所以我的想法是简单地追加到最后,但如果你有更好的想法,欢迎!
由于我的文本是在HTML表格中格式化的,我想在此表的末尾附加我的新文本.为此,我尝试使用它insertBeforeEnd,HTMLDocument但即使我尝试了大量的解决方案,我也无法使它工作.请注意,我只有"table"标签.
这是我的代码的一部分
JEditorPane jep = new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
jep.setEditorKit(kit);
jep.setDocument(doc);
//setting dummy text within a HTML table
jep.setText("<table><tr><td>A line of text</td></tr><tr><td>Another line of text</td></tr></table>");
Run Code Online (Sandbox Code Playgroud)
现在在这个表的末尾添加一些文本
//getting the Table Element
Element e = doc.getElement(doc.getDefaultRootElement(), StyleConstants.NameAttribute, HTML.Tag.TABLE);
Run Code Online (Sandbox Code Playgroud)
请注意,该元素似乎正确找到System.out.println(e.getName())"表"
现在
//inserting text at the end of the table
try {
doc.insertBeforeEnd(e, "<tr><td>A New Line</td></tr>");
} catch (BadLocationException ex) {
System.out.println(ex); …Run Code Online (Sandbox Code Playgroud) 我正在使用字符串构建器将文本附加到我的JTextPane,我已将内容类型设置为pane.setContentType("text/html");但我的JTextPane上实际上没有显示文本.
这是我追加的一个例子:
buildSomething.append("<b style=\"color:pink\">"+Birthday+"</span>");
Run Code Online (Sandbox Code Playgroud)
有什么我做错了吗?我该如何解决这个问题呢?
我需要显示链接,所以我正在使用带有setContentType的JTextPane.但是,内容不会换行,也没有滚动.JTextPane的内容将从RSS源返回.这是完整的代码:
import java.awt.*;
import javax.swing.*;
class Main extends JFrame
{
JFrame frame;
JTabbedPane tabbedPane;
JPanel home, news;
public Main()
{
setTitle("My Title" );
setSize( 900, 600 );
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home();
news();
tabbedPane = new JTabbedPane();
tabbedPane.addTab( " Home", home );
tabbedPane.addTab( "News", news );
JPanel framePanel = new JPanel();
framePanel.setLayout(new BorderLayout());
framePanel.add( tabbedPane, BorderLayout.CENTER );
getContentPane().add( framePanel );
}
public void home()
{
home = new JPanel();
// some stuffs here
}
public void news()
{
news = new JPanel(); …Run Code Online (Sandbox Code Playgroud) 请查看以下代码
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class Form extends JFrame
{
private JTextPane textPane;
private JLabel results;
private JPanel center,south;
private FlowLayout flow;
private ArrayList array;
private Color color;
private StyledDocument doc;
private Style style, fontSize;
public Form()
{
textPane = new JTextPane();
textPane.setMinimumSize(new Dimension(100,100));
doc = textPane.getStyledDocument();
doc.addDocumentListener(new TextActions());
results = new JLabel("Number of letters: ");
array …Run Code Online (Sandbox Code Playgroud) 只是试图为JTextPane中的文本着色 - 但问题是文本和下划线不能有不同的颜色.我该怎么办?或者甚至可能吗?下面的示例打印所有文本并以红色下划线.
JTextPane pane = new JTextPane();
StyleContext context = new StyleContext();
Style style = pane.addStyle("Black", null);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
StyleConstants.setForeground(style, Color.BLACK);
StyledDocument document = pane.getStyledDocument();
style = pane.addStyle("Red Underline", style);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setUnderline(style, true);
pane.getDocument().insertString(0, "Test String", style);
Run Code Online (Sandbox Code Playgroud) 我不理解JTextPane中的包装行为.如果我插入一个短文本,然后是一个JComponent然后再短文本我可以在一行中看到插入的东西,如果框架当然足够大.但是如果文本更长,那么它需要几行,组件总是放在一个新行中.
我已经认识到,在将一个组件插入JTextPane后,它的文本会变长一个字符.因此,如果一个组件被JTextPane视为一个字符,为什么它不像一个字符呢?可能它取决于java版本?我使用Java(TM)SE运行时环境(版本1.7.0-b147)
下面是我的代码(使用shortText/longText实例化变量currentText以重现上述行为):
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
String shortText = "one two three four five six seven";
String longText = "A text component that can be marked up with attributes that are represented graphically. You can find how-to information and examples of using text panes in Using Text Components, a …Run Code Online (Sandbox Code Playgroud) 我正在尝试用一个小的HTML-wysiwyg,JTextPane但是我无法BackgroundAction使用它.我使用setCharacterAttributes的StyledDocument的JTextPane,但它似乎有问题.视图还可以,但事实Document并非如此.
这是一个显示问题的小型演示代码.有2个JTextPane:
JTextPane并将其设置在第二个文本上- >虽然它们具有相同的文本,但它们不会显示相同的内容.
有没有办法在当前选定的文本上设置背景颜色并让JTextPane报告更新HTML文本?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class TestDifferentStyles {
private void initUI() {
JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextPane textPane = new JTextPane();
final JTextPane textPane2 = new JTextPane();
textPane2.setEditable(false);
textPane.setContentType("text/html");
textPane2.setContentType("text/html");
textPane.setText("<html><head></head><body><p>Hello world</p></body></html>");
SimpleAttributeSet set = new …Run Code Online (Sandbox Code Playgroud) 我正在尝试JOptionPane使用超链接调整长句的对话框()的高度.
我的代码是......
public class DialogTest {
public static void main(String[] args) throws Exception {
JTextPane jtp = new JTextPane();
Document doc = jtp.getDocument();
for (int i = 0; i < 50; i++) {
doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
if ((3 == i) || (7 == i) || (15 == i)) {
doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setUnderline(attrs, true);
StyleConstants.setForeground(attrs, Color.BLUE);
String text = "www.google.com";
URL url = …Run Code Online (Sandbox Code Playgroud) 我正在创建一个记录器,以显示输出作为更大的Java swing GUI的一部分.不幸的是,我添加它后经历了一个减速.我已经跟踪过这个问题了Document.insertString().
我做了一个测试,显示了这种放缓:
LogPanel.java
public class LogPanel extends JPanel{
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private JEditorPane textPane;
private static int numTextRows = 50;
private SimpleAttributeSet keyWord;
private Document document;
public LogPanel() {
super(new BorderLayout());
add(makePanel(), BorderLayout.CENTER);
}
private Component makePanel() {
// Just a text area that grows and can be scrolled.
textPane = new JTextPane();
document = textPane.getDocument();
keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.BLACK);
//textArea.setRows(numTextRows);
textPane.setEditable(false);
textPane.setFont(new Font("monospaced", Font.PLAIN, 12));
DefaultCaret caret …Run Code Online (Sandbox Code Playgroud) java ×10
jtextpane ×10
swing ×10
highlighting ×2
document ×1
dom ×1
font-size ×1
html ×1
insert ×1
jcomponent ×1
jeditorpane ×1
joptionpane ×1
resize ×1
xml ×1