|
Search in text file
Hello all, im trying to do a search in a text file. Can any tell me why its not working. thx. here is my code;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
public class Layout {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
private static final Component btnGO = null;
private static JCheckBox commonWords;
private static TextField txtSearch;
public static void addComponentsToPane(Container pane) throws FileNotFoundException {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation. RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
TextField txtSearch;
txtSearch = new TextField("Click GO to Search");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(txtSearch, c);
final JLabel lblMsg = new JLabel();
pane.add(lblMsg, c);
JButton btnGO = new JButton("GO");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
ActionListener actionPerformed = null;
btnGO.addActionListener (actionPerformed);
pane.add(btnGO, c);
commonWords = new JCheckBox("Common Words");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(commonWords, c);
// //button = new JButton("Long-Named Button 4");
final JTextArea textArea = new JTextArea("", 4, 30);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
// textArea.
final JScrollPane sbrText;
sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VER TICAL_SCROLLBAR_ALWAYS);
textArea.setAutoscrolls(true);
textArea.setLineWrap(true);
JScrollPane jScrollPane1;
jScrollPane1 = new JScrollPane(textArea);
textArea.setEditable(false);
// add(scrollPane, BorderLayout.CENTER);
pane.add(textArea, c);
pane.add(jScrollPane1, c);
String line;
sbrText.setVisible(true);
pane.add(sbrText, c);
Layout text; // this is the textarea you already have on your page
File theFile = new File ("Sentences.txt");
BufferedReader br = new BufferedReader (new FileReader ("Sentences.txt"));
try {
// String line;
while ((line = br.readLine()) != null) {
// final JTextArea textArea = null;
textArea.append (line);
}
} catch (IOException e) {
e.printStackTrace ();
JOptionPane.showConfirmDialog(null, "File not found.");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
* @throws FileNotFoundException
*/
private static void createAndShowGUI() throws FileNotFoundException {
//Create and set up the window.
JFrame frame = new JFrame("File Reader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
// addComponentsToPane(frame.sbrText.setVisible(true) );
//Display the window.
frame.setVisible(true);
//frame.sbrText.setVisible(true);
}
public void actionPerformed (ActionEvent event) {
System.out.println("yey clicked");
String s = txtSearch.getText();
if (s.length() <= 0) {
message("Nothing to search");
return;
}
String content = txtSearch.getText();
int index = content.indexOf(s, 0);
if (index >= 0) { // match found
try {
int end = index + s.length();
// hilit.addHighlight(index, end, painter);
txtSearch.setCaretPosition(end);
// entry.setBackground(entryBg);
message("'" + s + "' found.");
}
finally {
System.out.println("worked");
}
}
else {
// entry.setBackground(ERROR_COLOR);
message("'" + s + "' not found.");
}
}
void message(String msg) {
// lblMsg.setText(msg);
System.out.println(msg);
}
// DocumentListener methods
// public void search() {
// hilit.removeAllHighlights();
// class CancelAction extends AbstractAction {
// public void actionPerformed(ActionEvent ev) {
// hilit.removeAllHighlights();
// entry.setText("");
// entry.setBackground(entryBg); btnGO.addActionListener(this);
// }
// }
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
File file = new File("C:\\Documents and Settings\\Administrator.MYCOMPUTER\\My Documents\\School Documents\\IAD\\Assignments\\Java\\English Sentences\\Java Sentences.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
contents.append(text)
.append(System.getProperty(
"line.separator"));
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (reader != null)
{
reader.close();
}
} catch (IOException e)
{
e.printStackTrace();
JOptionPane.showConfirmDialog(null, "File not found.");
}
}
}
});
};
protected static void lblMsg(String string) {
// TODO Auto-generated method stub
}
public static JCheckBox getCommonWords() {
return commonWords;
}
public static void setCommonWords(JCheckBox commonWords) {
Layout.commonWords = commonWords;
}
public static TextField getTxtSearch() {
return txtSearch;
}
public static void setTxtSearch(TextField txtSearch) {
Layout.txtSearch = txtSearch;
}
public static boolean isShouldFill() {
return shouldFill;
}
public static boolean isShouldWeightX() {
return shouldWeightX;
}
public static boolean isRIGHT_TO_LEFT() {
return RIGHT_TO_LEFT;
}
public static Component getBtnGO() {
return btnGO;
}
}
|