Results 1 to 10 of 10
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need more action listener help

    Trying to write a program that will search the text field for the word in the search box and return how many there are in the found box,my program stalls as soon as i click search and I am not seeing why, this is what i have:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class WordSearch implements ActionListener
    {
    	public WordSearch()
    	{
    		JFrame wordSearch = new JFrame("Word Search");//create frame 
    		wordSearch.setLayout(new BorderLayout());//set layout to borderlayout
    		wordSearch.setSize(500, 300);//set size of frame
    		wordSearch.setLocation(200, 200);//set where frame will populate on screen
    		
    		
    		JLabel label = new JLabel("Empty or Copy & Paste Text");//label for top panel
    		JButton clear = new JButton("Clear");//Add clear button for top panel
    		clear.setMnemonic('C');//set c as the mnemonic for clear
    		
    		JTextArea area = new JTextArea(30,40);//set a typable text area
    		
    		area.setLineWrap(true);
          area.setWrapStyleWord(true);
    		
    		JTextArea search = new JTextArea(2,10);//set a typable search area
    		search.setVisible(true);
    		search.setEditable(true);//allows user to enter text
    		JButton search1 = new JButton("Search");//creates a search button
    		search1.setMnemonic('S');//set s as the mnemonic for search
    		
    		JLabel found2 = new JLabel("# Found");
    		JTextArea found = new JTextArea(2,5);//sets a found search area
    		found.setVisible(true);
    		JButton reset = new JButton("Reset");//creates new button to reset the findings
    		reset.setMnemonic('R');
    		JButton exit = new JButton("Exit");//creates new button to exit program
    		exit.setMnemonic('x');
    		
    		JPanel jpNorth = new JPanel();//create a panel for the top
    		JPanel jpCenter = new JPanel();//create a center panel for text box
    		JPanel jpSouth = new JPanel();//create a panel for the bottom
    		
    		jpNorth.add(label);//places label in top panel
    		jpNorth.add(clear);//places button in top panel
    		
    		jpCenter.add(area);//places text box in center panel
    		
    		jpSouth.add(search);//places text box in bottom panel
    		jpSouth.add(search1);//places search button
    		jpSouth.add(found2);//label for found
    		jpSouth.add(found);//places found txt box in bottom panel
    		jpSouth.add(reset);//places reset button in bottom panel
    		jpSouth.add(exit);//places exit button in bottom panel
    		
    		wordSearch.add(jpNorth, BorderLayout.NORTH);//places top panel in north position
    		wordSearch.add(jpCenter, BorderLayout.CENTER);//places center panel in center position
    		wordSearch.add(jpSouth, BorderLayout.SOUTH);//places bottom panel in bottom postion
    		
    		JMenuBar mbar = new JMenuBar();//create menu bar
    		wordSearch.setJMenuBar(mbar);
    		JMenu file = new JMenu("File");//creates a file button on menu bar
    		JMenuItem resetAll = new JMenuItem("ResetAll");//create reset all option under file
    		resetAll.setMnemonic('A');
    		JMenuItem exit1 = new JMenuItem("Exit");//create exit option under file
    		exit1.setMnemonic('x');
    		file.add(resetAll);//add reset all to file option on menu bar
    		file.add(exit1);//add exit to file option on menu bar
    		mbar.add(file);//adds file option to menu bar
    		
    		exit.addActionListener(this);//adds action listener to the exit button
    		exit1.addActionListener(this);//adds action listener to the exit menu item
    		
    		wordSearch.setVisible(true);//allows the frame to be visible
    		
    		Searcher searchIT = new Searcher(search1, area, search, found);//sets listner for search1 JButton
    		
    		wordSearch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//so program will close when x is clicked		
    	}
    	
    	public void actionPerformed(ActionEvent e)//sets an action to be performed for the exit options
    	{
    		if (e.getActionCommand().equals("Exit"))
    		{
    			System.exit(0);
    		}
    		else if (e.getActionCommand().equals("x"))
    		{
    			System.exit(0);
    		}
    	}
    	public static void main(String[] args)
    	{
    		WordSearch word = new WordSearch();
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Searcher implements ActionListener
    {
    	//attributes
    	JButton button;
    	JTextArea area;
    	JTextArea search;
    	JTextArea found;
    	
    	public Searcher(JButton _button, JTextArea _area, JTextArea _search, JTextArea _found)
    	{
    		button = _button;//instantiate button from word search program
    		button.addActionListener(this);//add action listener to button
    		area = _area;//instantiate text field
    		search = _search;//instantiate search field
    		found = _found;//instantiates found field
    	}
    	
    	public void  actionPerformed(ActionEvent e)
    	{
    		if (e.getActionCommand().equals("Search"))
    			{
    				int lastIndex = 0;
    				int count =0;
    				if ((area.getText()).length() < 1)//show warning message if text field is empty
    					{
    						JOptionPane frame = new JOptionPane("Message");
    						JOptionPane.showMessageDialog(frame, "Target TextArea is empty");						
    					}
    				else if ((search.getText()).length() < 1)//show warning message is search field is empty
    					{
    						JOptionPane frame = new JOptionPane("Message");
    						JOptionPane.showMessageDialog(frame, "Search TextArea is empty");
    						search.requestFocusInWindow();
    					}
    				else//to perform an actual search
    					{
    						while(lastIndex != -1)
    							{
    								lastIndex = area.getText().indexOf(search.getText(),lastIndex);
    								if( lastIndex != -1)
    									{
                 						count ++;
          							}
    							}
    					found.setText(""+count);
    					
    					}
    			}
    	}
    }
    Thank you for your help

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Need more action listener help

    program stalls as soon as i click search
    Can you explain what 'stalls' means? Do you get an Error message, or is there an infinite loop or waiting on user input?

    Add some calls to the println method to print out messages when methods are called so you can see where the code is executing.
    Last edited by Norm; 01-17-2013 at 03:11 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need more action listener help

    Im not getting any error messages at all, it just stops and i have to manually click end in jGrasp, and i will try to add the print methods as well

  4. #4
    dynamix is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: Need more action listener help

    well this is very surprising :/. No idea of why this would happen.

    ndsmith20, let me know if you find out what the problem is :/

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Need more action listener help

    i have to manually click end in jGrasp,
    Sounds like an infinite loop. Be sure to add printlns inside all loops and have them print out the variables that control the loop.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Need more action listener help

    Specifically, you know that it has something to do with your ActionListener and the calling of your actionPerformed method, so put some calls into your code such as:

    System.out.println("Entering actionPerformed");

    As the first thing that your actionPerformedMethod() does and then put

    System.out.println("About to exit actionPerformed");

    As the last thing that your method does, and then once you can see whether your method is being called, you can narrow it down more by entering more calls to System.out.println().

  7. #7
    lbarowski is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need more action listener help

    Instead of adding print statements, it's probably more convenient to set a breakpoint on the first line of actionPerformed(), debug, then step through to see what's going on.

    Also, though it rarely causes a problem for simple examples, you should not execute gui code except from the AWT EDT. So you should do:
    Java Code:
    SwingUtilities.invokeLater(
       new Runnable() {
          public void run() {
             WordSearch word = new WordSearch();
          }
       });

  8. #8
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need more action listener help

    Thank you guys, I will set some break points and some print statements to debug this program. Ill let you know what I find

  9. #9
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need more action listener help

    set a print line in my loop and yes it is an infinite loop. now i just to find out how to get out of it

  10. #10
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need more action listener help

    got it
    Java Code:
    while(lastIndex != -1)
    							{
    								lastIndex = area.getText().indexOf(search.getText(), lastIndex);
    								if( lastIndex != -1)
    									{
                 						count ++;
    										System.out.println("test");
    										lastIndex += (search.getText()).length();
          							}
    							}

Similar Threads

  1. Need help with action listener please!!!
    By ndsmith20 in forum New To Java
    Replies: 3
    Last Post: 01-17-2013, 03:44 AM
  2. Can anyone help me with an action listener?
    By mdCollins1 in forum New To Java
    Replies: 5
    Last Post: 03-21-2012, 05:07 AM
  3. Replies: 2
    Last Post: 12-11-2011, 11:44 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •