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 / General / June 2006

Tip: Looking for answers? Try searching our database.

JTree application

Thread view: 
giangiammy@gmail.com - 26 Jun 2006 08:54 GMT
Hi all,

I'm using a JTree to visualiza an XML file:
I create the various node with:

new DefaultMutableTreeNode("THIS IS: string to show");

I'D like to have the string separated in 2 parts,
"THIS IS" should be bold face, while
"string to show" is normal face.

Is there a way to divide that string and have it shown
with different fonts.

Or is there some other widgets to use?

(Eventually, if you have a link to an example)

thanks
giammy
Andrew T. - 26 Jun 2006 09:56 GMT
..
> new DefaultMutableTreeNode("THIS IS: string to show");
>
[quoted text clipped - 4 lines]
> Is there a way to divide that string and have it shown
> with different fonts.

Most Swing components accept HTML, so you might try
something like..

 new DefaultMutableTreeNode(
   "<html><body><b>THIS IS:</b> string to show");

HTH
Andrew T.
Rogan Dawes - 26 Jun 2006 10:06 GMT
> ..
>> new DefaultMutableTreeNode("THIS IS: string to show");
[quoted text clipped - 14 lines]
> HTH
> Andrew T.

That would work as a hack, but I suspect that you really want to use a
TreeCellRenderer to do the job properly. Not meaning to dismiss Andrew's
approach, which I use below, but because I suspect that you actually
want to do something a bit more complicated than you are actually asking.

For example, extending DefaultTreeCellRenderer, you could create your
nodes using

new DefaultMutableTreeNode("string to show");

and render it with

public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean
hasFocus) {

    JLable label = (JLabel) super.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
    label.setText("<html><body><b>THIS IS:</b> " + value.toString());
    return label;
}

This is more of a building block to get you started building your own
more customised renderers.

Regards,

Rogan
giangiammy@gmail.com - 26 Jun 2006 11:09 GMT
Hi,

and thanks for the answers:
I tested both solution: the first (just put html in element node)
did not work (probably the Jtree widget do not support it - I
do not know)

Anyway with the second solution a defined MyRenderer and
works correctly: by now I just made  it bold, but probably
I will add some other modification

thanks
giammy


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.