import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class AllWordsContainingTheSubstring2 extends JFrame { private static final long serialVersionUID = 1L; public static void main(String[] args) { // Construct an instance of this class and show it AllWordsContainingTheSubstring2 aWindow = new AllWordsContainingTheSubstring2(); aWindow.setVisible(true); } private JTextField substring = new JTextField(); private JTextArea wordsWithSubstring = new JTextArea(); private JTextArea wordsBeginingWithSubstring = new JTextArea(); private ArrayList allWords; public AllWordsContainingTheSubstring2() { // read all words into the list of words getAllWords(); // Lay out the GUI, initialize the instance variable setSize(350, 400); setLocation(50, 100); setTitle("Show words"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cp = getContentPane(); JPanel topPanel = new JPanel(); topPanel.setLayout(new GridLayout(2, 1)); topPanel.add(substring); topPanel.add(new JLabel("Enter part or all of a word above ")); cp.add(topPanel, BorderLayout.NORTH); // Set up two textFields JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new GridLayout(1, 2)); wordsBeginingWithSubstring.setEditable(false); wordsWithSubstring.setEditable(false); bottomPanel.add(wordsBeginingWithSubstring); bottomPanel.add(wordsWithSubstring); cp.add(bottomPanel, BorderLayout.CENTER); wordsWithSubstring.setEditable(false); substring.addActionListener(new TextFieldListener()); } private void getAllWords() { allWords = new ArrayList(); Scanner s = null; try { s = new Scanner(new File("words.txt")); } catch (FileNotFoundException e) { System.out.println("words.txt not found"); } while (s.hasNextLine()) { allWords.add(s.next()); } } private class TextFieldListener implements ActionListener { public void actionPerformed(ActionEvent ae) { String search = substring.getText().toLowerCase(); search = search.trim(); // Don't look for empty strings or blank entries if (search.length() == 0) return; wordsWithSubstring.setText(""); wordsWithSubstring.append("Contains '" + search + "'\n"); wordsWithSubstring.append("===================\n"); wordsBeginingWithSubstring.setText(""); wordsBeginingWithSubstring.append("Begins with '" + search + "'\n"); wordsBeginingWithSubstring.append("===================\n"); int count = 0; for (int i = 0; i < allWords.size(); i++) { String nextWord = allWords.get(i); int index = nextWord.indexOf(search); if (index == 0) { wordsBeginingWithSubstring.append(nextWord + "\n"); count++; } if (index > 0) { wordsWithSubstring.append(nextWord + "\n"); count++; } } if (count == 0) JOptionPane.showMessageDialog(null, "'" + search + "'" + " not found in any of the " + allWords.size() + " words"); } } }