Does anyone have any ideas on how I can search a text file and list the results in a JComponent, like a JPanel.
I've been trying to make this work out for two days now, but no success will really appreciate a reply. Thanks a lot in advance.
I've been trying to write a class that handles search queries to a text file. My main goal is to get the lines in a text file that contain the search keywords entered in a JTextField and print them out in an appropriate JComponent(something like a JTextField, JTextPane, whichever best applicable).
I'd like the search results to show in columns like how google search results get displayed, so that each line from the text file gets printed in its own line. I've been told that it's best to use an ArrayList. I really don't know how to do this. I've picked up ideas from all over and this is what I have so far:
Much appreciation in advance. I am very new to Java. I've been at it the whole day trying to get this right and have not gone any far. Am willing to try anything offered, even a new approach.
// The class that handles the search query
// Notice that I've commented out some parts that show errors
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextPane;
public class Search {
public static String path;
public static String qri;
public Search(String dTestFileDAT, String qry) {
path = dTestFileDAT;
qri = qry;
}
public static JTextPane resultJTextPane;
public static List<String> linesToPresent = new ArrayList<String>();
public static List<String> searchFile(String path, String match){
File f = new File(path);
FileReader fr;
try {
fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line;
do{
line = br.readLine();
Pattern p = Pattern.compile(match);
Matcher m = p.matcher(line);
if(m.find())
linesToPresent.add(line);
} while(line != null);
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// resultJTextPane = new JTextPane();
// resultJTextPane = (JTextPane) Home.BulletinsJPanel.add(linesToPresent);
return linesToPresent;
}
}
// This handles the click event to take the query. Notice that I've commented out some parts that show errors
private void mouseClickedSearch(java.awt.event.MouseEvent evt) {
Search fs = new Search("/D:/TestFile.dat/", "Text to search for");
// searchResultsJPanel.add(Search.searchFile("/D:/TestFile.dat/", "COLE"));
// searchResultsJTextField.add(fs);
}
There are a number of possible solutions, this is just a simple one (no seriously, it is ;))
Basically, this just uses a
JList
to store all the matches of the search text from the search file.This is a case sensitive search, so beware
You could also take another tact and simply highlight the matches...
This uses a slightly different approach as this is interactive. Basically you simply type, wait a 1/4 second and it will start searching...
Try this: