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

display.setText("\n\n*** Bad Input, please try again! \n");

This is where I start having trouble
BOARDCLASS

public void printBoard(){
       
        System.out.print("\n\n\n\n\n\n\n\n|");
        peg1.print();        
        System.out.print("|");
        peg2.print();
        System.out.print("|");
        peg3.print();
        System.out.print("\n\n\n\n\n\n\n");
        if(peg3.topNum() == 1 && peg2.topNum() == peg1.topNum()){ 
            gameFinish = true; 
        } 
        //do some debugging 
        System.out.println(peg1.topNum() + "" + peg2.topNum() + "" + peg3.topNum());            
    }

PEG CLASS

public void print() {
        printList();
    }

LLIST CLASS

public void printNode(llnode node) { 
  if (node != null) { 
    printNode(node.nextNode()); 
    System.out.print(node.dataVal() + " "); 
  } 
}

LLNODE CLASS

public int dataVal() {
        return data;
    }

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.

This article has been dead for over six months. Start a new discussion instead.