last day (18 days later) » 

2:49 AM
0
A: Java JTextPane add emty space to JScrollPane

VGRYou can set the JTextPane's margin, based on the bounds of its enclosing JViewport and the last line of its document: static void updateMargin(JTextPane textPane) { JViewport viewport = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, textPane); if (viewport != nul...

 
Hmm.. this is in deed smart. This way I don't have to fiddle around with fontHeights... But I had to do a slight modification to your solution. I lied about the TextPane. There are in fact two JTextPanes inside a JPanel inside a JScrollPane. so at the end I apply the offset to the JPanel and not the JTextPane. And also since margin doesn't work on JPanels I used EmptyBorder. The result is this: it works but the last line is allowed to go out of the viewport by a few pixels. Is it because I use border?
 
VGR
I dont think the use of a Border would cause a problem. I'm guessing you need to account for the existing margins of one or both of your JTextPanes. If the JTextPanes are side by side, you might want to do the modelToView calls on each of them, and create a union of those two Rectangles.
 
The second JTextPane is for the Line Numbers (See pic in OP) so it should be fine with calculating only the main TextPane... But still it is off my few pixels, which results in offsets up to one line with extreme big/small font sizes. What Am I missing. PS I am writing a "TestBed", basically a mockup version of my application to post here so you can see what is going.
Good news is: Your code works. I tested it in my snippet app and it works like a charm. (Will update OP with the working code.) Bad news is that I now have to find the bug in my real application, which is going to be a pain in the )). If you wish you could have a peek in here github.com/Haeri/MonolithTextEditor and maybe find out what the problem is...??
PS: Since you are clearly a genius, may I ask a second question that buggs me for a long time: In the code you will see that the Line number TextPane is set BorderLayout.LINE_START. This makes it possible to scroll it out of the view in the x-axis.. Is there a way to prevent this from happening?
Never mind. Found it out. It was because I was always adding an extra line, so it was actually working, but the last line was always empty. But the x-scroll problem still remains...
 
VGR
Rather than placing both JTextPanes in a single panel, I would make the line number JTextPane the JScrollPane's row header view.
 
But this would break the y-Scroll link to the main TextPane..
 
VGR
2:49 AM
I don't think it would. A JScrollPane automatically synchronizes the vertical scrolling of the row header with the scrolling of the main viewport's content.
 
oh... I know whats wrong.. Its because the Line TextPane and the actual scrollpane have different heights because of that empty space we just inserted xD.. But how to fix this.
 
VGR
Since they are both JTextPanes, you can give them both the same margin. Or put each of them in a JPanel with the same empty border.
 
I am now adding the border to both textpanes. this works now. But now a new problem appears.. the click area to focus on the text has been reduced.. Can I make an actionlistener on an emptyborder to react on clicks??
 
VGR
You can add a MouseListener to the JPanel which has the border, and in the listener's mouseClicked or mousePressed method, call textPane.requestFocusInWindow(). (But you wouldn't have to do that if you set the JTextPane's margin instead of using a JPanel and empty border.)
 
I changed it to use margin, but for some reason it doesn't work.. The margin itself is the right value but it does not expand the scrollpane like border does...
 
 
12 hours later…
VGR
2:44 PM
Is the JTextPane itself the scrollpane's viewport view? Or is it still inside a JPanel?
 
 
6 hours later…
9:10 PM
In the sample code I posted I implemented this first inside the JPanel and after that without the JPanel(JtextPane directly bound to scrollPane) The margin didn't seem to affect the scroll pane. (with border it worked...)
 

  last day (18 days later) »