-1

I am trying to develop an editor without scrollbars using jtextarea, so if the text is too long to fit within jtextarea it should be splitted into smaller substrings which could be edited within the jtextarea without showing vertical scrollbar because it is easy to get rid of horizontal one by using setLineWrap and setWrapStyleWord. I have tried to use vertical scrollbar adjusmentListener to listen to its changes but it doesnt work because jtextarea append and setText methods activated at the end of business logic, so I tried to use multithreaded and SwingWorker to invoke these methods but also doesnt work. I also tried to invoke repaint, revalidate and update methods with no hope. please help me to pass this issue and thanks in advance.

Mat
  • 202,337
  • 40
  • 393
  • 406
muaz
  • 550
  • 9
  • 15

3 Answers3

1

I am trying to develop an editor without scrollbars using jtextarea, so if the text is too long to fit within jtextarea it should be splitted into smaller substrings which could be edited within the jtextarea without showing vertical scrollbar because it is easy to get rid of horizontal one by using setLineWrap and setWrapStyleWord.

I'm not sure I fully understand this. Perhaps you can explain further? Why not place the JTextarea inside of a JScrollPane? What is your desired behavior if the text it contains is greater than that which the JTextArea can display?

I have tried to use vertical scrollbar adjusmentListener to listen to its changes but it doesnt work because jtextarea append and setText methods activated at the end of business logic, so I tried to use multithreaded and SwingWorker to invoke these methods but also doesnt work. I also tried to invoke repaint, revalidate and update methods with no hope.

If you want to trap entered text before it is committed to the text component, consider setting a DocumentFilter to the JTextArea's associated Document (a PlainDocument).

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you very much *Hovercraft Full Of Eels* for replying, dear I dont want JScrollPane to be shown because it is a pre-requirement of the homework. – muaz Jan 15 '12 at 03:05
  • However; to be clear about my question that if the String which I need to set or append to a JTextArea is too long and it will cause displaying the vertical Scrollbar, this String must be divided into shorter substrings – muaz Jan 15 '12 at 03:06
  • each of them can be set to the JTextArea without displaying the vertical ScrollBar, and show them successively within the JTextArea in response to an event such as pushing a button , so does the DocumentFilter help me with this??? thanks – muaz Jan 15 '12 at 03:06
  • How is the String that is to be displayed in the JTextArea obtained? Does the user type it in? Does it come from a file? A Socket? – Hovercraft Full Of Eels Jan 15 '12 at 03:08
  • I get the String from xhtml files by inputstream and append or set them into jTextArea after parsing, and as I mentioned to Mr. Robin this must take the font size changes and window resizing in the consideration, so if I can get the maximum char counts of the AbstactDocument without scrolling the problem will be solved. thanks Eels – muaz Jan 15 '12 at 20:53
1

As Hovercraft Full Of Eels already suggested, you have to take a look at the Document, DocumentFilter and/or DocumentListener interfaces.

By adjusting those you have full control over what text is displayed when somebody wants to append text. You could for example remove the first part of the text before/after appending new text, hence limiting the number of lines/characters in the document.

The Swing tutorial about textcomponents contains such an example in the Implementing a DocumentFilter section, where the DocumentSizeFilter class is the one you are looking for (not part of the JDK, part of the Swing tutorial code). As already suggested as a comment in that sample code, it would be an option to remove the first part of the document when appending new text which would make the contents too long, but I leave that up to you since it is tagged as homework.

Robin
  • 36,233
  • 5
  • 47
  • 99
  • Thank you for replying Robin, but DocumentSizeFilter needs to specify maximum character count manually, so if the window is resized or font size is changed the ScrollBar will appear again, I need to get the maximum displayed character count allowed without appearance of ScrollBar and all problems will disappear, thanks alot – muaz Jan 15 '12 at 20:19
  • BTW this is a documents reader as term's project rather than a homework ;) thank you Robin – muaz Jan 15 '12 at 20:24
  • I suggest to look at the getters available on the `JTextArea` (`getRows`, `getColumns`, ... ). I am pretty sure you can figure something out to avoid a hard coded maximum character count and support a text area with a dynamic size – Robin Jan 15 '12 at 21:13
0

Thank you both Hovercraft and Robin I got it, your answers trend me to the correct way, I found the accurate answer at "Core Swing: advanced programming" book by using JTextArea.modelToView() passing the JTextArea Document Length as parameter this method returns a Rectangle object whose coordination represent the final character coordination and then compare these coordination with the bottom corner of the JTextArea to see if this final char reachs to this bottom or not.

muaz
  • 550
  • 9
  • 15