3 Replies Latest reply on Aug 7, 2010 8:57 AM by 699554

    i need autocomplete  for search for words in a txt. file

    843789
      i am not so good in java.
      I have a running code for search in text with a txt. file (from user bluefox815).

      But I need a solution with autocomplete for search for words in a txt. file.

      test_file.txt (Teil des Inhaltes):

      Roboter robots
      Mechatronik mechatronics
      and so on


      Can you help me please.

      Here is the code:
      ----------------------------------------------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      
      /*
      * this program searches for a string in a text file and
      * says which line it found the string on
      */
      
      public class SearchText implements ActionListener {
      
      private String filename = "test_file.txt";
      
      private JFrame frame;
      private JTextField searchField;
      private JButton searchButton;
      private JLabel lineLabel;
      private String searchFor;
      private BufferedReader in;
      
      public SearchText() {
      frame = new JFrame("SearchText");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      searchField = new JTextField(80);
      searchButton = new JButton("Search");
      // this is used later in our actionPerformed method
      searchButton.setActionCommand("search");
      // this sets the action listener for searchButton, which is the current class
      // because this class implements ActionListener
      searchButton.addActionListener(this);
      lineLabel = new JLabel("nach dem Fachbegriff suchen");
      }
      
      public void createGUI() {
      JPanel topPanel = new JPanel();
      topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
      JPanel bottomPanel = new JPanel();
      JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
      topPanel.add(searchField);
      topPanel.add(searchButton);
      bottomPanel.add(lineLabel);
      mainPanel.add(topPanel);
      mainPanel.add(bottomPanel);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setVisible(true);
      }
      
      public void actionPerformed(ActionEvent e) {
      // now we get the action command and if it is search, then it is the button
      if ("search".equals(e.getActionCommand())) {
      searchFor = searchField.getText();
      searchTheText();
      }
      }
      
      private void searchTheText() {
      // I initialize the buffered reader here so that every time the user searches
      // then the reader will start at the beginning, instead of where it left off last time
      try {
      in = new BufferedReader(new FileReader(new File(filename)));
      } catch (IOException e) {
      }
      String lineContent = null;
      int currentLine = 0;
      // this will be set to true if the string was found
      boolean foundString = false;
      while (true) {
      currentLine++;
      // get a line of text from the file
      try {
      lineContent = in.readLine();
      } catch (IOException e) {
      break;
      }
      // checks to see if the file ended (in.readLine() returns null if the end is reached)
      if (lineContent == null) {
      break;
      }
      if (lineContent.indexOf(searchFor) == -1) {
      continue;
      } else {
      lineLabel.setText(String.valueOf(lineContent));
      foundString = true;
      break;
      }
      }
      if (!foundString)
      lineLabel.setText("Es kann kein Fachbegriff gefunden werden.");
      try {
      in.close();
      } catch (IOException ioe) {
      }
      }
      
      public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
      public void run() {
      new SearchText().createGUI();
      }
      });
      }
      
      }