Announcement Announcement Module
Collapse
No announcement yet.
GlazedList AutoCompleteSupport with JGoodies Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • GlazedList AutoCompleteSupport with JGoodies

    Hi,
    I'm using AutoCompleteSupport and facing a problem to get selectedItem when user type in any value in the combo. It works fine when item selected using mouse.

    Typing any value into the combo causes the popup to appear with filtered content and select any item within the popup, the combobox will show the correct selected item but in the backend the "getSelected()" function will return the first element of the List.

    For example : Consider a ComboBox contains the following elements

    AED
    EUR
    GBP
    GIP
    GRD

    Now type "G" to select GBP the combo will show the selected item as GBP but in the backend 'AED' currency got selected (first element in the List). The BindBean will have the selected Currency as 'AED' instead of GBP.

    Selecting any item using mouse (don't use AutoComplete behaviour ) the "getSelected()" is returning the correct selected value.

    Please advice where I went wrong in my code.

    This is how I'm doing.
    Code:
    package com.rbs.futures.greenscope.client.admin.account;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    
    import ca.odell.glazedlists.BasicEventList;
    import ca.odell.glazedlists.EventList;
    import ca.odell.glazedlists.ListSelection;
    import ca.odell.glazedlists.ListSelection.Listener;
    import ca.odell.glazedlists.swing.AutoCompleteSupport;
    
    import com.rbs.futures.greenscope.client.common.glazedlists.JComboBoxListSelectionStrategy;
    import com.rbs.futures.greenscope.client.common.glazedlists.ListSelectionStrategy;
    
    public class AutoCompleteSupportLearning
    {
        JFrame                        frame;
        AutoCompleteSupport<String>   autocomplete;
        private JComboBox             comboBox;
        private ListSelection<String> listSelector;
    
        public AutoCompleteSupportLearning()
        {
            initGui();
        }
    
        private void initGui()
        {
    
            Box vbox, hbox;
            frame = new JFrame("Currency Chooser");
    
            final EventList<String> items = getCurrencies();
    
            Listener selectionListener = new SelectionListener();
            listSelector = new ListSelection<String>(items);
            listSelector.addSelectionListener(selectionListener);
    
            vbox = Box.createVerticalBox();
            vbox.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
            hbox = Box.createHorizontalBox();
    
            hbox.add(new JLabel("Select Currency:"));
    
            comboBox = new JComboBox();
    
            ActionListener comboActionListener = new ComboBoxActionListener();
            comboBox.addActionListener(comboActionListener);
    
            comboBox.setPrototypeDisplayValue("Marmoset");
            hbox.add(comboBox);
    
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    autocomplete = AutoCompleteSupport.install(comboBox, items);
                }
            });
    
            vbox.add(hbox);
            frame.add(vbox);
            frame.pack();
            frame.setVisible(true);
        }
    
        private EventList<String> getCurrencies()
        {
            EventList<String> result = new BasicEventList<String>();
            result.add("AED");
            result.add("AUD");
            result.add("EUR");
            result.add("GBP");
            result.add("GIP");
            result.add("GRD");
            result.add("SAR");
            return result;
        }
    
        abstract class ListSelectionTemplate<T> implements ListSelection.Listener
        {
            abstract EventList<T> getSelected();
    
            abstract void resetBindBean();
    
            abstract void setBindBean(T selected);
    
            public void selectionChanged(int arg0, int arg1)
            {
                EventList<T> events = getSelected();
    
                if (events.isEmpty()) {
                    resetBindBean();
                }
                else {
                    T selected = events.iterator().next();
                    setBindBean(selected);
                }
            }
        }
    
        class SelectionListener extends ListSelectionTemplate<String>
        {
            @Override
            EventList<String> getSelected()
            {
                return listSelector.getSelected();
            }
    
            @Override
            void resetBindBean()
            {
                bindBean.setCurrency(null);
            }
    
            @Override
            void setBindBean(String selected)
            {
                bindBean.setCurrency(selected);
                comboBox.setSelectedItem(selected);
            }
        }
    
        class ComboBoxActionListener extends ComboBoxActionListenerTemplate
        {
            @Override
            ListSelection getListSelection()
            {
                return listSelector;
            }
        }
    
        abstract class ComboBoxActionListenerTemplate<T> implements ActionListener
        {
            abstract ListSelection<T> getListSelection();
    
            public void actionPerformed(ActionEvent e)
            {
                JComboBox source = (JComboBox) e.getSource();
                ListSelectionStrategy<JComboBox> strategy = new JComboBoxListSelectionStrategy();
                strategy.select(source, getListSelection());
            }
        }
    
        public static void main(String[] args)
        {
            AutoCompleteSupportLearning obj = new AutoCompleteSupportLearning();
        }
    
    }
Working...
X