Creating Dynamic Jtextpane component

Asked

Viewed 207 times

0

I had a problem with editing labels, that was solved by using the JTextPane.

So, I thought of creating my own component of this type, so that 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 label + separate content .

It would be the problem 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 this way:

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 "";
    }
}
  • Using a custom component has nothing to do with being "object-oriented".

1 answer

1


How you’re extending your Jtextpane component gives you access to his methods within the class. That way, you could use setText() within your format method but this is not necessary because when setting the doc it takes 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 use 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  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.