• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

I need help in Java really fast.

Status
Not open for further replies.

Dinokill

Member
I'm starting some small projects on java and I can't get pass this error.

WordsPanel.java:27: error: cannot find symbol
message.addActionListener(listener);
^
symbol: method addActionListener(WordsPanel.WordListener)
location: variable message of type JTextArea
1 error

Here is my code

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class WordsPanel extends JPanel
{
   private JTextArea message;
   private JLabel numWords, average;
   private int wordNumber = 0;
   
   public WordsPanel()
   {
      // panel for text area
      message = new JTextArea(5, 25);
      message.setLineWrap(true);
      message.setWrapStyleWord(true);
      JPanel panel = new JPanel();
      panel.add(message);
      
      WordListener listener = new WordListener();
      message.addActionListener(listener);
      
      // panel for word count
      numWords = new JLabel("Numer of words: " + wordNumber);
      JPanel panel1 = new JPanel();
      panel1.add(numWords);
 
      add(panel);
      add(panel1);
      
      setPreferredSize(new Dimension(400, 300));
      setBackground(Color.white);
   }
   
   private class WordListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         //some stuff ill do later
      }  
   }
}

I have no idea why the error when this code works perfectly

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LeftRightPanel extends JPanel
{
   private JButton left, right;
   private JLabel label;
   private JPanel buttonPanel;
   
   public LeftRightPanel()
   {
      left = new JButton("Left");
      right = new JButton("Right");
      
      ButtonListener listener = new ButtonListener();
      left.addActionListener(listener);
      right.addActionListener(listener);
      
      label = new JLabel("Push a button");
      
      buttonPanel = new JPanel();
      buttonPanel.setPreferredSize(new Dimension(200, 40));
      buttonPanel.setBackground(Color.blue);
      buttonPanel.add(left);
      buttonPanel.add(right);
      
      setPreferredSize(new Dimension(200, 80));
      setBackground(Color.cyan);
      add(label);
      add(buttonPanel);
   }
   
   private class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         if(event.getSource() == left)
         {
            label.setText("Left");
         }
         else
         {
            label.setText("Right");
         }
      }
   }
}


In short. I don't know why it can'T find the symbol addActionListener on the first program I posted when the second works perfectly.

I'm using JGRASP.
 

Patryn

Member
It's been a while since I've done Java, so I'm not sure the full stack of inheritence, but in your first program you're calling addActionListener on a JTextArea object, but in the second program you're calling it on a JButton object.

Is addActionListener a valid method on a JTextArea object? That appears to be what the error is complaining about.

Documentation indicates that it's not, which is what your problem is, which makes sense as one appears to be just for displaying text and the other is a button.
 

Adaren

Member
It's been years since I did any Java, but it looks like JTextArea doesn't have a method named addActionListener.

So you'll need to either change the method name to something that a JTextArea can actually do, or you'll need to change the type of the message variable to something that can "addActionListener".

(I'm pretty much just reading the error message, so yeah. Hopefully someone else can help you more lol.)
 
It's been a while since I've done Java, so I'm not sure the full stack of inheritence, but in your first program you're calling addActionListener on a JTextArea object, but in the second program you're calling it on a JButton object.

Is addActionListener a valid method on a JTextArea object? That appears to be what the error is complaining about.

Yeah, JTextArea doesn't have an addActionListener method.

What is the code supposed to do, OP?
 
I don't known anything of the library you are using, but googling JTextArea results in that the JTextArea (message) does not contain the method addActionListerer, while the JButtons do.

Also: docs.
 
It seems you want to count the number of words in the JTextArea every time it changes?

If that's the case you probably want to use addKeyListener or getDocument().addDocumentListener and also change your WordListener accordingly.
 
Status
Not open for further replies.
Top Bottom