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 8072179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:56:09+00:00 2026-06-05T13:56:09+00:00

I have created a custom JTree. That tree could be filtered to show only

  • 0

I have created a custom JTree. That tree could be filtered to show only those nodes that contains given criteria (string). Now, is there any way to bold only that part of DefaulMutableTreeNode that contains searched string?

If a tree node has label = “StackOverflow” and user is searching for nodes that contains “Stack”, “StackOverflow” node would be rendered with “Stack” part bolded.

How to achieve that?

  • 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-05T13:56:11+00:00Added an answer on June 5, 2026 at 1:56 pm

    You need to set your own TreeCellRenderer.

    Example:

    The code below produces this screenshot:

    screenshot

    final JTextField field = new JTextField();
    final JTree tree = new JTree(new String[] {"Hello World", "StackOverflow"});
    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        @Override
        public Component getTreeCellRendererComponent(JTree tree,
                Object value, boolean sel, boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
    
            String search = field.getText();
            String text = value.toString();
    
            StringBuffer html = new StringBuffer("<html>");
            Matcher m = Pattern.compile(Pattern.quote(search)).matcher(text);
            while (m.find())
                m.appendReplacement(html, "<b>" + m.group() + "</b>");
            m.appendTail(html).append("</html>");
    
            return super.getTreeCellRendererComponent(
                    tree, html.toString(), sel, expanded, leaf, row, hasFocus);
        }
    });
    field.addKeyListener(new KeyAdapter() {
        @Override public void keyReleased(KeyEvent e) { update(); }
        private void update() { 
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
            model.nodeStructureChanged((TreeNode) model.getRoot());
        }
    });
    JFrame frame = new JFrame("Test");
    frame.add(field, BorderLayout.NORTH);
    frame.add(tree, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
    

    • Java Swing Tutorial: How to Use HTML in Swing Components
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created custom listview in that have list of textview & list of
I have created Custom Dialog for my application. While i run that application in
I have created a custom attribute that I am using on my class MyClass
I have created a custom AppleScript that works, and placed it in the folder:
I have created a custom validation rule (uniqueUserName). The problem I'm having is that
We have created custom listview by using the Base adapter. List view contains Text
Have created a custom navigation menu in wordpress that has some pages and some
i have created custom Cell in a controller CustomCellController. The custom Cell Contains a
I have created a custom module that creates a custom Database table. I have
I have created custom post types that also have custom meta_boxes I've created. Currently,

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.