How can I highlight some text in an inactive textfield?

Sasivarnan N.A

Here is a sample for you.


import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;

public class TpHighlight extends JFrame {
    String tf_str="Hello this is a demo for High Lighting";
    JTextField tef;
    DefaultHighlighter high = new DefaultHighlighter();
    DefaultHighlighter.DefaultHighlightPainter highlight_painter =
        new DefaultHighlighter.DefaultHighlightPainter(
             new Color(198,198,250));
    SimpleAttributeSet set=new  SimpleAttributeSet();

    public TpHighlight () {
        tef=new JTextField(12);
        getContentPane().add(tef,BorderLayout.NORTH);
        getContentPane().add(new JLabel(
            "Hello,this is a simple JTextPane Demo!"),
                             BorderLayout.CENTER);
        tef.setText(tf_str);
        tef.setEditable(false);
        tef.setHighlighter(high);
        try{
            high.addHighlight(6,10,highlight_painter);
            high.addHighlight(16,20,highlight_painter);
            high.addHighlight(25,29,highlight_painter);
        }
        catch(Exception ignoreMe) {}

        setSize(250,120);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TpHighlight();
    }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact