Reputation: 11
I need my program to wrap lines after a certain number of characters, like in Microsoft word. The program is supposed to take large text and convert it to all upper or lower. It ends up running off the end of the screen and I can't afford a bigger one.
All I currently have is one that wraps after characters, possibly in the middle of a word, which ends up looking weird.
int linecount = 0;
for(int i = 0; i < ask.length(); i++)
{
askformat += ask.charAt(i);
linecount++;
if (linecount==50)
{
askformat += '\n';
linecount = 0;
}
}
Upvotes: 0
Views: 163
Reputation: 1050
If it is a GUI application and you are using a JTextArea, you simply need to add the following line:
jta.setLineWrap(true)
jta.setWrapStyleWord(true)
Where jta is your JTextArea instance.
See Java API.
EDIT: Added setWrapStyleWord
as per @BackSlash's suggestion, which is more appropriate if the text being entered is expected to have whitespace breaks, like real text. This is the same way that a word document would wrap. For clarification, the difference is that just setLineWrap
will add new lines after any character boundary (so the lines will split, when the full width of the area of the JTA is filled irrespective of if it is mid word or not) whereas with setWrapStyleWord
it will add new lines at whitespace boundaries, meaning that the text will be wrapped after a word (unless there is no whitespace on the line in whichcase it, will exhibit the same functionality as setLineWrap
)
Upvotes: 3