Example usage for javax.swing JTextPane setText

List of usage examples for javax.swing JTextPane setText

Introduction

In this page you can find the example usage for javax.swing JTextPane setText.

Prototype

@BeanProperty(bound = false, description = "the text of this component")
public void setText(String t) 

Source Link

Document

Sets the text of this TextComponent to the specified content, which is expected to be in the format of the content type of this editor.

Usage

From source file:Main.java

public static void main(String args[]) throws BadLocationException {
    JFrame jf = new JFrame("StyledText");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = jf.getContentPane();

    JTextPane pane = new JTextPane();
    SimpleAttributeSet set = new SimpleAttributeSet();
    StyleConstants.setBold(set, true);

    // Set the attributes before adding text
    pane.setCharacterAttributes(set, true);
    pane.setText("java2s.com ");

    set = new SimpleAttributeSet();
    StyleConstants.setItalic(set, true);
    StyleConstants.setForeground(set, Color.red);
    StyleConstants.setBackground(set, Color.blue);

    Document doc = pane.getStyledDocument();
    doc.insertString(doc.getLength(), "Swing ", set);

    set = new SimpleAttributeSet();
    StyleConstants.setFontSize(set, 24);

    doc.insertString(doc.getLength(), "Tutorial", set);

    JScrollPane scrollPane = new JScrollPane(pane);
    cp.add(scrollPane, BorderLayout.CENTER);

    jf.setSize(400, 300);//  w  w  w.ja  va  2  s .c o  m
    jf.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200, 200);//from  www  .  j  av  a2  s .c  om
    f.getContentPane().setLayout(new BorderLayout());
    JTextPane jtp = new JTextPane();
    jtp.setEditorKit(new WrapEditorKit());
    JScrollPane jsp = new JScrollPane(jtp);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    f.getContentPane().add(jsp, BorderLayout.CENTER);
    jtp.setText("thisIsATestThisisAtestthisIsATestThisisAtestthisIsATestThisisAtest");
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextPane text = new JTextPane() {
        @Override/*from  w w  w. j av a2s .c  o  m*/
        public String getToolTipText() {
            return ((JComponent) getParent()).getToolTipText();
        }

        @Override
        public String getToolTipText(MouseEvent event) {
            return ((JComponent) getParent()).getToolTipText(event);
        }
    };
    text.setText("Lorem ipsum dolor sit");
    ToolTipManager.sharedInstance().registerComponent(text);

    JFrame frame = new JFrame("Testing");
    JPanel panel = new JPanel(new BorderLayout());
    panel.setToolTipText("tooltip from parent");
    frame.setContentPane(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(text);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();
    JTextField tf = new JTextField("is");
    String word = "";
    Highlighter highlighter = new UnderlineHighlighter(null);

    textPane.setHighlighter(highlighter);
    textPane.setText("This is a test");
    panel.setLayout(new BorderLayout());
    panel.add(new JLabel("Enter word, then press ENTER key: "), "West");
    panel.add(tf, "Center");

    final WordSearcher searcher = new WordSearcher(textPane);
    tf.addActionListener(e -> {//from w  w w. j  av  a 2  s. co m
        String w = tf.getText().trim();
        int offset = searcher.search(w);
        if (offset == -1) {
            return;
        }
        try {
            textPane.scrollRectToVisible(textPane.modelToView(offset));
        } catch (BadLocationException ex) {
        }

    });
    textPane.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void removeUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void changedUpdate(DocumentEvent evt) {
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(panel, "South");
    f.add(new JScrollPane(textPane), "Center");
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[1];
    tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
    TabSet tabset = new TabSet(tabs);

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
    pane.setParagraphAttributes(aset, false);
    pane.setText("\tright\tleft\tcenter\tdecimal\n" + "\t1\t1\t1\t1.0\n"
            + "\t200.002\t200.002\t200.002\t200.002\n" + "\t.33\t.33\t.33\t.33\n");

    JFrame frame = new JFrame("TabExample");
    frame.setContentPane(new JScrollPane(pane));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(360, 120);/*from  w w w.j a  v a  2s  .c om*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[2];
    tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
    tabs[1] = new TabStop(100, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
    TabSet tabset = new TabSet(tabs);

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
    pane.setParagraphAttributes(aset, false);
    pane.setText("\tright\tleft\tcenter\tValue\n" + "\t200.002\t200.002\t200.002\t200.002\n");

    JFrame d = new JFrame();
    d.setContentPane(new JScrollPane(pane));
    d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    d.setSize(360, 120);/* w ww.  ja v  a 2  s.c o  m*/
    d.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String HTMLTEXT = "<html><head><style>.foot{color:red} .head{color:black}</style></head>"
            + "<span style='font-family:consolas'>java2s.com</span><br/>"
            + "<span style='font-family:tahoma'>java2s.com</span>";
    JTextPane textPane1 = new JTextPane();

    textPane1.setContentType("text/html");
    textPane1.setFont(new Font("courier new", Font.PLAIN, 32));
    textPane1.setDocument(new HTMLDocument() {
        @Override/*w w w . j a va2  s. com*/
        public Font getFont(AttributeSet attr) {
            StyleContext styles = (StyleContext) getAttributeContext();
            Font f = styles.getFont(attr);
            String ff = f.getFamily();
            System.out.println(ff);
            return textPane1.getFont();
        }
    });
    textPane1.setText(HTMLTEXT);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(textPane1));
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:TabExample.java

public static void main(String[] args) {

    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[4];
    tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
    tabs[1] = new TabStop(100, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
    tabs[2] = new TabStop(200, TabStop.ALIGN_CENTER, TabStop.LEAD_NONE);
    tabs[3] = new TabStop(300, TabStop.ALIGN_DECIMAL, TabStop.LEAD_NONE);
    TabSet tabset = new TabSet(tabs);

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
    pane.setParagraphAttributes(aset, false);
    pane.setText("\tright\tleft\tcenter\tdecimal\n" + "\t1\t1\t1\t1.0\n"
            + "\t200.002\t200.002\t200.002\t200.002\n" + "\t.33\t.33\t.33\t.33\n");

    JFrame frame = new JFrame("TabExample");
    frame.setContentPane(new JScrollPane(pane));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(360, 120);/*  www  . j ava  2s.c o  m*/
    frame.setVisible(true);
}

From source file:Main.java

private static Component blackJTextPane() {
    JTextPane pane = new JTextPane();
    pane.setBackground(Color.BLACK);
    pane.setForeground(Color.WHITE);
    pane.setText("Here is example text");
    return pane;//from   w w w.  ja  v a  2  s .  c o  m
}

From source file:Main.java

/**
 * Creates a new <code>JTextPane</code> object with the given properties.
 *
 * @param text The text which will appear in the text pane
 * @param backgroundColor The background color
 * @return A <code>JTextPane</code> object
 *///from  w  w w. j  a  va  2 s  .  c o  m
public static JTextPane createJTextPane(String text, Color backgroundColor) {
    JTextPane jTextPane = new JTextPane();
    jTextPane.setBorder(null);
    jTextPane.setEditable(false);
    jTextPane.setBackground(backgroundColor);
    jTextPane.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    if (text != null) {
        jTextPane.setText(text);
    }
    jTextPane.setVerifyInputWhenFocusTarget(false);
    jTextPane.setAutoscrolls(false);
    return jTextPane;
}