tags:

views:

101

answers:

1

I'm newbie and am trying to figure out find and replace in notepad. Here is the code for find. I am wondering how can i implement a way so that after pressing the button the dialog box doesn't close and so i can use the dialog box for next find ( like find next in notepad).

            import java.awt.BorderLayout;
            import java.awt.event.*;
            import java.io.BufferedReader;
            import java.io.File;
            import java.io.FileInputStream;
            import java.io.FileReader;
            import java.io.InputStreamReader;
            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 > -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){}
            }

Thanx for the help.

+3  A: 

JOptionPane is meant to be used just to aks a simple question for the user to answer with yes/no, ok/cancel or ask the for simple input and than be closed. "Real" find/replace dialog boxes need to do a lot more than this, don't try to use JOptionPane for this, it was not designed for that purpose. You will need complete new dialog, (JFrame in you case) that can accomplish what you need, and you will be able to extend it to specify other options like "Match Case", use regular expressions and so on.

Notice: Aside from this your code needs A LOT of other improvements. Start with not using magic numbers and strings hard coded, name your variables appropriately ( and not button1 ) , stick up to the coding and formatting conventions in the language that you are using. Try to use smaller classes and methods that do just one thing - "A class should have one, and only one, reason to change". Try to extract to search functionality itself in its own class and write unit tests for it. After time you could try to look at the some open source Java projects (JEdit is good example for editor) just to see how professional code looks like. Good luck.

devdimi