List years in JCombobox dynamically from given rule

4

I have an application that lists records in a JTable , and each record has a record date using Date .

In this list, I put a filter per year via JCombobox , where the initial year is what the application started to use (2013), up to 5 years after the current year, as can be seen in print: p>

This list is instantiated by this line:

//Atributo iniciado direto do JFrame
public static final Integer[] listadeAno = {2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020};

...
//listaAno é o nome do JComboBox do JFrame, nesse instante ele já foi instanciado
this.listaAno = new JComboBox(ListaDeOficiosUI.listadeAno);

My question is how to make the generation of this list dynamic, and create a ArrayList of Integer that stores a list of years, where the first is necessarily 2013, up to 5 years longer than the current one? >

I have created this code to do this, but I would like to know if there is any way to optimize this, preferably without using loop repetition, if possible.

//mudei o tipo do atributo para ArrayList
 public static final ArrayList<Integer> listadeAno = new ArrayList<>();
...
public static void setListaDeAnos() {
    //lista de ano dinâmica conforme o ano atual + 5
    int anoAtual = Calendar.getInstance().get(Calendar.YEAR);
    for (int i = 0; 2013 + i <= anoAtual + 5; i++) {
        if (i == 0) {
            ListaDeOficiosUI.listadeAno.add(2013);
        } else {
            ListaDeOficiosUI.listadeAno.add(2013 + i);
        }
    }
}

Note: The filter works normally, my question is only regarding the dynamic creation of this list to popular JCombobox .

    
asked by anonymous 16.12.2015 / 12:01

2 answers

3

Alternatively, you create a class that provides you with a string based on the elements that pass by parameter to it.

import java.util.Iterator;

public class Range implements Iterable<Integer> {
    private int min;
    private int count;

    public Range(int min, int count) {
        this.min = min;
        this.count = count;
    }

    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            private int cur = min;
            private int count = Range.this.count;
            public boolean hasNext() {
                return count != 0;
            }

            public Integer next() {
                count--;
                return cur++; // first return the cur, then increase it.
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
}

The usage would look like this:

public static void setListaDeAnos() {
    int anoAtual = LocalDate.now().getYear();
    ListaDeOficiosUI.listadeAno = new Range(2013, (anoAtual - 2013) + 6)
}

Retired from this SOen response

Either way, I think the way you're doing right now is fine and you have no reason to try to change. The only tip I would give you is to remove that if within for , because that's long-winded.

for (int i = 0; 2013 + i <= anoAtual + 5; i++) {
    ListaDeOficiosUI.listadeAno.add(2013 + i);        
}
    
16.12.2015 / 14:15
2

If your preference is always two years before, now and five years later, a solution like this, I think it would solve.

public static List<Integer> listaDeAnos() {
    int anoAtual = LocalDate.now().getYear();
    return Arrays.asList(anoAtual - 2, anoAtual - 1, anoAtual, 
            anoAtual + 1, anoAtual + 2, anoAtual + 3, anoAtual + 4, anoAtual + 5);
}

Edited

  

With this, it should be possible to generate what you need.

public static List<Integer> listaDeAnos() {
    List<Integer> lista = new ArrayList<>();
    int anoAtual = LocalDate.now().getYear();

    lista.add(anoAtual);
    for (int i = 1; i <= 5; i++) {
        lista.add(anoAtual + i);
    }
    while (anoAtual != 2013) {
        lista.add(0, --anoAtual);
    }
    return lista;
}
    
16.12.2015 / 13:38