Creating dynamic JTextPane component

0

I had a problem with editing labels , which was fixed using JTextPane .

So I thought of creating my own component of this type, so I can pass values "dynamically", and writing a few lines, making it more object - oriented. However, I believe that my logic is not very correct, because I could not change the component as in the answer that I based: Format separate label + content .

Would the problem be the way I tried to use the component method? :

 campo.setText(campo.formatar("Texto", "" + string, 0)); //aplicar o método do componente 

I tried to do it as follows:

import java.awt.Color;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class JTextPaneExample {

    private String string = "10";

    public void createAndShowGUI() {
        JFrame f = new JFrame("JTextPaneExample");

        MeuCampoJTextPane campo = new MeuCampoJTextPane();

        f.getContentPane().add(new JScrollPane(campo));
        campo.setText(campo.formatar("Texto", "" + string, 0)); //aplicar o método do componente  

        f.setSize(400, 300);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            new JTextPaneExample().createAndShowGUI();

        });
    }
}

class MeuCampoJTextPane extends JTextPane {

    // cria um StyleContext e um Document para o jtextpane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);

    // cria um estilo e adiciona atributos personalizados nele
    final Style redStyle = sc.addStyle("RED", null);
    final Style blueStyle = sc.addStyle("BLUE", null);

    public MeuCampoJTextPane() {

        this.setEditable(false);

    }

    public String formatar(String texto, String valor, int tipo) {
        String stringFormatada = "";

        try {
            switch (tipo) {
                case 0:
                    blueStyle.addAttribute(StyleConstants.Foreground, Color.blue);
                    blueStyle.addAttribute(StyleConstants.FontSize, 14);
                    blueStyle.addAttribute(StyleConstants.Bold, true);
                    doc.insertString(0, texto, blueStyle);

                    DecimalFormat formatoDecimal2 = new DecimalFormat("## 00");
                    String d2 = formatoDecimal2.format(Float.valueOf(valor));
                    redStyle.addAttribute(StyleConstants.Foreground, Color.red);
                    redStyle.addAttribute(StyleConstants.FontSize, 12);
                    doc.insertString(this.getText().length(), d2, redStyle);
                    stringFormatada = d2;
                    break;
                case 1:
                    break;
                default:
                    break;
            }
            return stringFormatada;
        } catch (Exception e) {

        }
        return "";
    }
}
    
asked by anonymous 06.08.2017 / 19:37

1 answer

1

As you are extending your JTextPane component this gives you access to its methods within the class. That way, you could use setText () inside your format method but this is not necessary because setting the doc will take care of changing the text.

class MeuCampoJTextPane extends JTextPane {

// cria um StyleContext e um Document para o jtextpane
private StyleContext sc = new StyleContext();
private final DefaultStyledDocument doc = new DefaultStyledDocument(sc);

// cria um estilo e adiciona atributos personalizados nele
private final Style redStyle = sc.addStyle("RED", null);
private final Style blueStyle = sc.addStyle("BLUE", null);

public MeuCampoJTextPane() {
    redStyle.addAttribute(StyleConstants.Foreground, Color.red);
    redStyle.addAttribute(StyleConstants.FontSize, 12);
    blueStyle.addAttribute(StyleConstants.Foreground, Color.blue);
    blueStyle.addAttribute(StyleConstants.FontSize, 14);
    blueStyle.addAttribute(StyleConstants.Bold, true);
}

public void formatar(String texto, String valor, int tipo) {
    setStyledDocument(doc);
    setEditable(false);

    try {
        switch (tipo) {
            case 0:
                DecimalFormat formatoDecimal2 = new DecimalFormat("## 00");
                String d2 = formatoDecimal2.format(Float.valueOf(valor));
                doc.insertString(0, texto, blueStyle);
                doc.insertString(getText().length(), d2, redStyle);
                break;
            case 1:
                break;
            default:
                break;
        }
    } catch (Exception e) {

    }
}

That way the usage would look like this:

MeuCampoJTextPane campo = new MeuCampoJTextPane();
f.getContentPane().add(new JScrollPane(campo));
campo.formatar("Texto", "" + string, 0); //aplicar o método do componente  
    
06.08.2017 / 20:18