modelos.CustomListModelLibros.java Source code

Java tutorial

Introduction

Here is the source code for modelos.CustomListModelLibros.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelos;

import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Administrador
 */
public class CustomListModelLibros extends AbstractListModel implements ComboBoxModel {

    private ArrayList<Libros> lista = new ArrayList<>();

    @Override
    public int getSize() {
        return lista.size();
    }

    @Override
    public Object getElementAt(int index) {
        Libros p = lista.get(index);
        return "Isbn :" + StringUtils.leftPad(String.valueOf(p.getIsbn()), 15, '0') + "     Titulo: "
                + StringUtils.rightPad(p.getTitulo(), 50, ' ') + "     Autor: "
                + StringUtils.rightPad(p.getAutor(), 30, ' ') + "     Categorias: "
                + StringUtils.rightPad(p.getCategorias(), 30, ' ');
    }

    /**
     *
     * @param p
     */
    public void addLibro(Libros p) {
        lista.add(p);
        this.fireIntervalAdded(this, getSize(), getSize() + 1);
    }

    /**
     *
     * @param index0
     */
    public void eliminarLibro(int index0) {
        lista.remove(index0);
        this.fireIntervalRemoved(index0, getSize(), getSize() + 1);
    }

    /**
     *
     * @param index
     * @return
     */
    public Libros getLibro(int index) {
        return lista.get(index);
    }

    /**
     *
     * @return
     */
    public ArrayList<Libros> getLista() {
        return lista;
    }

    /**
     *
     */
    public void removeAll() {
        lista.clear();
    }

    String selection = null;

    public void setSelectedItem(Object anItem) {
        selection = (String) anItem; // to select and register an
    } // item from the pull-down list

    // Methods implemented from the interface ComboBoxModel
    public Object getSelectedItem() {
        return selection; // to add the selection to the combo box
    }

}