Results 1 to 1 of 1
Thread: autocomplete in text file
- 08-06-2010, 04:57 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
autocomplete in text file
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 for search in text with a txt. file.
Can you help me please.
Here is the code:
----------------------------------------------
Java 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(); } }); } }Last edited by Eranga; 08-06-2010 at 05:10 PM. Reason: code tags added
Similar Threads
-
textfield - printstream - text file - to much text
By keneid in forum New To JavaReplies: 2Last Post: 06-14-2010, 10:18 AM -
Insert text records from text file into a DB
By nicedad in forum JDBCReplies: 8Last Post: 11-06-2009, 06:52 AM -
AutoComplete for jtextfield
By pinks_70986 in forum New To JavaReplies: 2Last Post: 02-12-2009, 06:46 AM -
how to use live validation with autocomplete in dojo text boxes in <s:text box>
By subashm28 in forum Suggestions & FeedbackReplies: 2Last Post: 01-23-2009, 04:09 PM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks