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) {
    final JTextPane pane = new JTextPane();
    pane.setText("Some text");

    JButton buttonButton = new JButton("Insert label");
    buttonButton.addActionListener(e -> {
        JLabel label = new JLabel("label");
        label.setAlignmentY(0.85f);//from w  w w.j a v  a2  s .c o  m
        pane.insertComponent(label);

    });

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(buttonButton, BorderLayout.SOUTH);
    panel.add(pane, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextPane textPane = new JTextPane();
    textPane.setText("12345678\n\t1\t2\t3a");
    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(700, 100));

    setTabs(textPane, 8);//from  w w w  . ja v  a2s  .com

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JTextPane textPane = new JTextPane();
    textPane.setText("This is a test string");

    StyleConstants.setBold(BOLD, true);

    StyleConstants.setItalic(ITALIC, true);

    int start = 5;
    int end = 10;

    textPane.getStyledDocument().setCharacterAttributes(start, end - start, BOLD, false);
    textPane.getStyledDocument().setCharacterAttributes(start, end - start, ITALIC, false);
    for (int i = start; i < end; i++)
        System.out.println(//from w  ww  .j a  v a2s  .  c  o  m
                textPane.getStyledDocument().getCharacterElement(i).getAttributes().containsAttributes(BOLD)); // all now print true

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(textPane));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument doc = new DefaultStyledDocument();
    JTextPane textPane = new JTextPane(doc);
    textPane.setText("this is a test.");

    Random random = new Random();
    for (int i = 0; i < textPane.getDocument().getLength(); i++) {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set,
                new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        StyleConstants.setFontSize(set, random.nextInt(12) + 12);
        StyleConstants.setBold(set, random.nextBoolean());
        StyleConstants.setItalic(set, random.nextBoolean());
        StyleConstants.setUnderline(set, random.nextBoolean());

        doc.setCharacterAttributes(i, 1, set, true);
    }//  w  w w  .j  a  v a  2 s.co m

    frame.add(new JScrollPane(textPane));
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane ta = new JTextPane();
    ta.setContentType("text/html");
    ta.setText("<HTML><BODY><CODE> import java.io.*; <br> public class MyIO{}</CODE><br></BODY></HTML>");
    JScrollPane jsp = new JScrollPane(ta);
    f.getContentPane().add(jsp);//w  ww .  j  a v a 2s .co  m

    f.setSize(300, 200);
    f.setLocationRelativeTo(null);
    f.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.setText(HTMLTEXT);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(textPane1));
    f.setSize(320, 240);//from www  .j  av a2s  . co  m
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        sb.append("this is a test. ");
    }//from   w  w w. j a v  a 2 s.  com
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextPane newsTextPane = new JTextPane();
    newsTextPane.setContentType("text/html");
    newsTextPane.setEditable(false);
    newsTextPane.setText(sb.toString());

    JScrollPane scrollPane = new JScrollPane(newsTextPane);
    scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    frame.add(scrollPane);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane pane = new JTextPane();
    pane.setEditorKit(new CustomEditorKit());
    pane.setText("Underline With Different Color");

    StyledDocument doc = (StyledDocument) pane.getDocument();
    MutableAttributeSet attrs = new SimpleAttributeSet();
    attrs.addAttribute("Underline-Color", Color.red);
    doc.setCharacterAttributes(0, doc.getLength() - 1, attrs, true);

    JScrollPane sp = new JScrollPane(pane);
    frame.setContentPane(sp);//  ww  w.j av a 2s  . c  o  m
    frame.setPreferredSize(new Dimension(400, 300));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane editorPane = new JTextPane();
    editorPane.setSelectedTextColor(Color.red);

    // set content as html
    // editorPane.setContentType("text/html");
    editorPane.setText("<p color='#FF0000'>Cool!</p>");

    // added <u></u> to underlone button
    JButton label = new JButton("button");

    label.setAlignmentY(0.85f);/* w  w w .  ja va2  s  . c  om*/

    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {

            if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
                JOptionPane.showMessageDialog(null, "Hello!");
            }
        }
    });

    editorPane.insertComponent(label);
    frame.getContentPane().add(editorPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane textPane = new JTextPane();
    final JScrollPane scrollPane = new JScrollPane(textPane);

    String text = "Lorem ipsum dolor sit amet, " + "consectetur adipiscing elit."
            + "Fusce nec sapien id diam consequat adipiscing.";
    textPane.setText(text);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(scrollPane);

    frame.setSize(new Dimension(200, 200));
    frame.setVisible(true);/*from ww w .  j  a va 2 s .  c  o m*/

    FontMetrics metrics = textPane.getFontMetrics(textPane.getFont());
    textPane.setMargin(new Insets(scrollPane.getViewport().getHeight() - metrics.getHeight(), 0, 0, 0));
}