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:
I'm filling the JList
s 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.print
s 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 JList
s, 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?