remove From JList List - Java Swing

Java examples for Swing:JList

Description

remove From JList List

Demo Code


//package com.java2s;

import javax.swing.*;

public class Main {
    public static void removeFromList(JList list, Object object) {
        ListModel model = list.getModel();
        if (model == null) {
            model = new DefaultListModel();
            list.setModel(model);//from  ww  w. j  a  v a2  s  . co  m
        }
        ;
        if (model instanceof DefaultListModel) {
            DefaultListModel listModel = (DefaultListModel) model;
            listModel.removeElement(object);
        }
    }

    public static void removeFromList(JList list, int index) {
        ListModel model = list.getModel();
        if (model == null) {
            model = new DefaultListModel();
            list.setModel(model);
        }
        ;
        if (model instanceof DefaultListModel) {
            DefaultListModel listModel = (DefaultListModel) model;
            listModel.removeElementAt(index);
        }
    }
}

Related Tutorials