hi ...
i ve written a code that would highlight a word in text file when i keep my cursor on that word. I ve another code that uses a file chooser to select a text file and open it in the text area. now i want to integrate both the code. i.e.. I need to choose a txt file from the system and open it the text area. Then when i mouse click on any word in that text file ( that is opened in the text area)that word should get highlighted..
below is my code. i get some errors wherever the word "action " is mentioned..is nay line missing or if anything is to be imported?? Im not sure what is to be included..Im running java code using netbeans ide..The orange parts of the code are the errors..
So pls help me in fixing the errors.
package jfilechooserdemo.resources;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
class MyCustomFilter extends javax.swing.filechooser.FileFilter {
@Override
public boolean accept(File file) {
// Allow only directories, or files with ".txt" extension
return file.isDirectory() || file.getAbsolutePath().endsWith(".txt");
}
@Override
public String getDescription() {
// This description will be displayed in the dialog,
// hard-coded = ugly, should be done via I18N
return "Text documents (*.txt)";
}
}
public class JFileChooserDemo extends javax.swing.JFrame implements MouseListener {
/** Creates new form JFileChooserDemo */
public JFileChooserDemo() {
initComponents();
}
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
String filePath;
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
filePath = file.getAbsolutePath();
try {
// What to do with the file, e.g. display it in a TextArea
Action selectWord;
textarea.read( new FileReader( file.getAbsolutePath() ), null );
textarea.addMouseListener( this );
String s=new String();
selectWord = getAction(DefaultEditorKit.selectWordAction);
countLetter(filePath);
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}
private void countLetter(String filePath){
Scanner in = null;
try{
in = new Scanner(new File(filePath));
}catch(Exception e){
System.out.print("Not got");
}
int nbWords = 0;
while(in.hasNext()) {
String a = in.next();
nbWords++;
}
String ss = ""+nbWords;
jLabel4.setText("the number of words are: "+ss);
System.out.println(nbWords);
}
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)
{
selectWord.actionPerformed( null );
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
private void wordcountActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFileChooserDemo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem Open;
private javax.swing.JFileChooser fileChooser;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JPopupMenu jPopupMenu2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JTextPane jTextPane2;
private javax.swing.JTextArea textarea;
private javax.swing.JMenu wordcount;