Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:22:07+00:00 2026-06-04T04:22:07+00:00

Is it possible to add some blank space between node rows in a JTree?

  • 0

Is it possible to add some blank space between node rows in a JTree? I am using custom images for the node icons and I guess the images are larger than the standard node icons, so the node icons sit very close together. It would look nicer if there was a bit of separation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:22:07+00:00Added an answer on June 4, 2026 at 4:22 am

    To add an actual spacing between tree nodes you will have to modify the UI and return a proper AbstractLayoutCache successor (by default JTree uses two classes, depending on the row height value: FixedHeightLayoutCache or VariableHeightLayoutCache).

    The easiest way to add some spacing between nodes is to modify renderer, so it will have some additional border, for example:

    public static void main ( String[] args )
    {
        JFrame frame = new JFrame ();
    
        JTree tree = new JTree ();
        tree.setCellRenderer ( new DefaultTreeCellRenderer ()
        {
            private Border border = BorderFactory.createEmptyBorder ( 4, 4, 4, 4 );
    
            public Component getTreeCellRendererComponent ( JTree tree, Object value, boolean sel,
                                                            boolean expanded, boolean leaf, int row,
                                                            boolean hasFocus )
            {
                JLabel label = ( JLabel ) super
                        .getTreeCellRendererComponent ( tree, value, sel, expanded, leaf, row,
                                hasFocus );
                label.setBorder ( border );
                return label;
            }
        } );
        frame.add ( tree );
    
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        frame.setVisible ( true );
    }
    

    This is still a bit harder than just setting the static row height (as Subs offered to you in comment), BUT it is better due to different possible font sizes and styles on various OS. So you won’t have size problems anywhere.

    By the way, you can also change the node selection representation the way you like, that way you can even fake the spacing visually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to add some black space between views that are shown by
Is it possible to add icons to all/some of the menu-links in your account-dashboard?
I used glade to create some gtk buttons. is it possible to add an
Is it possible to add some kind of restriction to the web.config to limit
Is it possible to add some value from resource file right into the XAML
Is it possible to add some kind of properties to a Wordpress page? In
Is it possible to add some files form a directory to an IDEA project
Is it possible to add some additional attributes to my components which are then
Is it possible to add some kind of an event/handler when an element is
Is it possible to add messages to the built-in error console of Firefox from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.