Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By milovan
  • 1 Post By kneitzel

Thread: Text Area can not be resolved or is not a field.

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    118
    Rep Power
    0

    Default Text Area can not be resolved or is not a field.

    In my application, some text should be added to a text area in response to a click on a button. So as an action listener to this class, I made another class which implements the ActionListener.

    Inside this class, I have obtained the text which I want to be added to the text area. But the text area is in another class and for the action listener I wrote another class.

    Now the problem is that when I try to add the text to the text area by the following line of code, it says that textArea_1 can not be resolved or is not a field.

    Java Code:
    ParentPanel.textArea_1.setText("Name:"+ncrarray[0]+"\nCode:"+ncrarray[1]+"\nRank:"+ncrarray[2]);
    What should I do about it?

    _________________________________________

    Even if I try to write a method like the following in the class in which the text area is created, it gives the same error.

    Java Code:
    public void printTextArea(String text) {
    	    textArea_1.setText(text);
    	}
    NOTE:- The text area is present inside a constructor of the class. I am writing the method outside the constructor (ofcourse).
    Last edited by Zarah; 04-30-2014 at 10:20 PM.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Text Area can not be resolved or is not a field.

    show us how you instantiated textArea..

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    118
    Rep Power
    0

    Default Re: Text Area can not be resolved or is not a field.

    Quote Originally Posted by milovan View Post
    show us how you instantiated textArea..
    Java Code:
    JTextArea textArea_1 = new JTextArea();
    		textArea_1.setText(" ");
    		GroupLayout gl_panel_2 = new GroupLayout(panel_2);

    I was using the Eclipse Window Builder, this code was autogenerated..

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Text Area can not be resolved or is not a field.

    Ok, so if you instantiate JTextArea in this manner and it is inside the constructor it is no wonder if it is not reachable outside the constructor. You should declare JTextArea outside constructor (inside a declaration part at the beggining of the class) in order to be visible to all members of the class

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Text Area can not be resolved or is not a field.

    Also this part about all these different classes confuses me. One class has text you want to display in text area in another class via button and ActionListener which is in third class. Looks like over complicated (not saying impossible or even required by design), just go through your design and think if it could be simplified.

  6. #6
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    118
    Rep Power
    0

    Default Re: Text Area can not be resolved or is not a field.

    Ok, so if you instantiate JTextArea in this manner and it is inside the constructor it is no wonder if it is not reachable outside the constructor. You should declare JTextArea outside constructor (inside a declaration part at the beggining of the class) in order to be visible to all members of the class
    Doing that makes the error go away from the method in the same class, but is still there in the other class. Neither can I call the method "printTextArea" of that class. It gives the same error: The method printTextArea(String) is undefined for the type...

    Here is the calling code:

    Java Code:
    String textToPass="Name:"+ncrarray[0]+"\nCode:"+ncrarray[1];
    		new ParentPanel().printTextArea(textToPass); /////error
    Here is the method in the other class:

    Java Code:
    public void printTextArea(String text) {             // No error
    	    new ParentPanel().textArea_1.setText(text);
    	}

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Text Area can not be resolved or is not a field.

    Sorry to say but this is not good approach. For every referring to textArea you are creating new ParentPanel objects but both, method and textArea its self, are supposed to be in ParentPanel??
    In this case you should have one instantiated object:

    Java Code:
    ParentPanel panel = new ParentPanel();
    ...
    panel.printTextArea(someText);
    Something like that. It is too little details to see how your code is functioning now to give you right direction. Try to make small example to show these basic operations that you want to make and that gives you trouble.
    Zarah likes this.

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Text Area can not be resolved or is not a field.

    I think you missed some basics with object oriented design....

    Just a comparison:
    A class is a blueprint for a house.
    With new you build a new house.
    Fields are rooms in the house.

    So you created somewhere a house with a plan labeld "ParentPanel". But when you wanted to put something inside a room, you
    - create a new house
    - put stuff in a room in there
    - And then of course forget the address of the house.

    You need to take care of the address (reference) of the house you want to work with.
    So when you have 2 instances (a and b) and you want that one instance (a) is doing something with the other instance (b), then you have to have a reference of b inside a.

    That is really some basic stuff and if you have problems with this then you should read about that before you continue your development task.

    Konrad
    Zarah likes this.

Similar Threads

  1. eclipse java cannot be resolved or is not a field
    By newProgrammer in forum Android
    Replies: 4
    Last Post: 08-31-2012, 06:27 PM
  2. eclipse java cannot be resolved or is not a field
    By newProgrammer in forum Eclipse
    Replies: 1
    Last Post: 08-30-2012, 06:47 PM
  3. cannot be resolved or is not a field
    By Stevemagegod in forum Eclipse
    Replies: 3
    Last Post: 04-10-2012, 01:14 AM
  4. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  5. Replies: 0
    Last Post: 12-09-2010, 06:56 PM

Posting Permissions

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