Results 1 to 3 of 3
  1. #1
    Ghobliin is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Lightbulb custom jcomboxeditor fires automatic jcombox actionperform action

    as the tittle says when i select an item from the jcombobox pop up menu nothing bad happens but when i come back to the editor(to type text again) then action performed event fires immediately for no reason.

    i create a custom jcombox model and editor (and founded some lines of code that augmented the problem an automatic fired action event).

    the example (sscce)

    Java Code:
    import Controllers.CustomComboBoxModel;
    import Controllers.ProductosItemEditor;
    //import Store.util.DatabaseHelper;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.List;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import org.hibernate.HibernateException;
    
    public class CustomComboBoxEditor {
    
        public static void main(String[] args) {
            new CustomComboBoxEditor();
        }
    
        public CustomComboBoxEditor() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    final JComboBox cb = new JComboBox();
    
                    cb.setEditable(true);
                    cb.setEditor(new ProductosItemEditor());
                    CustomComboBoxModel model = new CustomComboBoxModel();
                    cb.setModel(model);
                    cb.addItem("Apple");
                    cb.addItem("Banana");
                    cb.addItem("Orange");
                    cb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
                        @Override
                        public void keyReleased(KeyEvent event) {
                            System.out.println("typed text " + cb.getEditor().getItem());
                            //populateModel(cb.getEditor().getItem().toString());
                        }
      /*
                        public void populateModel(String text) throws HibernateException {
                            List l = DatabaseHelper.GetProductsBy(text);
                            for (Object object : l) {
                                cb.addItem(object);
                            }
    
                        }*/
                    });
                     cb.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    JComboSearcherActionPerformed(evt);
    
                }
    
                        private void JComboSearcherActionPerformed(ActionEvent evt) {
                            System.out.println(" action perform combobox " + cb.getSelectedItem());
                        }
            });
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
                    frame.add(cb);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    
    }
    my editor
    Java Code:
     import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyListener;
    import java.lang.reflect.Method;
    
    import javax.swing.JTextField;
    import javax.swing.ComboBoxEditor;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    public class ProductosItemEditor extends JPanel implements ComboBoxEditor {
    
        private JTextField txtField = new JTextField();
        private JButton clearButton = new JButton();
        private Object oldValue;
    
        /**
         *
         */
        public ProductosItemEditor() {
            setLayout(new BorderLayout());
    
            clearButton.setBackground(new java.awt.Color(255, 255, 255));
            clearButton.setForeground(new java.awt.Color(0, 51, 51));
            clearButton.setText("X");
            clearButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    txtField.setText("");
                    txtField.requestFocusInWindow();
                    //remove the above line and an action performed event of combobox 
                    // gets fired automaticaly
                }
            });
    
            add(txtField, BorderLayout.CENTER);
            add(clearButton, BorderLayout.EAST);
    
        }
    
        @Override
        public Component getEditorComponent() {
            return this;
    
        }
    
        public String getText() {
            return txtField.getText();
        }
    
        @Override
        public Object getItem() {
            Object newValue = txtField.getText();
    
            if (oldValue != null && !(oldValue instanceof String)) {
    
                if (newValue.equals(oldValue.toString())) {
                    return oldValue;
                } else {
    
                    Class<?> cls = oldValue.getClass();
                    try {
                        Method method = cls.getMethod("valueOf", new Class[]{String.class});
                        newValue = method.invoke(oldValue, new Object[]{txtField.getText()});
                    } catch (Exception ex) {
                    }
                }
            }
    
            return newValue;
        }
    
        @Override
        public void setItem(Object anObject) {
            String text;
    
            if (anObject != null) {
                text = anObject.toString();
                oldValue = anObject;
    
            } else {
                text = "";
            }
    
    
    
        }
    
        @Override
        public void selectAll() {
            txtField.selectAll();
        }
    
        @Override
        public void addActionListener(ActionListener l) {
            txtField.addActionListener(l);
        }
    
        @Override
        public void removeActionListener(ActionListener l) {
            txtField.removeActionListener(l);
        }
    
        @Override
        public void addKeyListener(KeyListener e) {
            txtField.addKeyListener(e);
        }
    
        @Override
        public void removeKeyListener(KeyListener e) {
            txtField.removeKeyListener(e);
        }
    
        @Override
        public void addNotify() {
            super.addNotify();
            txtField.requestFocusInWindow();
        }
    }

    my model
    Java Code:
    import javax.swing.AbstractListModel;
    import javax.swing.MutableComboBoxModel;
    import java.util.*;
    import java.io.Serializable;
    import java.util.logging.Logger;
    
    
    public final class CustomComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
    
        ArrayList<Object> objects;
        Object selectedObject;
    
        /**
         * Constructs a DefaultComboBoxModel object initialized with a vector.
         *
         * @param v a Vector object ...
         */
        public CustomComboBoxModel(ArrayList v) {
            objects = v;
    
            if (getSize() > 0) {
                selectedObject = getElementAt(0);
            }
        }
    
        /**
         *
         */
        public CustomComboBoxModel() {
            this.objects = new ArrayList<Object>();
        }
    
        // implements javax.swing.ComboBoxModel
        /**
         * Set the value of the selected item. The selected item may be null.
         * <p>
         *
         * @param anObject The combo box value or null for no selection.
         */
        public void setSelectedItem(Object anObject) {
            if (anObject != null) {
                selectedObject = anObject;
    //            fireContentsChanged(this, -1, -1);
    // quit above line and bug return (the action perform gets fired automaticaly            
            }
        }
    
        // implements javax.swing.ComboBoxModel
        public Object getSelectedItem() {
    
            return selectedObject;
        }
    
        // implements javax.swing.ListModel
        public int getSize() {
            return objects.size();
        }
    
        // implements javax.swing.ListModel
        public Object getElementAt(int index) {
            if (index >= 0 && index < objects.size()) {
    
                return objects.get(index);
            } else {
                return null;
            }
    
        }
    
        /**
         * Returns the index-position of the specified object in the list.
         *
         * @param anObject
         * @return an int representing the index position, where 0 is the first position
         */
        public int getIndexOf(Object anObject) {
            return objects.indexOf(anObject);
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void addElement(Object anObject) {
            objects.add(anObject);
            fireIntervalAdded(this, objects.size() - 1, objects.size() - 1);
            if (objects.size() == 1 && selectedObject == null && anObject != null) {
                setSelectedItem(anObject);
            }
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void insertElementAt(Object anObject, int index) {
            objects.add(index, anObject);
            fireIntervalAdded(this, index, index);
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void removeElementAt(int index) {
            if (getElementAt(index) == selectedObject) {
                if (index == 0) {
                    setSelectedItem(getSize() == 1 ? null : getElementAt(index + 1));
                } else {
                    setSelectedItem(getElementAt(index - 1));
                }
            }
    
            objects.remove(index);
    
            fireIntervalRemoved(this, index, index);
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void removeElement(Object anObject) {
            int index = objects.indexOf(anObject);
            if (index != -1) {
                removeElementAt(index);
            }
        }
    
        /**
         * Empties the list.
         */
        public void removeAllElements() {
            if (objects.size() > 0) {
                int firstIndex = 0;
                int lastIndex = objects.size() - 1;
                objects.clear();
                selectedObject = null;
                fireIntervalRemoved(this, firstIndex, lastIndex);
            } else {
                selectedObject = null;
            }
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: custom jcomboxeditor fires automatic jcombox actionperform action

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Ghobliin is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: custom jcomboxeditor fires automatic jcombox actionperform action

    now i fixed the problem and modified the behavior which i didn't wanted (when you type text in a editable combobox the first occurrence in the list is placed as the text in the editor) i also modified some other problems which fired jcombobox action performed events for no reason. all of this problems where in the model and one at the editor.

    Java Code:
    public class ProductosItemEditor extends JPanel implements ComboBoxEditor {
    
    private JTextField txtField = new JTextField();
    private JButton clearButton = new JButton();
    
    private Object myObject;
    private Object myReturnObject;
    
    /**
     *
     */
    public ProductosItemEditor() {
        setLayout(new BorderLayout());
    
        clearButton.setBackground(new java.awt.Color(255, 255, 255));
        clearButton.setForeground(new java.awt.Color(0, 51, 51));
        clearButton.setText("X");
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txtField.setText("");
                txtField.requestFocusInWindow();
                //remove the above line and an action performed event of combobox 
                // gets fired automaticaly
            }
        });
    
        add(txtField, BorderLayout.CENTER);
        add(clearButton, BorderLayout.EAST);
    
    }
    
    @Override
    public Component getEditorComponent() {
        return this;
    
    }
    
    public Object getText() {
        return txtField.getText();
    }
    
        @Override
        public Object getItem() {
    //it's good for my you can implement your own method 
            ///or look at CustomComboBoxModel for studies porposes
            return "";
    
    
    }
    
    @Override
        public void setItem(Object anObject) {
    
        if (anObject != null) {
            myObject = anObject;
            myReturnObject = anObject;
            //I can now set whatever i want in the text feild            
            txtField.setText(myObject.toString());//<--
    
        } else {
            myReturnObject = "";
        }
    }
    
    @Override
        public void selectAll() {
        txtField.selectAll();
    }
    
    @Override
        public void addActionListener(ActionListener l) {
        txtField.addActionListener(l);
    }
    
    @Override
        public void removeActionListener(ActionListener l) {
        txtField.removeActionListener(l);
    }
    
    @Override
        public void addKeyListener(KeyListener e) {
        if (e == null) {
            return;
        }
        txtField.addKeyListener(e);
    }
    
    @Override
        public void removeKeyListener(KeyListener e) {
        txtField.removeKeyListener(e);
    }
    
    @Override
        public void addNotify() {
        super.addNotify();
        txtField.requestFocusInWindow();
    }
    
    }
    and the model(stores data in an arrayList)

    Java Code:
      import javax.swing.AbstractListModel;
    import javax.swing.MutableComboBoxModel;
    
    import java.util.*;
    
    
    import java.io.Serializable;
    
    
    public final class CustomComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
    
        ArrayList<Object> objects;
        Object selectedObject;
    
        /**
         * Constructs a DefaultComboBoxModel object initialized with a vector.
         *
         * @param v a Vector object ...
         */
        public CustomComboBoxModel(ArrayList v) {
            objects = v;
    
            if (getSize() > 0) {
                selectedObject = getElementAt(0);
            }
        }
    
        /**
         *
         */
        public CustomComboBoxModel() {
            this.objects = new ArrayList<Object>();
        }
    
        // implements javax.swing.ComboBoxModel
        /**
         * Set the value of the selected item. The selected item may be null.
         * <p>
         *
         * @param anObject The combo box value or null for no selection.
         */
        public void setSelectedItem(Object anObject) {
            if (anObject != null) {
                selectedObject = anObject;
    //            fireContentsChanged(this, -1, -1);
    // quit above line and bug return (the action perform gets fired automaticaly            
            }
        }
    
        // implements javax.swing.ComboBoxModel
        public Object getSelectedItem() {
    
            return selectedObject;
        }
    
        // implements javax.swing.ListModel
        public int getSize() {
            return objects.size();
        }
    
        // implements javax.swing.ListModel
        public Object getElementAt(int index) {
            if (index >= 0 && index < objects.size()) {
    
                return objects.get(index);
            } else {
                return null;
            }
    
        }
    
        /**
         * Returns the index-position of the specified object in the list.
         *
         * @param anObject
         * @return an int representing the index position, where 0 is the first position
         */
        public int getIndexOf(Object anObject) {
            return objects.indexOf(anObject);
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void addElement(Object anObject) {
            objects.add(anObject);
            fireIntervalAdded(this, objects.size() - 1, objects.size() - 1);
            if (objects.size() == 1 && selectedObject == null && anObject != null) {
    //            setSelectedItem(anObject); this line causes automatic jcombobox action performed events
            }
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void insertElementAt(Object anObject, int index) {
            objects.add(index, anObject);
            fireIntervalAdded(this, index, index);
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void removeElementAt(int index) {
            if (getElementAt(index) == selectedObject) {
                if (index == 0) {
                    setSelectedItem(getSize() == 1 ? null : getElementAt(index + 1));
                } else {
                    setSelectedItem(getElementAt(index - 1));
                }
            }
    
            objects.remove(index);
    
            fireIntervalRemoved(this, index, index);
        }
    
        // implements javax.swing.MutableComboBoxModel
        public void removeElement(Object anObject) {
            int index = objects.indexOf(anObject);
            if (index != -1) {
                removeElementAt(index);
            }
        }
    
        /**
         * Empties the list.
         */
        public void removeAllElements() {
            if (objects.size() > 0) {
                int firstIndex = 0;
                int lastIndex = objects.size() - 1;
                objects.clear();
                selectedObject = null;
                fireIntervalRemoved(this, firstIndex, lastIndex);
            } else {
                selectedObject = null;
            }
        }
    
    }

    i would be glad to see the inspection of this code for improvements, i hope some body can use this and save some time

Similar Threads

  1. Replies: 1
    Last Post: 08-08-2012, 05:20 AM
  2. how can i change color of disabled jcombox
    By RichersooN in forum AWT / Swing
    Replies: 14
    Last Post: 07-25-2012, 05:08 AM
  3. how to use JCombox to view a calendar and choose it
    By kiki2009 in forum AWT / Swing
    Replies: 4
    Last Post: 04-01-2010, 09:12 PM
  4. Jcombox problem......
    By pra.deep in forum AWT / Swing
    Replies: 7
    Last Post: 10-13-2009, 03:32 PM
  5. Prob with event handling for Jcombox
    By harsha in forum AWT / Swing
    Replies: 4
    Last Post: 09-18-2009, 03:14 PM

Tags for this Thread

Posting Permissions

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