Java Utililty Methods JList Add Remove

List of utility methods to do JList Add Remove

Description

The list of methods to do JList Add Remove are organized into topic(s).

Method

voidaddDoubleClickEvent(JList list)
add Double Click Event
list.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        JList source = (JList) e.getSource();
        if (e.getClickCount() == 2) {
            ListSelectionListener[] listeners = source.getListSelectionListeners();
            for (int i = 0; i < listeners.length; i++) {
                listeners[i].valueChanged(new ListSelectionEvent(source, source.getSelectedIndex(),
                        source.getSelectedIndex(), false));
...
voidaddItemJList(javax.swing.JList jlist, String item)
add Item J List
int size = jlist.getModel().getSize();
String[] ary = new String[size + 1];
for (int i = 0; i < size; i++) {
    ary[i] = (String) jlist.getModel().getElementAt(i);
ary[size] = item;
jlist.setModel(new javax.swing.DefaultComboBoxModel(ary));
voidaddToList(JList list, Object object)
add To List
ListModel model = list.getModel();
if (model == null) {
    model = new DefaultListModel();
    list.setModel(model);
;
if (model instanceof DefaultListModel) {
    DefaultListModel listModel = (DefaultListModel) model;
...
voidfillList(Collection anList, JList anJList)
fillList
DefaultListModel listModel = new DefaultListModel();
if (anList != null && anJList != null) {
    Object el = null;
    Iterator it = anList.iterator();
    while (it.hasNext()) {
        el = it.next();
        listModel.addElement(el);
anJList.setModel(listModel);
if (listModel.size() > 0) {
    anJList.setSelectedIndex(0);
voidfillList(JList aListComponent, String theList, boolean removeQuotes)
Fills a JList component with a String representation of the to-be contents

Example of input argument theList with !removeQuotes : ["123","456","789"]
Example of input argument theList with removeQuotes: [123,456,789]
DefaultListModel itsModel = new DefaultListModel();
aListComponent.setModel(itsModel);
String aList = theList;
if (aList.startsWith("[")) {
    aList = aList.substring(1, aList.length());
if (aList.endsWith("]")) {
    aList = aList.substring(0, aList.length() - 1);
...
voidfillList(Object[] elements, JList list)
fillList
setListModel(generateListModel(elements), list);
voidJListAddObject(javax.swing.JList list, Object item)
J List Add Object
javax.swing.ListModel lm = list.getModel();
Object[] newList = new Object[lm.getSize() + 1];
for (int i = 0; i < lm.getSize(); i++) {
    newList[i] = lm.getElementAt(i);
newList[lm.getSize()] = item;
list.setListData(newList);
voidremoveAllListItems(JList sourceList)
remove All List Items
DefaultListModel selectedTableListModel = (DefaultListModel) sourceList.getModel();
selectedTableListModel.removeAllElements();
voidremoveFromList(JList list, Object object)
remove From List
ListModel model = list.getModel();
if (model == null) {
    model = new DefaultListModel();
    list.setModel(model);
;
if (model instanceof DefaultListModel) {
    DefaultListModel listModel = (DefaultListModel) model;
...
voidremoveItemJList(javax.swing.JList jlist)
remove Item J List
String id = (String) jlist.getSelectedValue();
if (id != null) {
    int size = jlist.getModel().getSize();
    if (size == 0)
        return;
    String[] ary = new String[size - 1];
    int cnt = 0;
    for (int i = 0; i < size; i++) {
...