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
.