0

I am new to Swing development and I am creating a Java application where a user can load text documents and tag parts of text.

The tagged parts of text would then be highlighted and the user can click on the tagged text to edit/remove the tag. Is there any library/class that I can use to achieve this.

I have already looked at JTextArea. But to best of my knowledge it can only display plain text (correct me if I am wrong)

To be more clear of what I am looking for:

When you tag a question on Stack Overflow (SO), the tags are highlighted with a little box and highlighting around the tag. I am looking something similar in Java.

So, if the component that I add is say X, then X should be able to hold plain text as well as tagged text. The tagged text should look like the SO tags.

Edit

An example for clarification.

Original Text:

This is some sample text.

After tagging, say sample, this is what it should look like:

With Tagged Text

enter image description here

That x is basically a button to remove all tags associated with sample.

Community
  • 1
  • 1
Ankit
  • 6,772
  • 11
  • 48
  • 84

3 Answers3

3

this can help you jeditorpane and jtextpane

mulax
  • 219
  • 3
  • 9
3

There's a working example here that sets color using StyledEditorKit.ForegroundAction. It also illustrates related classes in StyledEditorKit.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

My idea is, take a textfield,when we double clik on it set to editable then write tag and hit enter.. then update the tag, for highlighting use setBackground,setForeground with textfield

JTextField jt=new JTextField("tag");

set it as

jt.setEditabe(false);

add MouseListener to jt see this LINK mouse listener

jt.addMouseListener(this);

write the code in mouseClicked event,if we double click it will set as editable state

public void mouseClicked(MouseEvent e) {
    int count=0;
    count=e.getClickCount();
    if(count==2)
    {
        jt.setEditable(true);
        count=0;

    }

}

then edit the tag...do rest of the code,i.e update TAG by hitting enter using key binding see this LINK

padman
  • 491
  • 1
  • 3
  • 14
  • Thanks but I am not concerned about the event handlers. I need some component that I can use for a complete document, for which `JTextField` is not a good option and also I do not need to tag the whole field/document, just parts of it (like the way you highlight in **Word** or **Acrobat Reader** – Ankit Jun 30 '12 at 05:01