4
I have an application that lists records in a JTable
, and each record has a registration date using Date
.
On this list, I put one 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 the print:
That 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 must be 2013, up to 5 years more than the current?
I created this code to do this, but I’d like to know if there’s any way to optimize this, preferably without using loop loop, 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);
}
}
}
Obs.: The filter works normally, my doubt is only in relation to the dynamic creation of this list to popular the JCombobox
.
In the end, I ended up using this version with
for
even.– user28595