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