public void search() {
    hilit.removeAllHighlights();

    String s = entry.getText();
    if (s.length() <= 0) {
      message("Nothing to search");
      return;
    }

    String content = textArea.getText();
    int index = content.indexOf(s, 0);
    if (index >= 0) {
      try {
        int end = index + s.length();
        hilit.addHighlight(index, end, painter);
        textArea.setCaretPosition(end);
        entry.setBackground(entryBg);
        message("'" + s + "' found. Press ESC to end search");
      } catch (BadLocationException e) {
        e.printStackTrace();
      }
    } else {
      entry.setBackground(ERROR_COLOR);
      message("'" + s + "' found. Press ESC to start a new search");
    }
  }
  public TextFieldDemo() {
    initComponents();

    InputStream in = getClass().getResourceAsStream("content.txt");
    try {
      textArea.read(new InputStreamReader(in), null);
    } catch (IOException e) {
      e.printStackTrace();
    }

    hilit = new DefaultHighlighter();
    painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
    textArea.setHighlighter(hilit);

    entryBg = entry.getBackground();
    entry.getDocument().addDocumentListener(this);

    InputMap im = entry.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = entry.getActionMap();
    im.put(KeyStroke.getKeyStroke("ESCAPE"), CANCEL_ACTION);
    am.put(CANCEL_ACTION, new CancelAction());
  }