Search code examples
javaswingeditorjtextcomponent

how to fit text within jtextcomponent


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.


Solution

  • 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.