Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Highlighting Text Posted by saran185 on 28 Jan 2011 at 12:29 AM
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.
Report
Re: Highlighting Text Posted by krishbaloda on 28 Jan 2011 at 3:02 AM
u have opened that text document in JFrame or any where else???
Report
Re: Highlighting Text Posted by saran185 on 2 Feb 2011 at 1:47 AM
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.
/*
 

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);
    }

}

Report
Re: Highlighting Text Posted by krishbaloda on 2 Feb 2011 at 2:21 AM
/*


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".

Report
Re: Highlighting Text Posted by saran185 on 2 Feb 2011 at 3:21 AM
It worked ... thank u..



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.