0

I'm a bit confused on how the JTextArea works with regards to newline characters. I've got a JTextArea within a JScrollPane object.

JScrollPane scrollPane4 = new JScrollPane();
scrollPane4.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane4.setBounds(66, 155, 474, 133);
getContentPane().add(scrollPane4);

postingArea = new JTextArea();
postingArea.setMaximumSize(new Dimension(470, 200));
postingArea.setLineWrap(true);
postingArea.setBorder(border);
postingArea.setLineWrap(true);
postingArea.setWrapStyleWord(true);

scrollPane4.setViewportView(postingArea);

The text area is used to gather input from the user and post to an SQL database. The string can be read from the database and redisplayed on a separate web page at another time. When a user enters text, the line does wrap, however, what gets entered into the database is one long string. Therefore, what is redisplayed is one long string with a horizontal scroll bar. Is there a way for me to add newline characters at the appropriate location in the text via an event handler? Or, do I simply inform the user to press "Enter" when they want a new line?

hichris123
  • 10,145
  • 15
  • 56
  • 70
rrirower
  • 4,338
  • 4
  • 27
  • 45
  • 1
    don't use `setBounds` rely in layoutManagers, I'd use another JTextArea constructor with `rows` and `columns` – nachokk Mar 22 '14 at 17:51
  • @nachokk But, does your suggestion resolve the problem I'm having. Or, is that your preference? – rrirower Mar 22 '14 at 18:31
  • `scrollPane4.setBounds(66, 155, 474, 133);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components **for a robust GUI,** instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). And before you ask *"Or, is that your preference?"* read the **bold** part again. – Andrew Thompson Mar 23 '14 at 00:57

2 Answers2

1

I don't see where your TextArea is being added within your ScrollPane. If it were truly being added, the code would have looked like:

JScrollPane scrollPane4 = new JScrollPane(postingArea);

And make your TextArea as:

JTextArea postingArea = new JTextArea(ROWS, COLUMNS); 

Where ROWS and COLUMNS are integers specifying the number of rows and columns in the TextArea.

And don't use setBounds() method, but rely on the LayoutManager to do its work as said by @nachokk. Its not a preference, but good practice. Also, since the LayoutManager will do its work, regardless of you setting your component's bounds, if you LayoutManager doesn't consider the custom bounds parameters, it will calculate it on its own, and that's why you are having the issue.

The rest of your code looks fine.

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
  • Sorry for the confusion. I had left a line of code off and just edited the post. – rrirower Mar 22 '14 at 19:02
  • Alright, so in your code you're trying to set the maximum size of your textArea. Did you try the `setPreferredSize` and `setMinimumSize` methods? Because then the layout manager will acknowledge those values and show a text area embedded in the scroll pane properly. Try it and let us know. – Aman Agnihotri Mar 22 '14 at 19:13
  • I tried both setPreferredSize and setMinimumSize for the JTextArea. I also neglected to mention that I am using the Swing (GUI) Designer to change the properties. I don't know if that's significant. But, after changing the values, I still get one long string of text posted to my database. – rrirower Mar 22 '14 at 19:50
  • Ah, I get it now, you're getting a big long string in your database, because the wrapping of the text in your text area is just visual wrapping. It has nothing to do with the actual string which the text area holds. And yes you can add newline characters `\n` at appropriate locations in your string to really add some new lines in your string, which can then be posted to your database. I can help you create such method if you let me know how many characters max each line should hold so that I can write a method which will modify your string by adding new line characters to it. – Aman Agnihotri Mar 22 '14 at 20:08
  • But I doubt if your database will consider the line separator as a newline in the view which you are seeing. If it considers line separators appropriately, then a method which adds appropriate line separators will have its use, or else it won't solve the issue as well. – Aman Agnihotri Mar 22 '14 at 20:10
  • Acutally, you've confirmed my suspicions. I guess I just assumed that JTextArea would handle adding the newlines for me since I was seeing the visual effect of it moving to a new line. I can certainly add a method to break the lines up if that's the way you think I should proceed. – rrirower Mar 22 '14 at 20:15
  • Yeah that's what you should do. In my haste I didn't think that the JTextArea wrapping is just a visual wrapping and has nothing to do with the data in it. I realized it when I read your issue the second time. Let us know whenever you've solved the issue or if we can help you any further with this. – Aman Agnihotri Mar 22 '14 at 20:17
0

I have resolved this by using the Utilities class. In particular, i called getRowEnd () in a loop to determine the logical end of the line (eg. the point of the word wrap) in the JTextArea. I then inserted the string <br> at that point. The updated string was then inserted into an SQL database and later retrieved and displayed on another page. The text displayed correctly with proper line breaks.

rrirower
  • 4,338
  • 4
  • 27
  • 45