PDA

View Full Version : NEED HELP ACTIONLISTENER IS NOT BEING SEEN


Sheldonj21
04-07-2002, 01:49 AM
ok I need help this is what I am having a problem with the action listener is not being seen and I do not know why...this is only the end of my GUI
super("File Search : fgrep");
setLocation(200,200);
setSize (500,300);
Container c = getContentPane();
c.setBackground (Color.white);
c.setForeground (Color.black);

pnlLabel.add(lblFileName);
pnlLabel.add(txtFileName);
pnlLabel.add(lblString);
pnlLabel.add(txtString);
c.add(pnlLabel,BorderLayout.NORTH);

scroll.add(txtYourTextArea);
c.add (scroll, BorderLayout.CENTER);
**Suspected problem is thougt to be between here**

pnlButtons.add(btnSearch);
pnlButtons.add(btnCountLines);
pnlButtons.add(btnPrintLineNumbers);
c.add(pnlButtons,BorderLayout.SOUTH);

btnSearch.addActionListener(this);
btnCountLines.addActionListener(this);
btnPrintLineNumbers.addActionListener(this);
setVisible(true);
}
*********************AND HERE**********************
public void actionPerformed(ActionEvent event) {

if (event.getSource() == btnSearch) {
System.out.println("hello");
//txtYourTextArea.setText(this.grep.Sea());
}
if (event.getSource() == btnCountLines) {
txtYourTextArea.setText(this.grep.Cou());
}
if (event.getSource() == btnPrintLineNumbers) {
txtYourTextArea.setText(this.grep.Pri());
}


}
public static void main(String args[]) {
new GrepGUI(); }

}

kmj
04-07-2002, 01:17 PM
Please note that making your thread title have all capitals letters is going to annoy the people who you want to help you, and is considered bad form. If you really need help and want extra attention, we suggest you prepend [help] to your title (as per forum rules). Thanks.

Also, I suggest you surround the code in your post with CODE tags. These are simply the word CODE surrounded with [ and ], and to close /code.. [.code] and [./code] (but remove the periods)..



main (0){
while (1) {
x = y;
}
}

Dru Lee Parsec
04-08-2002, 12:32 PM
The problem is that your action listener asumes that your GUI is receiving the action event. It's not, the buttons are receiving the action events.

Go to www.CodeExamples.org Click on "Code Examples" and then on "Java". In the resulting list of examples look at the code for "Dealing with clickable buttons". It shows how to add a listener to a button rather than to a JPanel or JFrame.

I know that you think you're using the event source to find out if the button has been clicked, but what's happening is that when the button is clicked it's not sending that action up to the object that contains it.

I think that's how it use to work back in the 1.0 days. But now you have to add the action listener to the object that receives the action. This results in much cleaner and more object oriented code.