0

I am making a homework planner GUI program in Java. One GUI takes in data from the user about the assignment, determines priority for the order the homework should be done in, then allows the user to view their schedule as another GUI that displays all their assignments in the appropriate days of the week that they will work on them.

My problem is that I can't get the Strings to format correctly in the JLists. See here:

enter image description here

I'm filling the JLists with String representations of Homework Objects from a PriorityQueue, like this:

while(!sun.isEmpty())
    sundayListItems.add(sunday.poll().toString());
.
.
.
sundayList = new JList(sundayListItems.toArray());

My toString method in my Homework class looks like this, which works as it's supposed to in the console when I put in System.out.prints to see how it's working:

    String string = nameOfAssignment + " for " + hoursPerDay +" hours";
    if (string.length()>17) {//17 characters per line in this JList
        String[] words = string.split(" ");
        string = "";
        int count = words.length;
        int i = 0;
        while(count>0) {
            do{
                string += words[i] + " "; //adding words to a line until 17 characters
                i++;
                count--;
            } while(string.length() <= 17);

            string += "\n";//skips line when 17 characters

            do{
                string += words[i] + " ";
                count--;
                i++;
            } while(i < words.length);

            string += "\n\n";//separates assignments w/ 2 lines
        }
    }
    return string;

But, in the GUI JLists, the Strings are not formatting the way I want. I want the String to wrap around to the next line after 17 characters. Anyone have any ideas?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Tanner
  • 93
  • 1
  • 1
  • 11
  • How do you expect "wrap around" to work? If you put your first 17 characters on the first line, that's the first list item. Where should the remaining characters go? If "wrap around" means the next list item, then you should explicitly add the remaining characters as the next list item (indent it with spaces to make it clear). Adding a String to the list with "\n" is not going to make it wrap or force it to the next list item. You need to code that. – Edwin Torres Jun 05 '14 at 20:17
  • I don't want the remaining characters to be a new list item, I just want the list item to be two lines instead of one. – Tanner Jun 05 '14 at 20:48
  • Try this post: http://stackoverflow.com/questions/7306295/swing-jlist-with-multiline-text-and-dynamic-height – Edwin Torres Jun 05 '14 at 20:51

0 Answers0