Jump to content

  • Log In with Google      Sign In   
  • Create Account

accessing a jTextArea from inner object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Jarwulf   Members   -  Reputation: 222

Like
0Likes
Like

Posted 16 June 2012 - 01:17 PM

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.

Sponsor:

#2 Dauntless-v2   Members   -  Reputation: 125

Like
0Likes
Like

Posted 17 June 2012 - 11:45 AM

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

#3 Kyan   Members   -  Reputation: 395

Like
0Likes
Like

Posted 17 June 2012 - 01:05 PM

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.

#4 Jarwulf   Members   -  Reputation: 222

Like
0Likes
Like

Posted 19 June 2012 - 04:31 PM

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

#5 Kyan   Members   -  Reputation: 395

Like
0Likes
Like

Posted 19 June 2012 - 05:56 PM

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:
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.

#6 Jarwulf   Members   -  Reputation: 222

Like
0Likes
Like

Posted 06 July 2012 - 04:49 PM

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:

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, 06 July 2012 - 04:50 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS