#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Java search problem


    I have to do a word search app.

    I made the gui and all the codes I just do not know how to link the search button to search from the textarea.

    The code is working:

    Java Code:

    Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.BoxLayout;
    import java.awt.FlowLayout;
    import javax.swing.JLabel;
    import java.awt.Font;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import java.awt.Scrollbar;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.TextField;
    import java.awt.Button;
    import java.awt.Label;
    import javax.swing.JTextField;
    
    
    public class frame1 extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField found_field;
    	private JTextField search_field;
    
    	
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					frame1 frame = new frame1();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public frame1() {
    		setTitle("Word Search");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 571, 401);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		found_field = new JTextField();
    		found_field.setBounds(305, 319, 39, 20);
    		contentPane.add(found_field);
    		found_field.setColumns(10);
    		
    		JLabel lblNewLabel = new JLabel("Enter or Copy & Paste Text");
    		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
    		lblNewLabel.setBounds(142, 29, 164, 14);
    		contentPane.add(lblNewLabel);
    		
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setBounds(23, 82, 509, 216);
    		contentPane.add(scrollPane);
    		
    		final JTextArea text_field = new JTextArea();
    		scrollPane.setViewportView(text_field);
    		text_field.setLineWrap(true);
    		text_field.setWrapStyleWord(true);
    		text_field.setRows(10);
    		text_field.setColumns(100);
    		
    		Scrollbar scrollbar = new Scrollbar();
    		scrollbar.setBounds(515, 136, 17, 50);
    		contentPane.add(scrollbar);
    		
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.setBounds(0, 0, 555, 21);
    		contentPane.add(menuBar);
    		
    		JMenu mnNewMenu = new JMenu("File");
    		menuBar.add(mnNewMenu);
    		
    		JMenuItem mntmResetAll = new JMenuItem("Reset All");
    		mntmResetAll.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				search_field.setText("");
    				found_field.setText("0");
    				text_field.setText("");
    			}
    		});
    		mnNewMenu.add(mntmResetAll);
    		
    		JMenuItem mntmExit = new JMenuItem("Exit");
    		mntmExit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				System.exit(0);
    			}
    		});
    		mnNewMenu.add(mntmExit);
    		
    		Label label = new Label("#Found");
    		label.setFont(new Font("Dialog", Font.BOLD, 12));
    		label.setBounds(251, 317, 48, 22);
    		contentPane.add(label);
    		
    		search_field = new JTextField();
    		search_field.setBounds(23, 319, 129, 20);
    		contentPane.add(search_field);
    		search_field.setColumns(10);
    		
    		JButton btnNewButton = new JButton("Clear");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				text_field.setText("");
    			}
    		});
    		btnNewButton.setBounds(320, 25, 89, 23);
    		contentPane.add(btnNewButton);
    		
    		JButton btnNewButton_1 = new JButton("Search");
    		btnNewButton_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    		        if(text_field.getText().equals(""))
    		        {
    		            JOptionPane error = new JOptionPane();
    		            error.showMessageDialog(null,"Target TextArea is empty", " target text is empty", JOptionPane.WARNING_MESSAGE);
    		        }
    		        else if(search_field.getText().equals(""))
    		        {
    		            JOptionPane error = new JOptionPane();
    		            error.showMessageDialog(null,"Search TextField is empty", "search text is empty", JOptionPane.WARNING_MESSAGE);
    		        }
    			}
    		});
    		btnNewButton_1.setBounds(156, 318, 89, 23);
    		contentPane.add(btnNewButton_1);
    		
    		JButton btnReset = new JButton("Reset");
    		btnReset.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				search_field.setText("");
    				found_field.setText("0");
    			}
    		});
    		btnReset.setBounds(389, 318, 72, 23);
    		contentPane.add(btnReset);
    		
    		JButton btnExit = new JButton("Exit");
    		btnExit.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent e) {
    				 JOptionPane error = new JOptionPane();
    		            error.showMessageDialog(null,"Good-Bye-Thank You!", " E", JOptionPane.INFORMATION_MESSAGE);
    				System.exit(0);
    			}
    		});
    		btnExit.setBounds(471, 318, 61, 23);
    		contentPane.add(btnExit);
    	}
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    I forgot to mention that these two are the main problem that are not working at my project:

    Searches text for specified string
    Properly counts multiple occurrences
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how to link the search button to search
    Use an action listener. See the tutorial:http://docs.oracle.com/javase/tutori...nlistener.html

    Please edit you post and wrap the code in code tags.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    can anyone do that cuz i have no idea how to do that i am a beginner :S
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    can anyone do that
    Please explain what you are trying to do.

    Searches text for specified string
    What does the program need to do for this one?
    Where is the search String and where is the text to be searched?
    Look at the String class for several useful methods for searching a String.

    counts multiple occurrences
    Use an int variable and add 1 to it for each occurence.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    Please explain what you are trying to do.


    What does the program need to do for this one?
    Where is the search String and where is the text to be searched?
    Look at the String class for several useful methods for searching a String.


    Use an int variable and add 1 to it for each occurence.
    the main goal is to search the word that i write at the text field down when i write there the word and click the search button it should find the word written in the text area and also display the number of the repeated word
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What are the steps in that problem that you are having problems with?
    Make a list of the simple steps the program must take
    and say which ones you are having problems with.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    I have problems with these two steps:

    make the search text field search from the text area
    and
    make the count nr# text field display the number of the repeated words for example the word and is repeated 4 times and the value should be 4
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    make the search text field search from the text area
    OK start with that one
    What steps must be done to do that? Make a list of the simple steps the code needs to do.
    "make a search" is a complicated step. It needs to be broken down into a list of simpler steps.

    Possible first steps:
    1)Get the string to search for
    2)Save it in a variable
    then what ???

    Look at the String class for useful methods for doing the search,
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    if (e.getActionCommand().equals("Search"))
    {
    int lastIndex = 0;
    int count =0;

    }
    else
    {
    while(lastIndex != -1)
    {
    lastIndex = textArea.getText().indexOf(search.getText(),lastIndex);

    if( lastIndex != -1)
    {
    count ++;
    lastIndex ++;
    }
    }
    found.setText(""+count);

    }
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Did you finish the list of steps the program should do for the searching part of the program?

    What is the posted code supposed to do? Does it follow the steps that you made?

    Please wrap all posted code in code tags.

    Also posted at: http://www.java-forums.org/new-java/...h-problem.html
    Last edited by NormR; January 25th, 2013 at 06:42 AM.

IMN logo majestic logo threadwatch logo seochat tools logo