View unanswered posts | View active topics It is currently Sun May 28, 2017 9:33 pm



Reply to topic  [ 7 posts ] 
 Layout problem when using JTextPane 
Author Message

Joined: Wed Feb 22, 2012 11:55 am
Posts: 9
Post Layout problem when using JTextPane
Hi,

I have a layout problem when using JTextPane in a certain hierarchy, as illustrated in the attached testcase.

The problem is the following: the panel containing the JTextPane is initially laid out with some extra height. In my testcase, the extra height appears in green, since the panel has a green background color. As soon as there’s a re-layout (e.g. after maximize/minimize, or resize, etc.) this extra height disappears.

The problem also appears if you maximize the frame in one shot instead of gradually resizing the frame: what happens is that although the height of the JTextPane shrinks given that the text now fits on a single line, the parent panel preserves its previous height leading to extra space. The desired behavior would be for the parent panel to update its size as well.

This is most probably due to the wrapping feature of JTextPane, but how can we workaround it?

Thanks a lot,
Shant


Attachments:
MigTest.png
MigTest.png [ 24.99 KiB | Viewed 11653 times ]
MigTest.java [1.53 KiB]
Downloaded 504 times
Tue May 01, 2012 6:11 pm
Profile

Joined: Wed Feb 22, 2012 11:55 am
Posts: 9
Post Re: Layout problem when using JTextPane
Hi again,

There's something I didn't mention in my previous post: this problem of extra height on initial layout becomes even more serious when I launch my frame in the AWT thread using SwingUtilities.invokeAndWait(), as illustrated in the attached testcase.

By more serious I mean: this problem is not always reproducible if the frame is launched in the main thread, and even when reproducible, as in the testcase attached in my previous post, the extra height is small, and always the same. Whereas if the frame is launched in the AWT thread, the extra height that appears is not small and not always the same, it seems to be function of the amount of wrapping done in the text component.

I don’t have any knowledge in threads, so I have absolutely no clue why there should be any difference in the layout behavior if I launch my thread in the main thread or the AWT thread.

Sorry if this is not relevant here, but I also noticed that if the frame is launched in the AWT thread, the caret (cursor) of the text component is initially blinking, which is not the case if the frame is launched in the main thread.

The only thing I can guess is that there’s an extra re-layout happening in the main thread making the problem disappear, given that, as I mentioned in my previous post, the extra height disappears as soon as there’s a re-layout.

Thanks in advance,
Shant


Attachments:
MigTest.java [1.63 KiB]
Downloaded 509 times
MigTest.png
MigTest.png [ 25.66 KiB | Viewed 11640 times ]
Thu May 03, 2012 12:14 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Layout problem when using JTextPane
Hello,

Layouts in layouts, especially in JScrollPanes are always troublesome in Swing.

One thing is that width 99% is never good since that mean that the preferred size, which is calculated before the parent gotten its size, is never the same later when the actual layout is done.

Further, in Swing one need to set wmin 0 or the Swing components derived from JTextComponent will modify its minimum width to the current width (this is a bug/snag in Swing that will never be fixed).

Cheers,
Mikael


Fri May 04, 2012 1:24 pm
Profile WWW

Joined: Wed Feb 22, 2012 11:55 am
Posts: 9
Post Re: Layout problem when using JTextPane
Hello,

Thank you for your reply.

Quote:
One thing is that width 99% is never good


First of all, let me explain why I used the layout hint "w 99%": basically what I want is "w 100%", however "w 100%" has the following behavior (as illustrated in the attached image) which I don't want:
- I run my frame,
- I maximize it and hence the JTextPane increases in width and wrapping is adjusted accordingly,
- when I minimize back, the JTextPane preserves its width and wrapping is hence not modified, instead the horizontal scrollbar will appear.

The "w 99%" appears visually as a "w 100%" (since the 1% is hardly noticed), and gives the desired behavior: when I minimize back, the JTextPane shrinks back, and wrapping is adjusted accordingly.

To describe it again, the desired behavior is:
- the text component fills all the space available in its parent horizontally, and if not all text can fit in one line, the text should wrap vertically
- whenever there's more/less width available, wrapping should adjust accordingly

So far with my application constraints (i.e. the hierarchy of Swing components JFrame > rootPanel > scrollPane > panel > textPanel > JTextPane which I have to stick with) "w 99%" is giving me this behavior, but if this is not the best way to achieve it, please advise.

Quote:
in Swing one need to set wmin 0 or the Swing components derived from JTextComponent will modify its minimum width to the current width


As for the minimum width bug in Swing, yeah I'm familiar with that bug, but I don't see why it is relevant in my case. I'm saying this because here, say I initialize the width of my frame to 300: I DON'T have the problem: the text component sets its minimum width to the current width of 300, and hence I cannot shrink the width of my text component below 300. If I had that problem, then I would set wmin to 0 as you're suggesting.

My problem here, as I already described in my previous posts, is the extra height that appears under certain conditions:
- initially - depending on many variables (e.g. some initial panel dimensions, and text length do not reproduce the problem)
- when running in the AWT thread using SwingUtilities.invokeAndWait() as opposed to the main thread (any feedback on this?)
- if you maximize the frame in one shot, etc.

Thanks,
Shant


Attachments:
MigTest.png
MigTest.png [ 56.18 KiB | Viewed 11619 times ]
Tue May 08, 2012 4:11 pm
Profile

Joined: Wed Feb 22, 2012 11:55 am
Posts: 9
Post Re: Layout problem when using JTextPane
Hi,

Any updates on this?

Thanks,
Shant


Tue May 15, 2012 9:23 am
Profile

Joined: Wed Feb 22, 2012 11:55 am
Posts: 9
Post Re: Layout problem when using JTextPane
Hi,

Gentle reminder of my problem here: the JPanel containing the JTextPane leaves some extra height under certain conditions:
- initially
- when running in the AWT thread using SwingUtilities.invokeAndWait() as opposed to the main thread
- if you maximize the frame in one shot, etc.

Thanks again,
Shant


Mon May 21, 2012 12:05 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Layout problem when using JTextPane
Hello,

As mentioned % doesn't really work well with components that has their height depending on the width. The reason is that the value for % can't not be calculated in the "request for preferred size" initial phase since it depends on the parent. This even gets more unpredictable if you put everything in a scroll pane. This is a shortcoming of Swing and nothing can be done about it. The JTextComponents are hardwired to hack this and that hack makes it impossible to work around the issue.

Cheers,
Mikael


Mon May 21, 2012 4:39 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.