Formatting Text in a JTextPane

3

Hello, I have a JTextPane and would like to format parts of your content. Is it possible to do this?

JTextPane areaDeTexto = new JTextPane();
areaDeTexto.setText = "Este texto está formatado em negrito.\nEste em itálico.\nEste em sublinhado.\nE este não está formatado!";

What do I do in this code?

    
asked by anonymous 27.09.2015 / 18:12

2 answers

3

Try to use the html, put the texts in the tags <b></b> , <i></i> and <u></u> , but remember to give setContentType ("text / html") Try using the code below

public class Formatar {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
            frame.setSize(300, 300);
            frame.setVisible(true);
        JTextPane tp = new JTextPane();
            frame.getContentPane().add(BorderLayout.CENTER,tp);
            tp.setContentType("text/html");
            tp.setText("<html><b>Texto em Negrito, </b><i>Texto em Itálico, </i><u>Texto Sublinhado</u></html>");
    }
}
    
27.09.2015 / 19:49
1

Friend's response Gui_Biem is very good !!
But I'll leave mine too.
 Assuming both classes are in the same package:

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class EstiloTextPanel extends JTextPane {

    private Style negrito;
    private Style italico;
    private Style normal;

    public EstiloTextPanel(StyledDocument doc) {
        super(doc);
        initStyles();
    }

    public EstiloTextPanel() {
        super();
        initStyles();
    }

    private void initStyles() {
        normal = StyleContext.getDefaultStyleContext().getStyle(
                StyleContext.DEFAULT_STYLE);

        negrito = getStyledDocument().addStyle("bold", normal);
        StyleConstants.setBold(negrito, true);

        italico = getStyledDocument().addStyle("italic", normal);
        StyleConstants.setItalic(italico, true);
    }


    public void insertBoldText(String text) throws BadLocationException {
        getStyledDocument().insertString(getStyledDocument().getLength(), text,
                negrito);
    }

    public void insertItalicText(String text) throws BadLocationException {
        getStyledDocument().insertString(getStyledDocument().getLength(), text,
                italico);
    }

    public void insertNormalText(String text) throws BadLocationException {
        getStyledDocument().insertString(getStyledDocument().getLength(), text,
                normal);
    }

}

and also:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.text.BadLocationException;

public class TestandoEstiloTextPanel extends JFrame {

    private EstiloTextPanel verPanel;

    public TestandoEstiloTextPanel() throws BadLocationException {
        super("FORMATADO");

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 300);
        setLocationRelativeTo(null);


        verPanel = new EstiloTextPanel();
        add(verPanel, BorderLayout.CENTER);
        verPanel.setEditable(false);

        verPanel.insertBoldText("Negrito\n");
        verPanel.insertItalicText("Itálico\n");
        verPanel.insertNormalText("Normal\n");


        setVisible(true);
    }

    public static void main(String[] args) throws BadLocationException {
        new TestandoEstiloTextPanel();
    }

}
    
27.09.2015 / 20:25