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:
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();
}
}
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
Re: Need more action listener help
Quote:
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.
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
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 :/
Re: Need more action listener help
Quote:
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.
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().
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:
Code:
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
WordSearch word = new WordSearch();
}
});
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
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
Re: Need more action listener help
got it
Code:
while(lastIndex != -1)
{
lastIndex = area.getText().indexOf(search.getText(), lastIndex);
if( lastIndex != -1)
{
count ++;
System.out.println("test");
lastIndex += (search.getText()).length();
}
}