Radim Burget
Radim Burget

Reputation: 1516

JTextPane - get component values

I have JTextPane, where are inserted 2 JLabels. If any of the labels are clicked, they change content from AAA to clicked.

This code iterates over the elements in the JTextPane:

        for(int i = 0; i < tp.getDocument().getLength(); i++) {
            System.out.println(((StyledDocument) tp.getDocument()).getCharacterElement(i));
        }

How can I access the labels to print "clicked" "AAAA" to the std out?

enter image description here

package texteditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.JButton;

public class JTextPaneExample extends JPanel {

    private JTextPane tp;

    public JTextPaneExample() {
        setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "Text Content", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        add(panel, BorderLayout.CENTER);
        panel.setLayout(new BorderLayout(0, 0));

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

        tp = new JTextPane();
        tp.setEditable(false);
        scrollPane.setViewportView(tp);

        JLabel lbl = new JLabel("AAAA ");
        lbl.setOpaque(true);
        lbl.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        lbl.addMouseListener(new LabelAdapter2(lbl));
        tp.insertComponent(lbl);

        lbl = new JLabel("BBBB ");
        lbl.setOpaque(true);
        lbl.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        lbl.addMouseListener(new LabelAdapter2(lbl));
        tp.insertComponent(lbl);

        JButton btnNewButton = new JButton("Write content");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                iterateOverContent(tp);
            }
        });
        panel.add(btnNewButton, BorderLayout.SOUTH);
    }

    private void iterateOverContent(JTextPane tp2) {
            for(int i = 0; i < tp.getDocument().getLength(); i++) {
                System.out.println(((StyledDocument) tp.getDocument()).getCharacterElement(i));
            }
    }

    private class LabelAdapter2 extends MouseAdapter {
        private JLabel lblNewLabel;

        public LabelAdapter2(JLabel lbl) {
            this.lblNewLabel = lbl;
        }


        public void mouseClicked(MouseEvent evt) {
            lblNewLabel.setText("clicked");
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("GoBoard");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new JTextPaneExample());
        frame.setPreferredSize(new Dimension(400, 400));
        frame.pack();
        frame.setVisible(true);
    }
}

Upvotes: 1

Views: 592

Answers (1)

Dave T.
Dave T.

Reputation: 158

i looked into your Question and found a Solution:

just replace your method to iterate the elements of the TextPane with this:

private void iterateOverContent(JTextPane tp2) {
        for(int i = 0; i < tp.getDocument().getLength(); i++) {
            Element elem = ((StyledDocument) tp.getDocument()).getCharacterElement(i);
            AttributeSet as = elem.getAttributes();
            if (as.containsAttribute(AbstractDocument.ElementNameAttribute, StyleConstants.ComponentElementName)) {
                if(StyleConstants.getComponent(as) instanceof JLabel) {
                    JLabel myLabel = (JLabel)StyleConstants.getComponent(as);
                    System.out.println(myLabel.getText());
                }
            }
            System.out.println(((StyledDocument) tp.getDocument()).getCharacterElement(i));
        }
}

as you can see, i first save the element into a new variable and then read out all the attributes (yes, the code could be alot shorter, but this way it is more clear - i hope :-) ) After that, we check if the attributes say, that this element is a component.

then the important part: we can get a Component from the attribute set via the StyleConstants.getComponent Method.

Finally just some sanity checks, to see if we can really typecast it to a JLabel.

Best regards, David

Upvotes: 2

Related Questions