Sign in to follow this  
Jarwulf

accessing a jTextArea from inner object

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 this post


Link to post
Share on other sites
Assuming (from your description) you have:
[code]
public class Object1 {
public JTextArea textArea;
private InnerObject object2;
}
[/code]
then either:[list]
[*][i]textArea[/i] needs to be made static, thereby you may call [i]Object1.textArea [/i]to obtain the reference to the JTextArea. Do understand what [i]static[/i] means before you do this.
[*][i]object2[/i] needs a reference to it's containing [i]Object1[/i], thereby it may call [i]obj1Instance.textArea [/i][b]or[/b] you can just pass the [i]JTextArea[/i] itself. The easiest way is via [i]InnerObject[/i]'s constructor.
[/list]
Technically you could also use [non-static] inner classes to get around this but imo that's rarely a helpful solution.

Share this post


Link to post
Share on other sites
[quote name='Kyan' timestamp='1339959906' post='4950061']
Assuming (from your description) you have:
[code]
public class Object1 {
public JTextArea textArea;
private InnerObject object2;
}
[/code]
then either:[list]
[*][i]textArea[/i] needs to be made static, thereby you may call [i]Object1.textArea [/i]to obtain the reference to the JTextArea. Do understand what [i]static[/i] means before you do this.
[*][i]object2[/i] needs a reference to it's containing [i]Object1[/i], thereby it may call [i]obj1Instance.textArea [/i][b]or[/b] you can just pass the [i]JTextArea[/i] itself. The easiest way is via [i]InnerObject[/i]'s constructor.
[/list]
Technically you could also use [non-static] inner classes to get around this but imo that's rarely a helpful solution.
[/quote]

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

[CODE]public static jTextArea textArea;[/CODE]

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

[/CODE]
in Object2

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

Share this post


Link to post
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.

As for your second question:
[code]
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);
}
}
[/code]
[code]
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());
}
}
[/code]
Just be sure that the text area is initialized before you pass it, and that you don't accidentally destroy it before the [i]InnerObject[/i] 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 [i]Object1[/i] instead of a [i]JTextArea[/i] and use a getter to obtain the text area as well.

Share this post


Link to post
Share on other sites
[quote name='Kyan' timestamp='1340150162' post='4950770']
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.

As for your second question:
[code]
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);
}
}
[/code]
[code]
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());
}
}
[/code]
Just be sure that the text area is initialized before you pass it, and that you don't accidentally destroy it before the [i]InnerObject[/i] 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 [i]Object1[/i] instead of a [i]JTextArea[/i] and use a getter to obtain the text area as well.
[/quote]

Okay if I do something like this

[CODE]
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;
}
[/CODE]


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.


[CODE]
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;
}[/CODE]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this