Results 1 to 5 of 5
  1. #1
    monica is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default Editable combo box that adds new entries

    Hi,
    I want to create a combo box which is editable and it should update every new entries(entries that user type in combo box instead of selecting from present list) to the already present combo box list. Kindly help me with the code.
    Thank you.

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

    Default

    Kindly help me with the code.
    Sure. Let's see what you have so far, and tell us exactly what problem you are facing.

    db

  3. #3
    monica is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,
    The code is with many other components and many other methods apart from editable combo box. So i just picked up the code that are related to combo box alone. In this combo box, i could add new entries and it is updated in my database when i save the application. but i want the new entry to show up in combo box next time when user opens the application. and here comes my code.

    import javax.swing.JComboBox;
    public class EntryDialog extends JDialog {
    private JComboBox categoryComboBox;
    private String categoryLabels[] = {"Work", "University", "Assignment"};
    private EntryDialog() {
    }

    private void initComponents() {
    panel3 = new JPanel();
    // panel 3
    panel3.setLayout(new GridLayout(2, 4, 5, 15));
    categoryLabel = new JLabel("Category:", (int) CENTER_ALIGNMENT);
    panel3.add(categoryLabel);

    categoryComboBox = new JComboBox(categoryLabels);
    categoryComboBox.setEditable(true);
    categoryComboBox.setSelectedIndex(0);
    panel3.add(categoryComboBox);

    this.add(panel3);
    }
    }

  4. #4
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default Entry Dialog

    Java Code:
    /*****************   CustomComboBoxModel.java *************************/
    /**
     *
     * @author T.pugazhendhi
     */
    import java.util.List;
    import javax.swing.ComboBoxModel;
    
    /**
     * Custom Implementation of {@code ComboBoxModel} to allow adding a list of
     * elements to the list.
     */
    public interface CustomComboBoxModel<T> extends ComboBoxModel {
    
        void add(List<T> elementsToAdd);
    
        List<T> getElements();
    
    }
    Java Code:
    /*****************    DefaultCustomComboBoxModel.java *************************/
    
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.AbstractListModel;
    
    /**
     * author T.Pugazhendhi
     * Default Implementation of CustomComboBoxModel
     */
    public class DefaultCustomComboBoxModel<T> extends AbstractListModel implements CustomComboBoxModel<T> {
    
        List<T> objects;
        T selectedObject;
    
        /* ---------------------------------------------------------------------- */
        /**
         * Constructs an empty DefaultCustomComboBoxModel object.
         */
        public DefaultCustomComboBoxModel() {
            objects = new ArrayList<T>();
        }
    
        /* ---------------------------------------------------------------------- */
        /**
         * Constructs a DefaultCustomComboBoxModel object initialized with
         * an array of objects.
         *
         * @param items  an array of Object objects
         */
        public DefaultCustomComboBoxModel(final T items[]) {
            objects = new ArrayList<T>();
    
            int i, c;
            for (i = 0, c = items.length; i < c; i++) {
                objects.add(items[i]);
            }
    
            if (getSize() > 0) {
                selectedObject = objects.get(0);
            }
        }
    
        /* ---------------------------------------------------------------------- */
        // implements javax.swing.ComboBoxModel
        /**
         * Set the value of the selected item. The selected item may be null.
         * Make sure {@code anObject} is an instance of T otherwise a
         * ClassCastException will be thrown.
         * <p>
         * @param anObject The combo box value or null for no selection.
         */
        @Override
        public void setSelectedItem(Object anObject) {
            if ((selectedObject != null && !selectedObject.equals(anObject))
                    || selectedObject == null && anObject != null) {
                selectedObject = (T) anObject;
                fireContentsChanged(this, -1, -1);
            }
        }
    
        // implements javax.swing.ComboBoxModel
        @Override
        public T getSelectedItem() {
            return selectedObject;
        }
    
        // implements javax.swing.ListModel
        @Override
        public int getSize() {
            return objects.size();
        }
    
        // implements javax.swing.ListModel
        @Override
        public T getElementAt(int index) {
            if (index >= 0 && index < objects.size()) {
                return objects.get(index);
            } else {
                return null;
            }
        }
    
        /* ---------------------------------------------------------------------- */
        /**
         * implements javax.swing.MutableComboBoxModel
         */
        public void addElement(T anObject) {
            objects.add(anObject);
            fireIntervalAdded(this, objects.size() - 1, objects.size() - 1);
            if (objects.size() == 1 && selectedObject == null && anObject != null) {
                setSelectedItem(anObject);
            }
        }
    
    
        /* ---------------------------------------------------------------------- */
        @Override
        public void add(List<T> elementsToAdd) {
            objects.addAll(elementsToAdd);
        }
    
        /* ---------------------------------------------------------------------- */
        @Override
        public List<T> getElements() {
            return objects;
        }
    }
    Java Code:
    
    /*****************  EntryDialog.java *************************/
    /**
     *
     * author  T.pugazhendhi
     */
    import java.awt.GridLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.ArrayList;
    import javax.swing.JComboBox;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    
    /* ======================================================================== */
    /**
     * 
     * EntryDialog class has the combox inside the panel which will get the list 
     * using Comboxbox .,
     *
     */
    public class EntryDialog extends JDialog {
    
        private static EntryDialog objDialog = null;
        private JPanel mainPanel;
        private JComboBox categoryComboBox;
        private ArrayList<String> cmbListItem = new ArrayList<String>();
    
        /* --------------------------------------------------------------------- */
        /**
         *  initialize the properties of the Dialog
         */
        private void initComponents() {
    
            objDialog.setSize(200, 300);
            mainPanel = new JPanel();
            mainPanel.setLayout(new GridLayout(2, 4, 5, 15));
            categoryComboBox = new JComboBox();
    
            /**
             * setting the initial value in the list ., so that it will be added
             * into combo box using the DefaultComboBoxModel
             */
            cmbListItem.add("Type Here & hit Enter to Add Into ComboBox");
            final DefaultCustomComboBoxModel obj = new DefaultCustomComboBoxModel
                    (cmbListItem.toArray());
            categoryComboBox.setModel(obj);// iam setting the Custom model here 
            categoryComboBox.setSelectedIndex(0);
            categoryComboBox.setEditable(true);
    
            categoryComboBox.addItemListener(new ItemListener() {
                /**
                 *  Item state Change Listener i have written in order to Notify
                 *  the Changes, when the user Enter New item
                 */
                public void itemStateChanged(ItemEvent e) {
    
                    // this loop will check whether the value is already inside or not
                    // if its contains then it will skip .,
                    // if not it will add into the Combo box
                    if (!cmbListItem.contains(e.getItem().toString())) {
                        cmbListItem.add(e.getItem().toString());
                        obj.addElement(e.getItem().toString());
                        categoryComboBox.setSelectedIndex(0);
                    }
                }
            });
            
            mainPanel.add(categoryComboBox);
            this.add(mainPanel);
        }
    
        /**
         * Startup of EntryDialog
         * @param args
         */
        public static void main(String[] args) {
            
            objDialog = new EntryDialog();
            objDialog.initComponents();
            objDialog.setVisible(true);
        }
    }
    
    }



    hai rest of the thing u do it if u have doubt in this code

    post a reply on this ...
    :) Small word can mean it more ,.

  5. #5
    monica is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Client/ Server system that adds a stream of integers
    By Billywizz in forum New To Java
    Replies: 12
    Last Post: 01-27-2011, 01:28 PM
  2. dynamically populate the city combo box based on the values of state combo +ajax+jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-29-2010, 10:00 AM
  3. Replies: 3
    Last Post: 07-30-2010, 10:27 AM
  4. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 03:14 PM
  5. Creating a non-editable combo box
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:45 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
  •