setting JTextPane to content type HTML and using string builders

前端 未结 2 999
醉梦人生
醉梦人生 2020-12-03 19:06

I\'m using string builders to append text to my JTextPane, I\'ve set content type as pane.setContentType(\"text/html\"); but the no text actually appears on my

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 19:47

    @Joop Eggen

    1st. loop generate

    buildSomething.append("" + myBirthday + "");
    

    enter image description here

    2nd. loop generate same output, I think that doesn't matter if is wrapped inside .. or not because there is pane.setContentType("text/html");

    and (not correct code that I posted here ..)

    buildSomething1.append("" 
        + myBirthday + "");
    

    enter image description here

    import java.awt.*;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.html.HTMLDocument;
    
    public class MyTextPane implements Runnable {
    
        private JFrame frm;
        private JScrollPane jsp;
        private JTextPane jta;
        private StringBuilder buildSomething = new StringBuilder();
        private StringBuilder buildSomething1 = new StringBuilder();
        final String myBirthday = "Birthday";
    
        public MyTextPane() {
            for (int i = 0; i < 10; i++) {
                buildSomething.append("" + myBirthday + "");
                buildSomething1.append("" + myBirthday + "");
            }
            jta = new JTextPane();
            jta.setContentType("text/html");
            jta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            jta.setText(myBirthday);
            jsp = new JScrollPane(jta);
            jsp.setPreferredSize(new Dimension(250, 450));
            frm = new JFrame("awesome");
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setLayout(new BorderLayout());
            frm.add(jsp, BorderLayout.CENTER);
            frm.setLocation(100, 100);
            frm.pack();
            frm.setVisible(true);
            new Thread(this).start();
        }
    
        @Override
        public void run() {
            try {
                Thread.sleep(1500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    jta.setText(null);
                    HTMLDocument doc = (HTMLDocument) jta.getStyledDocument();
                    try {
                        doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), buildSomething.toString());
                    } catch (BadLocationException ex) {
                        Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            try {
                Thread.sleep(1500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    HTMLDocument doc = (HTMLDocument) jta.getStyledDocument();
                    try {
                        doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), buildSomething1.toString());
                    } catch (BadLocationException ex) {
                        Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MyTextPane fs = new MyTextPane();
                }
            });
        }
    }
    

提交回复
热议问题