Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ian Z
14,584 PointsProject i completed after what i learned from treehouse AND What is going on in my .toString() method? pics included
Heres project i made, it stores all the books in my imaginary bookstore and lets me search based on some properties. I made it in netbeans with Swing GUI
Theres one thing i dont understand and thats the .toString() used to print books to the Text Area as you can see in this picture SOMETIMES the text listens to the \t and sometimes it doesnt, and sometimes it ignores the difference in title length and sometimes it dosent, any ideas whats going on? Also the \t dosent work at all after author and price field
heres code for .toString()
public String toString( ){
String string = "Title: " + title +",\t \t"+ "Author: " + author + ",\t"+
"Price: " + price + ",\t"+ "Is a Hardcover: " + isHardcover + ",\t"+ "ISBN: " + ISBN;
return string;
}
and it is called here when "Show All Books" button is pressed
textArea.setText("");
textArea.setText("Our Library contains: \n");
textArea.append(bs.toString());
Edit: heres more pics of this Strange phenomena
And another pic
Ian Z
14,584 Pointsyour right ideally ill probably change it to columns with labels or something- but isnt it weird how it ignores the difference in length of some titles and sets the Author in the perfect location and on others it doesnt? its just Alien and makes no sense to me, it should be very slightly jagged with first couple of lines and not so perfect
Ian Z
14,584 PointsEdit: added more Pics Also: this happens not only in Swing JTextArea but also in console
1 Answer
Craig Dennis
Treehouse TeacherWhoa Ian! So awesome! Please share the code when you are happy with it!
One thing we haven't talked about yet is that String.format can do padding:
java> String.format("%-20s %-20s", "Column 1", "Column 2");
java.lang.String res1 = "Column 1 Column 2 "
Here's the docs on Formatters
Ian Z
14,584 PointsCool i didnt know about formatters and this helped me to get it working, however i still had to use the /t to make everything perfectly straight,
from what i read online the /t will automatically push the text to the next tab marking? so the page is split up into /t /t /t /t and i just formatted them all to be big enough so they snapped to same T
Ben Jakuben
Treehouse TeacherAwesome work, Ian! Thanks for sharing! :)




Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsOK - so you build up a long string with tabs inserted to try to maintain a column-like format. But some titles and authors etc. are longer than others so the tabs are overflowing and breaching the column alignment?
There's probably a neater way of doing that with some interpolation, but this is doing the job, isn't it?
Steve.