Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

Shortend text in JTree

Thread view: 
Carsten Zerbst - 01 Jun 2005 10:41 GMT
Hello,

sometimes I need to display objects with rather long
names in the JTree. Unfortunately my strings are abbreviated
and "..." is appended. My TreeCellRenderer is based
on the DefaultTreeCellRenderer, but I do not find the
reason for the abbreviation.

Does anybody knows how is shortening my text ?

Thanks, Carsten
Roland - 01 Jun 2005 23:57 GMT
> Hello,
>
[quoted text clipped - 7 lines]
>
> Thanks, Carsten

I've never experienced abbreviated tree node labels.
Can you show a short self contained compilable example demonstrating
this behaviour.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Andrew Thompson - 02 Jun 2005 03:25 GMT
> Can you show a short self contained compilable example demonstrating
> this behaviour.

For more details on preparing an SSCCE..
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Bent C Dalager - 02 Jun 2005 13:16 GMT
>> Does anybody knows how is shortening my text ?

JLabel does this when there is not enough room to display the entire
string (and the default renderer uses JLabel I think). My guess is
that your tree decides how much room is available to the tree node at
some early point. Later, more room is made available but the tree
doesn't notice and so when the node is drawn, the JLabel thinks it has
a lot less room than what is actually the case. Or perhaps you first
set one string that is short and then one that is long and it hangs on
to the preferred size of the first one for some reason.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Carsten Zerbst - 03 Jun 2005 12:46 GMT
Am Wed, 01 Jun 2005 11:41:20 +0200 schrieb Carsten Zerbst:

> sometimes I need to display objects with rather long
> names in the JTree. Unfortunately my strings are abbreviated
> and "..." is appended.

After some tinkering, I (hopefully) found the reason for
the behaviour.

In my TreeCellRenderer I use the DefaultTreeCellRenderer
by delegation. In most cases I just use the returnvalue
of the DTCR, but in certain cases I override certain
attributes in the returnvalue like  text or icon.

In those cases the preferred dimensions have to be updated,
otherwise the text is abbreviated.

By the way I recommend to take a look at https://datatips.dev.java.net/ :-)

Some code:

-----------------
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected,
       boolean expanded, boolean leaf, int row, boolean hasFocus ) {
       Component retval = null;

    //    .. use DefaultTreeCellRenderer by delegation

    //    ... update some attributes
   
       if ( retval instanceof JLabel ) {          
           JLabel lab = ( JLabel ) retval;
           int width = 20;
           if ( lab.getIcon(  ) != null ) {
               width += lab.getIcon(  ).getIconWidth(  );
           }

           if ( lab.getText(  ) != null ) {
               width += SwingUtilities.computeStringWidth( lab.getFontMetrics( lab.getFont(  ) ),
                   lab.getText(  ) );
           }

           Dimension nd  = new Dimension( width, (int) lab.getPreferredSize().getHeight());            
           lab.setPreferredSize( nd );
       }

    return retval;
}
------------
Andrey Kuznetsov - 06 Jun 2005 21:38 GMT
> By the way I recommend to take a look at https://datatips.dev.java.net/ 
> :-)

or at
http://jgui.imagero.com/doc/com/imagero/gui/swing/TLToolTipManager.html

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Karsten Lentzsch - 20 Jun 2005 13:47 GMT
> sometimes I need to display objects with rather long
> names in the JTree. Unfortunately my strings are abbreviated
[quoted text clipped - 3 lines]
>
> Does anybody knows how is shortening my text ?

Likely you've changed the tree node's label
and have not notified the tree views (JTree)
about that change. That can and should be done
by firing an appropriate TreeModelEvent.

See TreeModelListener#treeNodesChanged and
DefaultTreeModel#nodesChanged or
DefaultTreeModel#fireTreeNodesChanged

Best regards from Kiel (Week),
Karsten Lentzsch


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.