10 Replies - 2383 Views - Last Post: 27 March 2012 - 07:43 AM Rate Topic: -----

#1 0M3G4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Clearing Static JTextArea from another class

Posted 26 March 2012 - 04:56 PM

I have tried doing ta.setText(""); but it still is failing, and placing the code before my append. I would like to clear out my JTextArea because I am building an Interpreter, and "clear" is an necessary command. I attached my projects code, basically I need a solution to clear the JTextArea ta, which system.out is being redirected to. If you have any other tips for me let me know... By the way, the "clear" command is interpreted in Interpreter > parseLine() in this block of code.

if (isCommand(token))
{
	//do command logic
	if (token.equals("clear")); //Execute logic to clear ta...
	System.out.println("this token ("+token+") is a command");
	return;
}


Just so you know, I am writing this purely for my own education, so it isn't schoolwork...

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Clearing Static JTextArea from another class

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 05:00 PM

Whay a static JTextArea ? Not a very good practice

Simply pass the JTextArea as a parameter to the constructor of your other class so it will be able to access it

P.S.
post you code do to attach it
Was This Post Helpful? 0
  • +
  • -

#3 0M3G4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 05:24 PM

View Postpbl, on 26 March 2012 - 05:00 PM, said:

Whay a static JTextArea ? Not a very good practice

Simply pass the JTextArea as a parameter to the constructor of your other class so it will be able to access it

P.S.
post you code do to attach it


Static because it is in my main class...
Would it be better if I put it in Interpreter? I am self-taught and still learning so I really don't know good practices v.s. bad practices...

Also what did you mean by "post you code do to attach it?"
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 05:34 PM

Dont attach .zip files. Post your code using :code:

Your main class our your main() method

The main() method, that hou haven't posted so it is just a guess, is just to start an application
shouldn't have more than few lines

class Main extend JFrame {
   
     private JTextField ta;    // not static
     Main() {
       .. the JFrame code here
     }

     public static void main(String[] args) {
        Main m = new Main();
        OtherClass oc = new OtherClass(m.ta);  // pass JTextField to other class
     }


Was This Post Helpful? 0
  • +
  • -

#5 0M3G4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 09:12 PM

OK, I have tried that and it doesn't work...
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 09:17 PM

As clear as
"Mechanic, my car does not start. I changed a part, but it is still not starting"
Was This Post Helpful? 0
  • +
  • -

#7 0M3G4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 10:15 PM

View Postpbl, on 26 March 2012 - 09:17 PM, said:

As clear as
"Mechanic, my car does not start. I changed a part, but it is still not starting"


I apologize for lack of clarity... I cannot get my JTextArea to clear...
This is currently my code to try and attempt to, but it still fails, the JTextArea still has text in it.
	public void clear() {
	try {
	  Document doc = textArea.getDocument();
	  doc.remove(0, doc.getLength());
	  textPane.setText(""); 
	} catch(Exception exc) {
	  System.out.println("Thar be an error on the horizon...");
      return;
	}
	}


Pardon my mistake, the above could should be:
	public void clear() {
	try {
	  Document doc = textArea.getDocument();
	  doc.remove(0, doc.getLength());
	  textArea.setText(""); 
	} catch(Exception exc) {
	  System.out.println("Thar be an error on the horizon...");
      return;
	}
	}


I am not sure how to edit these posts...
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 10:16 PM

You are complicating your life for nothing

public void clear() {
  textArea.setText("");
}


will do it. You don't really need a method() for that type of thing
Was This Post Helpful? 0
  • +
  • -

#9 0M3G4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: Clearing Static JTextArea from another class

Posted 26 March 2012 - 10:28 PM

View Postpbl, on 26 March 2012 - 10:16 PM, said:

You are complicating your life for nothing

public void clear() {
  textArea.setText("");
}


will do it. You don't really need a method() for that type of thing


I cannot get that to work... I put it just like that into my code and it fails, the text area STILL has text in it from before. I think it maybe because I am using my JTextArea as an output for System.out... I am so confused. I really would like to get this to work because the reason I switched over to this is because I needed the screen to be "clearable." OK, here is my "mainWindow" method.
Spoiler


Now here is my main:
Spoiler


Now when I run this code, it still has "Hyperion Interpreter" in the TextPane/TextArea. I am not sure why but I cannot get it to work...
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Clearing Static JTextArea from another class

Posted 27 March 2012 - 07:32 AM

Did you never watch the movie GhostBusters??? You're not supposed to cross the streams! :)

Document doc = textPane.getDocument();
	  doc.remove(0, doc.getLength());
	  textPane.setText(""); 



This is redundant, as setText() calls doc.remove().

I don't fully understand why, but you redirecting System.out to that JTextpane interferes with its setText(). Why do that anyway?
Was This Post Helpful? 0
  • +
  • -

#11 0M3G4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-March 12

Re: Clearing Static JTextArea from another class

Posted 27 March 2012 - 07:43 AM

View Postfarrell2k, on 27 March 2012 - 07:32 AM, said:

Did you never watch the movie GhostBusters??? You're not supposed to cross the streams! :)

Document doc = textPane.getDocument();
	  doc.remove(0, doc.getLength());
	  textPane.setText(""); 



This is redundant, as setText() calls doc.remove().

I didn't realize that, sorry. I was just trying both ways to see what would happen.

View Postfarrell2k, on 27 March 2012 - 07:32 AM, said:

I don't fully understand why, but you redirecting System.out to that JTextpane interferes with its setText(). Why do that anyway?

I am building a GUI frontend for a primarily console application, and I am too lazy to change all of the occurrences of System.out in it. I don't understand why either...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1