Results 1 to 3 of 3
  1. #1
    msphelix is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default How do I scroll a JComboBox so that the selected item is at the top?

    Hi,
    I have a perfectly plain JComboBox except that it is an editable auto-complete JComboBox:
    every time the user types in the combo's text field, I search the combo and if I find an item that starts with what they typed, I select that item by using:
    Java Code:
    combo.getModel().setSelectedItem(theString);
    I also display the popup - the list - by using:
    Java Code:
    combo.showPopup();
    This is all working fine.

    My problem:
    The combo has the 50 states in it; when the user types Mi; Michigan is at the bottom of visible part of the popup; I want it at the top so that instead of seeing:

    Louisiana
    Maine
    Maryland
    Massachusetts
    Michigan

    the user sees

    Michigan
    Minnesota
    Mississippi
    Missouri
    Montana

    and can say "Oh, I want Mississippi." and select it, without finishing typing the name.

    How can I do this?
    Last edited by msphelix; 02-17-2017 at 05:06 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    24

    Default Re: How do I scroll a JComboBox so that the selected item is at the top?

    The default behavior is to scroll the list just enough to render the selected item visible. You could try selecting the first item in the combo and then select the item of interest.
    Java Code:
    combo.setSelectedIndex(0);
    combo.setSelectedItem(theString);
    Let us know whether that does it.

    db
    Last edited by DarrylBurke; 02-17-2017 at 10:52 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    24

    Default Re: How do I scroll a JComboBox so that the selected item is at the top?

    Wait, what did I say? No, try
    Java Code:
    combo.setSelectedIndex(combo.getItemCount() -1);
    combo.setSelectedItem(theString);
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. unselected item from selected list
    By prasathbala in forum NetBeans
    Replies: 0
    Last Post: 01-10-2012, 05:41 AM
  2. JComboBox-selected item
    By nenadm in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2011, 12:58 PM
  3. Why wont my JSP page display my selected item?
    By system32 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-01-2011, 04:02 PM
  4. How to get the selected item from combox
    By man4ish in forum AWT / Swing
    Replies: 5
    Last Post: 12-30-2009, 10:29 AM
  5. Replies: 2
    Last Post: 12-10-2009, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •