title: find and replace dialog box
body: <p>I'm trying to develop a Find and Replace like in Notepad. Here is my code so far for find. I am wondering how I can keep the dialog box open after pressing the button, so that I can use the dialog box for the next find.</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.event.*;
  import java.io.FileInputStream;
  import java.io.FileReader;
  import java.util.Scanner;
  
  import javax.swing.*;
  
  class TextAreaEx extends JFrame implements ActionListener, KeyListener {
  
      JButton button1;
      JTextArea tx = new JTextArea();
      int startFrom = 0;
      int offset = 0;
      String find = "";
      String text = "";
  
      TextAreaEx() {
          super("My Frame");
          FileInputStream fis = null;
          StringBuffer sb = new StringBuffer();
          try {
              Scanner scan = new Scanner(new FileReader("C:\\Users\\Sam\\Desktop\\networktools.txt"));
              while (scan.hasNext()) // while there's still something to read
              {
                  tx.append(scan.nextLine() + "\n"); // append
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          // text = sb.toString();
          text = tx.getText();
          text = text.toLowerCase();
          button1 = new JButton("Find");
          button1.addActionListener(this);
          getContentPane().add(button1, BorderLayout.PAGE_START);
          button1.setFocusable(false);
          JScrollPane p1 = new JScrollPane(tx);
          getContentPane().add(p1);
          JFrame.setDefaultLookAndFeelDecorated(true);
          tx.addKeyListener(this);
          setSize(400, 400);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocation(400, 300);
          setVisible(true);
      }
  
      public static void main(String s[]) {
          new TextAreaEx();
      }
  
      public void actionPerformed(ActionEvent e) {
          startFrom = 0;
          offset = 0;
          if (e.getSource() == button1) {
  
              find = (String) JOptionPane.showInputDialog(this, "FIND:\n", "Find", JOptionPane.INFORMATION_MESSAGE, null, null, null);
              find = find.toLowerCase();
              findWord();
              this.setVisible(true);
          }
      }
  
      public void findWord() {
          offset = text.indexOf(find, startFrom);
          if (offset &gt; -1) {
              tx.setFocusable(true);
              tx.select(offset, find.length() + offset);
              startFrom = find.length() + offset + 1;
          } else {
              JOptionPane.showMessageDialog(this, "No (more) matches");
          }
      }
  
      public void keyPressed(KeyEvent ke) {
          if (ke.getKeyCode() == KeyEvent.VK_F3) {
              findWord();
          }
      }
  
      public void keyReleased(KeyEvent ke) {
      }
  
      public void keyTyped(KeyEvent ke) {
      }
  }
  </code></pre>
  
  <p>Thanks for the help.</p>