Highlighting Text - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Highlighting Text

saran185saran185 Posts: 23Member
Hi guys,
I need to highlight a word in text document when i keep cursor on that word. am doing it in java with netbeans ide. please help me by giving code. thanks.

Comments

  • krishbalodakrishbaloda Posts: 45Member
    u have opened that text document in JFrame or any where else???
  • saran185saran185 Posts: 23Member
    hi

    ya i ve used jframe to open the text file in a textarea. i ve attached the code .. can u help me fixing it. this code highlights a selected line, but not a string. I want to highlight a string when cursor is moved or clicked on it. [code]/*


    package javaapplication10;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.lang.*;
    import java.io.*;
    import java.io.FileReader;
    import java.io.IOException;
    /*
    * @author saranya
    */
    public class TextAreaSelectLine extends JFrame implements MouseListener
    {
    JTextArea textArea;
    Action selectLine;
    String s=new String();

    public TextAreaSelectLine()
    { try
    {

    File f;
    f=new File("c://program files/java/jdk1.6.0/bin/ss.txt");
    if(!f.exists()&& f.length()<0)
    System.out.println("The specified file is not exist");

    else{
    FileInputStream finp=new FileInputStream(f);
    byte b;
    do{
    b=(byte)finp.read();

    s=Byte.toString(b);
    // System.out.print((char)b);

    textArea = new JTextArea( s , 10, 30 );
    textArea.addMouseListener( this );

    JScrollPane scrollPane = new JScrollPane( textArea );
    getContentPane().add( scrollPane, BorderLayout.SOUTH );
    getContentPane().add( new JTextArea() );

    selectLine = getAction(DefaultEditorKit.selectLineAction);

    }
    while(b!=-1);
    finp.close();
    }
    }
    catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }

    private Action getAction(String name)
    {
    Action action = null;
    Action[] actions = textArea.getActions();

    for (int i = 0; i < actions.length; i++)
    {
    if (name.equals( actions[i].getValue(Action.NAME).toString() ) )
    {
    action = actions[i];
    break;
    }
    }

    return action;
    }
    public void mouseClicked(MouseEvent e)
    {

    if ( SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1)
    {
    selectLine.actionPerformed( null );



    }
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    TextAreaSelectLine frame = new TextAreaSelectLine();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible(true);
    }

    }
    [/code]
  • krishbalodakrishbaloda Posts: 45Member
    /*


    package javaapplication10;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.lang.*;
    import java.io.*;
    import java.io.FileReader;
    import java.io.IOException;
    /*
    * @author saranya
    */
    public class TextAreaSelectLine extends JFrame implements MouseListener
    {
    JTextArea textArea;
    Action selectLine;
    String s=new String();

    public TextAreaSelectLine()
    { try
    {

    File f;
    f=new File("c://program files/java/jdk1.6.0/bin/ss.txt");
    if(!f.exists()&& f.length()<0)
    System.out.println("The specified file is not exist");

    else{
    FileInputStream finp=new FileInputStream(f);
    byte b;
    do{
    b=(byte)finp.read();

    s=Byte.toString(b);
    // System.out.print((char)b);

    textArea = new JTextArea( s , 10, 30 );
    textArea.addMouseListener( this );

    JScrollPane scrollPane = new JScrollPane( textArea );
    getContentPane().add( scrollPane, BorderLayout.SOUTH );
    getContentPane().add( new JTextArea() );

    selectLine =

    ////change here//////////////////////////////////////////////////////

    getAction(DefaultEditorKit.selectWordAction);

    }
    while(b!=-1);
    finp.close();
    }
    }
    catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }

    private Action getAction(String name)
    {
    Action action = null;
    Action[] actions = textArea.getActions();

    for (int i = 0; i < actions.length; i++)
    {
    if (name.equals( actions[i].getValue(Action.NAME).toString() ) )
    {
    action = actions[i];
    break;
    }
    }

    return action;
    }
    public void mouseClicked(MouseEvent e)
    {

    if ( SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1)
    {
    selectLine.actionPerformed( null );



    }
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    TextAreaSelectLine frame = new TextAreaSelectLine();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setVisible(true);
    }

    }


    use "DefaultEditorKit.selectWordAction" in place of "DefaultEditorKit.selectLineAction".

  • saran185saran185 Posts: 23Member
    It worked ...:-) thank u..
Sign In or Register to comment.