# accessing a jTextArea from inner object

This topic is 2060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

java with eclipse

I have an object Object1 where a jTexArea is declared as

public jTextArea txtArea;

I also have another object Object2 declared in Object1

private InnerObject Object2;

whenever I try to access txtArea from Object2 I get an error that it can't resolve txtArea. The error is not solved by switching Object2 into a static method. How whould I be able to access txtArea thanks.

##### Share on other sites
Try making your inner object public or protected. If that doesn't work, you might have to make it final.

##### Share on other sites
Assuming (from your description) you have:
 public class Object1 { public JTextArea textArea; private InnerObject object2; } 
then either:

• textArea needs to be made static, thereby you may call Object1.textArea to obtain the reference to the JTextArea. Do understand what static means before you do this.
• object2 needs a reference to it's containing Object1, thereby it may call obj1Instance.textArea or you can just pass the JTextArea itself. The easiest way is via InnerObject's constructor.

Technically you could also use [non-static] inner classes to get around this but imo that's rarely a helpful solution.

##### Share on other sites

Assuming (from your description) you have:
 public class Object1 { public JTextArea textArea; private InnerObject object2; } 
then either:

• textArea needs to be made static, thereby you may call Object1.textArea to obtain the reference to the JTextArea. Do understand what static means before you do this.
• object2 needs a reference to it's containing Object1, thereby it may call obj1Instance.textArea or you can just pass the JTextArea itself. The easiest way is via InnerObject's constructor.

Technically you could also use [non-static] inner classes to get around this but imo that's rarely a helpful solution.

Alright, so in the jTextArea declaration in Object1 I could go

public static jTextArea textArea;

And then do something like
 Object1.txtArea.append("Converting file please wait\n"); Object1.txtArea.setCaretPosition(txtArea.getText().length()); 
in Object2

I'm not quite sure what you mean in your second bullet point. Can you show me in code? Thx

##### Share on other sites
For your first question/example, yes, that will work as long as you ensure the text area is valid before you try to use it.

 public class Object1 { // this can also be private if you wish public JTextArea textArea; // the object that uses the text area private InnerObject object2; public Object1() { // do some kind of initialization of the JTextArea, e.g. textArea = new JTextArea( .. ); // pass it to the object that needs it object2 = new InnerObject(textArea); } } 
 public class InnerObject { // our own version of the text area private JTextArea myTextArea; // using different names to make them clear for this example public InnerObject(JTextArea param) { this.myTextArea = param; } // use your text box public void doSomethingClever() { this.myTextArea.append("Converting file please wait\n"); this.myTextArea.setCaretPosition(this.myTextArea.getText().length()); } } 
Just be sure that the text area is initialized before you pass it, and that you don't accidentally destroy it before the InnerObject instance is done with it (for whatever reason).

As per my initial comment in that bullet point, you could do the same thing with an Object1 instead of a JTextArea and use a getter to obtain the text area as well.

##### Share on other sites

For your first question/example, yes, that will work as long as you ensure the text area is valid before you try to use it.

 public class Object1 { // this can also be private if you wish public JTextArea textArea; // the object that uses the text area private InnerObject object2; public Object1() { // do some kind of initialization of the JTextArea, e.g. textArea = new JTextArea( .. ); // pass it to the object that needs it object2 = new InnerObject(textArea); } } 
 public class InnerObject { // our own version of the text area private JTextArea myTextArea; // using different names to make them clear for this example public InnerObject(JTextArea param) { this.myTextArea = param; } // use your text box public void doSomethingClever() { this.myTextArea.append("Converting file please wait\n"); this.myTextArea.setCaretPosition(this.myTextArea.getText().length()); } } 
Just be sure that the text area is initialized before you pass it, and that you don't accidentally destroy it before the InnerObject instance is done with it (for whatever reason).

As per my initial comment in that bullet point, you could do the same thing with an Object1 instead of a JTextArea and use a getter to obtain the text area as well.

Okay if I do something like this

 class Object 1 { public void actionPerformed(ActionEvent e) { InnerObject.Test(txtArea); } } //Inner Object public static int Test(JTextArea InnerTextArea) //DEBUGGING METHOD { int convertstepcounter=0, convertstepcounter2=0; convertstepcounter2=convertstepcounter; Integer.toString(convertstepcounter2); InnerTextArea.append(""+convertstepcounter2 + "\n"); InnerTextArea.setCaretPosition(InnerTextArea.getText().length()); return 0; } 

I get output in the txtArea as expected

But I want to do something more complicated I want to do a count in the txtArea while a file is being processed so the user knows the conversion is in progress. If I do this.

 class Object 1 { public void actionPerformed(ActionEvent e) { InnerObject.doStuff(OuterFile, txtArea); } } public static File doStuff(File InnerFile, JTextArea InnerTextArea) { try { while (somecondition) { //Do converting stuff here convertstepcounter2=convertstepcounter; Integer.toString(convertstepcounter2); InnerTextArea.append(""+convertstepcounter2 + "\n"); InnerTextArea.setCaretPosition(InnerTextArea.getText().length()); convertstepcounter++; } } catch(IOException ie){ ie.printStackTrace(); } reader.close(); return convertedFile; }

I get nothing, probably since you have to exit the while loop to print a message. Would there be a solution to this problem so I can get a counting txtArea while in InnerObject? Edited by Jarwulf