Example usage for javax.swing JComboBox removeAll

List of usage examples for javax.swing JComboBox removeAll

Introduction

In this page you can find the example usage for javax.swing JComboBox removeAll.

Prototype

public void removeAll() 

Source Link

Document

Removes all the components from this container.

Usage

From source file:com.cch.aj.entryrecorder.frame.EntryJFrame.java

private int FillProductComboBox(JComboBox comboBox, int id, int mouldId) {
    int result = -1;
    comboBox.removeAll();
    List<Product> allProducts = this.productService.GetAllEntities();
    if (allProducts.size() > 0) {
        List<Product> products = allProducts.stream()
                .filter(x -> x.getMouldId() != null && x.getMouldId().getId() == mouldId)
                .collect(Collectors.toList());
        if (products.size() > 0) {
            List<ComboBoxItem<Product>> productNames = products.stream().sorted(comparing(x -> x.getCode()))
                    .map(x -> ComboBoxItemConvertor.ConvertToComboBoxItem(x, x.getCode(), x.getId()))
                    .collect(Collectors.toList());
            Product product = new Product();
            product.setId(0);//from w w w  . j  av  a2 s . c o m
            product.setCode("- Select -");
            productNames.add(0, new ComboBoxItem<Product>(product, product.getCode(), product.getId()));
            ComboBoxItem[] productNamesArray = productNames.toArray(new ComboBoxItem[productNames.size()]);
            comboBox.setModel(new DefaultComboBoxModel(productNamesArray));
            if (id != 0) {
                ComboBoxItem<Product> currentProductName = productNames.stream().filter(x -> x.getId() == id)
                        .findFirst().get();
                result = productNames.indexOf(currentProductName);
            } else {
                result = 0;
            }
            comboBox.setSelectedIndex(result);
        }
    }
    return result;
}