import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Enumeration; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Element; import javax.swing.text.MutableAttributeSet; import javax.swing.text.PlainDocument; import javax.swing.text.SimpleAttributeSet; public class Test { public Test() { JFrame frame = new JFrame(); final JTextArea area = new JTextArea(); final PlainDocument pd = new PlainDocument(); MutableAttributeSet mats = new SimpleAttributeSet(); mats.addAttribute("Color", "some value"); mats.addAttribute("Color", "foo"); mats.addAttribute("HY", "hello thar"); try { pd.insertString(0, "HELLO WORLD\n", mats); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } area.setDocument(pd); area.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int offset = area.viewToModel(e.getPoint()); Element line = pd.getDefaultRootElement().getElement(pd.getDefaultRootElement().getElementIndex(offset)); AttributeSet atts = line.getAttributes(); System.out.println(line.getElementCount()); System.out.println("Att " + atts.getAttribute("Color") ); for(Enumeration e1 = atts.getAttributeNames(); e1.hasMoreElements();) { System.out.println(e1 + " " + atts); } System.out.println("Done"); } }); frame.setContentPane(area); frame.setSize(800,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new Test(); } }