Set text in JTextPane from another class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 10
Reputation: Barman007 is an unknown quantity at this point 
Solved Threads: 0
Barman007 Barman007 is offline Offline
Newbie Poster

Set text in JTextPane from another class

 
0
  #1
May 14th, 2008
Im hoping this is just simple, but I have a main class for my GUI and I do some printing in another class called board, which gets its information from my peg class, which gets its information from my linked list.

Is there and easy way to print my pegs out onto my JTextPane because System.out.print just prints out to the console. I'll show my functions here just in case its a little more complicated like having to change my void functions to string to return a value. If that is the case could some please let me know how to do that because ive tried before and couldnt do it.

This stuff works fine in my main class like this, where display is just what ive called my instance of the JTextPane
MAIN CLASS
  1. display.setText("\n\n*** Bad Input, please try again! \n");

This is where I start having trouble
BOARDCLASS
  1. public void printBoard(){
  2.  
  3. System.out.print("\n\n\n\n\n\n\n\n|");
  4. peg1.print();
  5. System.out.print("|");
  6. peg2.print();
  7. System.out.print("|");
  8. peg3.print();
  9. System.out.print("\n\n\n\n\n\n\n");
  10. if(peg3.topNum() == 1 && peg2.topNum() == peg1.topNum()){
  11. gameFinish = true;
  12. }
  13. //do some debugging
  14. System.out.println(peg1.topNum() + "" + peg2.topNum() + "" + peg3.topNum());
  15. }

PEG CLASS
  1. public void print() {
  2. printList();
  3. }

LLIST CLASS
  1. public void printNode(llnode node) {
  2. if (node != null) {
  3. printNode(node.nextNode());
  4. System.out.print(node.dataVal() + " ");
  5. }
  6. }

LLNODE CLASS
  1. public int dataVal() {
  2. return data;
  3. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Set text in JTextPane from another class

 
0
  #2
May 14th, 2008
Change your print methods to return the Strings you want, rather than actually sending output to System.out. Your GUI class can then put those strings wherever it wants to. If fact you could just override toString() to return the string representation of each of those objects and do away with print() methods altogether.

An alternative is to pass a reference to a text component to each print method and let each of the objects "print" to that instead of the console, but it does limit your flexibility a bit (i.e. you can only print to a JTextComponent or whatever you decide to pass as a parameter). Overriding toString() lets you get a custom string representation for any of the objects and use it as you please.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1932 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC