JComboBox: Trapping selection of items

I want to select items in a non-editable JComboBox by:
a) scrolling through and selecting with the mouse;
b) scrolling through with mouse or arrow keys and hitting [return];
c) using a KeySelectionManager to find an item be entering search characters then hit [return] or click with the mouse.
(a) seems to be the default behaviour, which is fine;
(b) arrows initially caused actionEvents to be fired, until I saw the tip on this site to use putClientProperty("JComboBox.lightweightKeyboardNavigation","Lightweight") - this now seems to work OK;
(c) is the problem.
I get an event every time I enter a character to search on, but I only want to know when the user selects an item with [return] or mouse-click. How and where should I trap these events to make this happen?
Sorry if this seems like a dumb question, but I'm new to Swing and I'm having trouble figuring out where and when to do all this event-handling stuff!
Thanks,
Chris

check out http://www.globalleafs.com 's download section. There are many programmes out there in java.

Similar Messages

  • How to select multiple items in JComboBox

    Is it possible to select multiple items in a JComboBox
    if yes, then how to do?
    If no , then is there any other way to acheieve this ?

    Hi
    ComboBoxModel extends ListModel and not ListSelectionModel, so i think JComboBox does not provide multiple selection. But u can try customizing ur combo box.. may be its possible.. not very sure
    Shailesh

  • Selecting the item when mouse wheel scrolled in JComboBox

    Hi All,
    I have 2 issues wrt JComboBox which are:
    1.Selecting the item when my mouse scroll wheel is rolled.
    2.Collapsing the expanded combobox ie. to initial state when i press ALT+Tab and revist the same window.It is like this..
    expand the combobox to with item selected and expanded.Now press alt+tab to see the same window then the combobx is still exapanded.Actulaly it is to be collapsed showing the seleted item only.
    Could any one pls help me out.
    cheers,
    sharath

    As regards problem 2 - do you want the combo to stay open, or to close when you Alt Tab, I can't quite tell from your question.
    If you want it to close, then I would say that is standard behaviour and I have never seen a Java (or any other) application that doesn't work like that, so if it doesn't work that way for you, then you must have coded it very oddly.
    Regards,
    Tim

  • Select same item (AGAIN) in JComboBox

    I don't know, if it's feature or bug :) But JCombobox do not "fire action" when I select same item again (value, which is already selected). But i need to call action ALWAYS when user select some item, even if he select already selected item. Anyone knows how to do it? Thanx

    Bug,... well, try this...
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class jComboBoxAction extends JPanel  {
    JComboBox jc;
        public  jComboBoxAction() {
          jc = new JComboBox();
          jc.addItem("France");
          jc.addItem("Germany");
          jc.addItem("Italy");
          jc.addItem("Japan");
          add(jc);
          jc.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            System.out.println(""+jc.getSelectedItem());
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new  jComboBoxAction());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }

  • Battling JComboBox behavior of removing items

    we are working on a document browser GUI and encountered some weird behavioral issue with JComboBox/DefaultComboBoxModel. The current workaround is ugly and we are wondering if there is somehow different way to get it to work.
    Our application uses an in-house class to keep a list of documents, and this class has APIs for navigation and deletion so forth. On the GUI, we display a JComboBox to show the name of the current document being viewed. This JComboBox is backed by a DefaultComboBoxModel which has a separate list containing only the names of the document. When a user selects a document from the JComboBox, the application jumps to the selected document, and when a user deletes a document, the corresponding document name is removed from the JComboBox.
    The problem is that our in-house class behaves differently than DefaultComboBoxModel. When an item is removed from the in-house class, it points to the next item on the list (i.e. the index value renames the same), unless the last item of the list is removed, and then the index simply points to the new last item of the list. This behavior is intuitive and is in conformation to user requirements.
    However, the DefaultComboBoxModel does the opposite with some oddity. When an item is removed from the ComboModel, the selectedIndex points to the previous item the list (i.e. the index value decrements by 1), unless it is the first item of the list, in which case it behaves even more oddly... it points to the next item in the list, but for some reason, the selectedIndex becomes 1 (instead of 0) as if the item to be deleted is still in the list!
    So we have been having trouble keeping the in-house class and the JComboBox in sync. For one thing, when a user deletes a document, it renders twice for the next document to be viewed (one triggered by JComboBox automatically points to the previous doc, which is incorrect, and the second one is our code that forces JComboBox to select the next doc by setting selected index), and problems occur especially for the boundary cases. We ended up doing a hack - add an ActionListener to the JComboBox only when it receives a mouse-down event and remove the actionListener inside of the actionPerformed() code, so that we can force the JComboBox to display the correct document name without triggering document rendition.
    But this is ugly. We are wondering if there is a better way. One possibility is of course to ditch the defaultComboBoxModel, and implement a mutableComboBoxModel in our in-house class. But is there another way (easier way so we dont have to deal with triggering events etc)? Thanks.

    And the way to solve it with a custom MutableComboBoxModel (also does not auto select an item that is added):
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class ListComboBoxModel extends AbstractListModel implements MutableComboBoxModel {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    ListComboBoxModel model = new ListComboBoxModel("one", "two", "three");
                    model.setSelectionChangeOnRemove(SelectionChange.SELECT_NEXT);
                    final JComboBox comboBox = new JComboBox(model);
                    comboBox.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("Selected: "
                                    + comboBox.getSelectedIndex()
                                    + "=" + comboBox.getSelectedItem());
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.getContentPane().add(comboBox, BorderLayout.PAGE_START);
                    frame.getContentPane().add(
                            new JButton(new AbstractAction("Delete") {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    int index = comboBox.getSelectedIndex();
                                    System.out.println("Before: " + index
                                            + "=" + comboBox.getSelectedItem());
                                    comboBox.removeItemAt(index);
                                    System.out.println("After: "
                                            + comboBox.getSelectedIndex()
                                            + "=" + comboBox.getSelectedItem());
                                    setEnabled(comboBox.getItemCount() > 0);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
        public enum SelectionChange { SELECT_PREVIOUS, SELECT_NEXT, CLEAR_SELECTION }
        private final List<Object> items;
        private SelectionChange changeOnRemove = SELECT_PREVIOUS;
        private Object selected;
        public ListComboBoxModel(Object...items) {
            this.items = new ArrayList<Object>();
            if(items != null) {
                this.items.addAll(Arrays.asList(items));
        public void setSelectionChangeOnRemove(SelectionChange changeOnRemove) {
            if(changeOnRemove == null) {
                throw new NullPointerException("SelectionChange should not be null");
            this.changeOnRemove = changeOnRemove;
        @Override
        public int getSize() {
            return items.size();
        @Override
        public Object getElementAt(int index) {
            return items.get(index);
        @Override
        public void setSelectedItem(Object anItem) {
            if((selected != null && !selected.equals(anItem))
                    || (selected == null && anItem != null)) {
                selected = anItem;
                fireContentsChanged(this, -1, -1);
        @Override
        public Object getSelectedItem() {
            return selected;
        @Override
        public void addElement(Object obj) {
            insertElementAt(obj, items.size());
        @Override
        public void removeElement(Object element) {
            removeElementAt(items.indexOf(element));
        @Override
        public void insertElementAt(Object element, int index) {
            items.add(index, element);
            fireIntervalAdded(this, index, index);
        @Override
        public void removeElementAt(int index) {
            Object removed = items.get(index);
            if(selected != null && selected.equals(removed)) {
                Object newSelection;
                int size = items.size();
                if(size == 1) {
                    newSelection = null;
                else {
                    switch(changeOnRemove) {
                    case CLEAR_SELECTION:
                        newSelection = null;
                        break;
                    case SELECT_NEXT:
                        if(index == size - 1) {
                            newSelection = items.get(index - 1);
                        else {
                            newSelection = items.get(index + 1);
                        break;
                    case SELECT_PREVIOUS:
                        if(index == 0) {
                            newSelection = items.get(index + 1);
                        else {
                            newSelection = items.get(index - 1);
                        break;
                    default:
                        assert false : "Unknown SelectionChange: " + changeOnRemove;
                        newSelection = null;
                        break;
                setSelectedItem(newSelection);
            items.remove(index);
            fireIntervalRemoved(removed, index, index);
    }

  • Set Default Value of Multi-select list item

    I have a multi-select list item I want to default the value of to '%' (which is really '%null%') and have it selected. I tried setting default value of item, but it doesn't take '%null%'. I also tried a computation with a static of
    :P507_ITEM := '%null%'; How do you get the default value set and selected?

    Hi
    Shijesh is right, you need to change your null return value and use that return value as your default. Try and use something of the same datatype as your real return values if you plan to use '%' to display all as it will make your queries simpler. eg.
    Company A returns 1
    Company B return 2
    % returns 0
    Then your query would be...
    SELECT ...
    FROM ...
    WHERE company_id = DECODE(:P_COMPANY,1,1,2,2,0,company_id)
    Hope this makes sense.
    Cheers
    Ben

  • Selecting open items in F-03 using additional selections

    I need to select open items in a GL account using more than one additional selection eg. document type, posting date, assignment
    I select Additional Selections then document type then add a document type, then I select Other Selection - posting date and then Process Open Items. Only the first selection for document type is recognised.
    Is this a bug?
    Edited by: Richard Somerset on Jan 7, 2009 5:14 PM

    However SAP help suggest that this can be done
    Searching for Open Items  
    Use
    After you have entered an account with open items, you can search for specific open items to be cleared.
    On the screen for selecting open items, you can search for specific open items to be cleared using the following additional selection fields:
    Gross amount
    Document number
    Posting date
    Other fields depending on the system configuration
    Procedure
    To search for open items using the additional selection criteria, proceed as follows:
    Choose one of these fields, such as Posting date, then choose Execute.
    The screen for entering selection criteria appears.
    Enter one or more single values or ranges within the selection, such as 01/01/1993 through 01/31/1993 for a range of posting dates.
    To search for specific open items by other criteria (optional), choose Edit ® Select more.
    Repeat steps 1 and 2 for the other selection criteria.
    After you enter your selection criteria, you have the following options:
    Display the list of open items by choosing Goto ® Open items.
    Display the clearing document header and the items entered so far by choosing Goto ® Doc. Overview.
    Post the clearing document by choosing Document ® Post
    Edited by: Richard Somerset on Jan 8, 2009 9:05 AM

  • Report for a selection of items+batch numbers that I would have had on hand

    I would like to run a report for a selection of items and batch numbers that I would have had on hand (only) for September 30th.  I have run the batch numbers report but this gives me all transactions from August 1/09 to September 30/09.

    Hi
    If you need to know quantity onhand ,probably you need to run query against OINM tables but If you are lloking for information then I guess this batch and serial number would be handy ....
    Hope this helps
    Bishal

  • Display all valid items and select multiple items

    Let us say I have valid items in table. For each purchase user can select few items from the list. I have to display all valid items from table and user should able to check items to buy. What is the best way we can implement this in forms.
    Thanks, lalitha

    For all the valid items you can create LOV as i understood then user can choose what he wants. But this sentence ti still confusing to me. Or what is the tricky things in you scanerio if only you have to show the valid item then use LOV.
    Lalitk wrote:
    and user should able to check items to buy.-Ammad

  • How to select line items in recording(BDC)

    Hi All,
    we are trying to Recording QP02 transaction ,we have inputs like material,plant,Group,Group Counter.
    after entering these inputs we will get multiful line items(inspection characteristics).we need to check one field for each item.
    How can we get this?
    Please help me?
    Thanks,
    Peddi reddy.

    Hi Kamesh,
    Thanks.
    After selecting line items i am selecting control indicators Tab.
    then Pop up screen called as (Edit characteristic control indicators) will open ,..then just pressing enter..the it will show another pop up ..here i am selecting one field(Long term Inspection)..the process has to do for all line items.
    In My program i have copied BDC performs which i got from Recording.
    Do i need to change tha performs or will it work?
    i tested in foreground with another material .... upto 3 or 4 line items its working fine.after that sytem does.t say anything.
    if i need to chage performs please help me .

  • Passing DB values to the textbox based on Select Value item

    Hi,
    I am very much new to the Oracle APEX. In my project, I have a requirement wherein I have to pass the different values to the corresponding textboxes on a different page based on the value I select from the "Select Value" item.
    I have tried using calling On-Demand Processes, but couldn't get the desired result.
    Please suggest!

    Hi 796444 ,
    Welcome to the forum. It will be good if you familiarize yourself with the forum ettiquittes. Also, when posting always state the following:
    a. Apex version
    b. DB version
    c. Web server ; EPG, apexlistener, etc
    d. Provide adequate details for others to understand your problem / what you are trying to achieve.
    e. Any code snippets you post should be enclosed in a pair of tags
    If your current problem is that the you are *calling* Page 2 from Page 1, and while doing so you want the value of P1_ITEM1 to be passed and set in p2_ITEM1 then
    a. Edit the branch
    b. In Action in Set these items write P2_ITEM1
    c. In With these values write &P1_ITEM1. (do not miss the dot at the end)
    It looks good if you use a better handle than 796444 :-)
    Regards,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How do you select and move more than one bookmark at a time? Shift+Click does not select multiple items that are next to one another in a list because the item

    How do you select and move more than one bookmark at a time?
    Shift+Click does not select multiple items that are next to one another in a list because the items open in firefox before this happens.

    Use the bookmarks library. You may use Shift +Click, and Ctrl + Click to create groupings of selected bookmarks to drag and drop.
    * one method of opening the bookmarks library is keyboard shortcut <br /> Ctrl+Shift+B (Windows)
    *see also [[How to use bookmarks to save and organize your favorite websites]]
    *and [[Use bookmark folders to organize your bookmarks]]

  • How to highlight selected list item in sap ui5?

    I have a list..in which custom list items have been used.
    On click of any custom list item it should get highlighted,
    I am able to click on each custom item...and have added an browser event
    on its click..which changes its color ..
    function to change its color goes like this:
    hbox.attachBrowserEvent("click", function(event) {
                    var idOfHbox= this.getId();
                     $('#'+idOfHbox).css"backgroundcolor","#e6f2f9");
    ALso for each custom list item I have used a button and a checkbox in a HBox..
    So am changing the hbox color (on click event).
    The problem here is...
    When i select one item, and then select another item..both of them are highlighted..
    I want only the current selected item to be highlighted.
    Please guide..

    Have you tried using the SelectionChange event on the list, rather than the browser onClick event?
    I believe the SelectionChange event is fired for each selected and deselected item in the list.
    https://sapui5.hana.ondemand.com/sdk/docs/api/symbols/sap.m.ListBase.html#event:selectionChange
    Many thanks,
    Jason

  • How do I select multiple items to paste a style?

    There are times when I have several paragraphs of text with a number or letter in front of strategic sections.  I like to change the color of these letters or numbers, make them boldface and change them to SuperScript.  Once I have the first one changed, in Pages 4, I would simply copy the character style for this changed letter or number.  Next, I would hold down the COMMAND key while double-clicking each subsequent letter or number.  Finally, I would paste the formatting which would update all the various items I had selected with the saved formatting.
    In Pages 5, I can copy the character style, and then hold down the COMMAND key and start double-clicking text items to reformat, but while the first item will highlight when selected, the second item I double click will highlight and the first one will de-select.
    Is there a way to multi-select text items in this way?
    Thanks for your help!!!
    Ron

    There isn't any multi- selection in Pages 5. use PAges  09 instead.

  • How can I select an item from a list component with a seperate button

    This is a repost, I decided it'd probably be better here than
    in the general discussion board. I will try to delete the other
    one.
    Hello Everyone,
    This is my first post here. I am trying to figure out how to
    select an item within a list component with a button. This button
    is calling a function. I have searched for the past 3 hours online
    and I can't find anything about it. I thought perhaps I could set
    the selectedItem parameter, but that is read only, and I believe it
    returns an object reference rather than something like a number so
    even if i could set it, it would be pointless. I also found a get
    focus button, thought that may lead me somewhere, but I think that
    is for setting which component has focus currently as far as typing
    in and things like that. I am lost.
    Basically I am looking for a way to type this
    myList.setSelected(5); where 5 is the 5th item in the list.
    Any help would be much appreciated.

    Never mind found it. It is the property called selectedIndex
    and it is writable

Maybe you are looking for

  • I am receiving the message "disc burner or software not found" when trying to burn a playlist to a blank disc.

    I ran the diagnostics test and the program was unable to detect a prerecorded cd. This is a fairly new problem and I am not sure what to do. The results to the diagnostics test are below: Microsoft Windows 7 x64 Home Premium Edition Service Pack 1 (B

  • Labview 8.0.1 installer error

    hello all, I am having an issue with building an installer package in labview 8.0.1.  I can build an executable every time, but when i try to build an installer I get the error message in the attatched txt file (its to lengthy to paste here) .  this

  • TableView initial sort

    Hi, I want to have an initial sort of a table and it should be done by the server. I know I could do it by application, but this is just a question if it is possible. I found for the htmlb:tableViewColumn the attribute preSelectedSortDirection = "DES

  • Where does iWeb hide its projects ?

    I have checked /Library, ~/Library, /System/Library, ~/Documents to no avail. I'd have thought it'd be along the lines of iMovie, ie in ~/Documents, but no. The documentation is quite sparse, I cannot find it in the online doc, and Spotlight returns

  • Count query taking time

    I have a query -->select c1,c2,c3 from table1 . This query takes only few milliseconds. But when I take count from the same query i.e. when I execute select count(c1,c2,c3) from table1 then it takes a very long time (about 1 min). The table1 contains