SHARE
TWEET

Untitled

a guest May 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class MyListModel extends AbstractListModel {
  8.  
  9.     private List<String> listElements = new ArrayList<>();
  10.  
  11.     void add(String text) {
  12.         listElements.add(text);
  13.         fireIntervalAdded(text, getSize() - 1, getSize() - 1);
  14.     }
  15.  
  16.     void remove(int index) {
  17.         try {
  18.             String elementToRemove = listElements.get(index);
  19.             listElements.remove(index);
  20.             fireIntervalRemoved(elementToRemove, getSize(), getSize());
  21.         } catch (Exception e) {
  22.             System.out.println("FAILED_TO_REMOVE_ELEMENT");
  23.             e.printStackTrace();
  24.         }
  25.     }
  26.  
  27.     boolean isElementExist(String text){
  28.         for (String listElement : listElements) {
  29.             if (listElement.toLowerCase().equals(text.toLowerCase())) return true;
  30.         }
  31.         return false;
  32.     }
  33.  
  34.     int getIndexOf(String text){
  35.         for(int i =0 ; i<listElements.size(); i++){
  36.             if (listElements.get(i).toLowerCase().equals(text.toLowerCase())) return i;
  37.         }
  38.         return -1;
  39.     }
  40.  
  41.     @Override
  42.     public int getSize() {
  43.         return listElements.size();
  44.     }
  45.  
  46.     @Override
  47.     public Object getElementAt(int index) {
  48.         return listElements.get(index);
  49.     }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top