Results 1 to 4 of 4
  1. #1
    danborgir is offline Member
    Join Date
    Jan 2011
    Rep Power

    Default looping through vectors and outputting to JTextArea

    I'm creating a move history feature for a chess game I am developing.

    In one class I have the vectors and then in another class I am collecting the data and outputting it in the .setText() part of the JTextArea. The code looks like this:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class MoveHistoryText extends JFrame {
        //============================================== instance variables
       JTextArea _resultArea = new JTextArea(25, 35);
        //====================================================== constructor
        public MoveHistoryText() {
    		//... Create String of all stuff
            //... Set textarea's initial text, scrolling, and border.
    		for(int moveList = 0; moveList < Pieces.pieces1History.size(); moveList++) {
            _resultArea.setText("Move " + Pieces.i + ": " + Pieces.pieces1History.elementAt(moveList) + " moved from square: " + Pieces.player1moveStart.elementAt(moveList) + " to square: " + Pieces.player2moveFinish.elementAt(moveList));
            JScrollPane scrollingArea = new JScrollPane(_resultArea);
            //... Get the content pane, set layout, add to center
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(scrollingArea, BorderLayout.CENTER);
            //... Set window characteristics.
            this.setTitle("Move History");
    In the
    Java Code:
    is where the magic happens. At the moment it outputs only the current move. What I want to be able to have is either the JTextArea updates once a piece has moved with the new history or once the class has been called from the main class it would loop through all the stored history in the vectors and print it all out to the JTextArea. I don't know the best way to do this so any help would be great! p.s. I know the for loop is probably wrong... Trial and error!

    thanks in advance! :D

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    Either manually append the text using string addition, getText, and setText, or using the append() method of JTextArea (you might want to have some sort of delimiter like a new line as well)

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    London, UK
    Blog Entries
    Rep Power


    Every time you use setText() again you write over whatever the Text was before.

    Why not use a JLabel instead with HTML so that you can output the text nicely formatted?

    We can use a List to read the Strings from
    Java Code:
    //create a List to hold the move history strings
    java.util.List<String> moveHistory = new java.util.ArrayList<>();
    //usage: moveHistory.add(String s);

    Then create a new method to convert the strings to formatted HTML
    Java Code:
    //returns String to set JLabel with
    private String getMoveHistory() {
        //define String
        String historyHTML = "<html><head><style type=\"text/css\">"
         + "tr.rowA td {background-color: #CCCCCC; color: #000000;}"
         + "tr.rowB td {background-color: #EEEEEE; color: #000000;}"
         + "</style></head><body><table width=100% height=100%>";
        //loop through our List of moves
        for (int i=0; i<moveHistory.size(); i++) {
            //define temp String
            String newRow = "<tr class=\"" + ((i%2==0)? "rowA" : "rowB") + "\">"
             +"<td>Move #" + i + "</td>"
             +"<td>" + moveHistory.get(i) + "</td></tr>";
            //append to result
            historyHTML += newRow;
        //end HTML
        historyHTML += "</table></body></html>";
        return historyHTML;

    Finally, set the JLabel with our new method
    Java Code:

  4. #4
    danborgir is offline Member
    Join Date
    Jan 2011
    Rep Power


    Thanks ozzyman that looks like a much better way to do it! :D

Similar Threads

  1. outputting different letters in different colours
    By Sarah24 in forum Java Applets
    Replies: 4
    Last Post: 03-02-2011, 03:32 PM
  2. Help With Outputting Data To A File
    By Prodigial Mouse in forum New To Java
    Replies: 14
    Last Post: 01-24-2011, 09:01 PM
  3. Outputting a * triangle
    By blackbeltsas in forum New To Java
    Replies: 1
    Last Post: 10-17-2010, 11:39 AM
  4. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM
  5. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts