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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:14:07+00:00 2026-06-03T19:14:07+00:00

Hi there :) I am using Java 6, to start with. Feature When you

  • 0

Hi there 🙂 I am using Java 6, to start with.

Feature

When you are using a JComboBox, you can usually select an entry by typing characters. Which is nice.

Problem

Unfortunately, this does not work, if your entries are HTML strings, e.g. "<html><b>foo</b> <i>bar</i></html>". I am using HTML mainly for showing my entries in different colors, by the way.

Solution, bad

So, to solve this I implemented my own KeySelectionListener by altering JComboBox$DefaultKeySelectionManager, basically ignoring all tags, if an entry starts with "<html>".

That works. But: I was wrong thinking that the one used by default is JComboBox$DefaultKeySelectionManager. Why? Because it only handles the first character typed. For long lists with many entries starting with the same char, this isn’t convenient at all.

The good one, handling all characters typed into the box is BasicComboBoxUI$DefaultKeySelectionManager (which is the default). The problem is, that this one interacts with the nesting ComboBoxUI. It takes its JList and calls getNextMatch(prefix, …). Sadly JList.getNextMatch() does not care about HTML entries.

I can’t simply extend BasicComboBoxUI to have a different JList implementation, because the given LAF decides which UI is used.

Any ideas? Java 7 doesn’t solve this, I guess?

  • 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-03T19:14:08+00:00Added an answer on June 3, 2026 at 7:14 pm

    This is indeed a pita, and I’ve been confronted to the same problem. In the end, the combo box uses the toString() method of the objects contained in the model for its key selection. The easiest way is to wrap all your HTML strings into objects which have a toString() method returning the text used for selection, and use a cell renderer to render the HTML. For example:

    public class HtmlOption {
        private final String rawText;
        private final String htmlText;
    
        private HtmlOption(String rawText, String htmlText) {
            this.rawText = rawText;
            this.htmlText = htmlText;
        }
    
        public String toString() {
            return this.rawText;
        }
    
        // getters, equals and hashCode omitted
    }
    

    And then fill your combo with these objects:

    model.addElement(new HtmlOption("foo bar", "<html><b>foo</b> <i>bar</i></html>"));
    

    And set a renderer to the combo box which does

    setText(((HtmlOption) value).getHtmlText());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way using Java to over-ride the browser authentication dialog box when
is there any example on using java method to call rhino-javascript function and return
I want city name from IP address using Java is there any idea to
I've been using java to parse numbers, e.g. (. Integer parseInt numberString) Is there
I am trying to create a GUI using java swing. From there I have
How to extract the path c:/documents and settings/user using Java... Is there any method??
I've been told that there is some overhead in using the Java try-catch mechanism.
Is there a way to list methods in a Java class using the JNI
I am using netbeans to code java and i was wondering if there is
Is there a way/tool to auto convert Java source code from using raw types

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.