File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Truncation of text in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Truncation of text in JTree" Watch "Truncation of text in JTree" New topic
Author

Truncation of text in JTree

John Forsyth
Greenhorn

Joined: May 30, 2007
Posts: 9
I'm using a JTree that can contain long (30+) character labels. I programmatically expand and select a node in the tree. Randomly, the JTree will truncate a label and add an ellipsis (...). I've tried using the default cell renderer as well as a custom one, yet it still truncates. I've tried setting the preferred width dynamically, but sometimes it's not set, or I have to add an enormous amount of pixels to resolve it (100-250). I've also tried calling the model's nodeChanged method.

However, when I do not expand the tree programmatically, the text does not truncate.

Is there a way to stop the truncation?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
John Forsyth wrote:I'm using a JTree that can contain long (30+) character labels. I programmatically expand and select a node in the tree. Randomly, the JTree will truncate a label and add an ellipsis (...). I've tried using the default cell renderer as well as a custom one, yet it still truncates. I've tried setting the preferred width dynamically, but sometimes it's not set, or I have to add an enormous amount of pixels to resolve it (100-250). I've also tried calling the model's nodeChanged method.

However, when I do not expand the tree programmatically, the text does not truncate.

Is there a way to stop the truncation?

Are you programmatically expanding the tree from the EDT, or from another thread?

If it's not an EDT issue, then it's hard to know what to suggest without seeing any of your code. (I guess you could try setLargeModel(true) to see if it makes a difference.)

bitguru blog
John Forsyth
Greenhorn

Joined: May 30, 2007
Posts: 9
Brian Cole wrote:
John Forsyth wrote:I'm using a JTree that can contain long (30+) character labels. I programmatically expand and select a node in the tree. Randomly, the JTree will truncate a label and add an ellipsis (...). I've tried using the default cell renderer as well as a custom one, yet it still truncates. I've tried setting the preferred width dynamically, but sometimes it's not set, or I have to add an enormous amount of pixels to resolve it (100-250). I've also tried calling the model's nodeChanged method.

However, when I do not expand the tree programmatically, the text does not truncate.

Is there a way to stop the truncation?

Are you programmatically expanding the tree from the EDT, or from another thread?

If it's not an EDT issue, then it's hard to know what to suggest without seeing any of your code. (I guess you could try setLargeModel(true) to see if it makes a difference.)


I was just populating the tree then calling
treeView.setSelectionPath(getPath(node));
treeView.scrollPathToVisible(getPath(node));

However, I added the call to setLargeModel(true), and so far, it seems to have stopped the truncation. Thanks!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
John Forsyth wrote:
I was just populating the tree then calling
treeView.setSelectionPath(getPath(node));
treeView.scrollPathToVisible(getPath(node));

However, I added the call to setLargeModel(true), and so far, it seems to have stopped the truncation. Thanks!


I'm glad that it's working to your satisfaction, but from which thread are you calling the setSelectionPath() method? If you print the value of SwingUtilities.isEventDispatchThread() from the same spot, for example, do you see true or false?
John Forsyth
Greenhorn

Joined: May 30, 2007
Posts: 9
Brian Cole wrote:
John Forsyth wrote:
I was just populating the tree then calling
treeView.setSelectionPath(getPath(node));
treeView.scrollPathToVisible(getPath(node));

However, I added the call to setLargeModel(true), and so far, it seems to have stopped the truncation. Thanks!


I'm glad that it's working to your satisfaction, but from which thread are you calling the setSelectionPath() method? If you print the value of SwingUtilities.isEventDispatchThread() from the same spot, for example, do you see true or false?


I am not setting the selection path from the event dispatch thread, or any additional threads. Calling SwingUtilities.isEventDispatchThread() is false.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
John Forsyth wrote:
I am not setting the selection path from the event dispatch thread, or any additional threads. Calling SwingUtilities.isEventDispatchThread() is false.


Well setSelectionPath() and scrollPathToVisible() are intended to be called from the EDT, as are all Swing methods except for a handful that are documented to be thread-safe.

I can't tell without seeing your code, but I suspect that had you used something like invokeLater() to do so, that you wouldn't have had problems.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

I've been seeing this sort of thing recently in my own Swing program which I first wrote several years ago and have been working on ever since. All of a sudden when I change a text field, it doesn't expand to display the whole text. If I minimize the application and then restore it, then magically it has expanded enough. Usually.

And this is code that I wrote several years ago and haven't changed. I think these glitches started to happen after I started to use a fairly recent version of Java 6 but I'm not really sure. There are other glitches, like things happening in strange orders so my calls to JButton.setEnabled, for example, don't happen at the right time. If I'm debugging then they do happen at the right time. If I have the code write something to System.out then they do happen at the right time. If it's within half an hour of high tide in Manila then they do happen at the right time, but maybe not. As I say this was working right for several years but now it isn't.

Normally I would say "Bugs? Look at your own code." But in this case (and maybe in your case too) I'm not so sure.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Truncation of text in JTree