List years in Jcombobox dynamically from certain rule

Asked

Viewed 438 times

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:

inserir a descrição da imagem aqui

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.

2 answers

3


Alternatively, you create a class that provides you with a sequence based on the elements you pass per 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 use would look like this:

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

Withdrawn of that reply by Soen


Anyway, I think the way you’re currently doing is fine and you have no reason to try to change. The only hint I’d give you is to remove that if within the for, 'Cause that is long.

for (int i = 0; 2013 + i <= anoAtual + 5; i++) {
    ListaDeOficiosUI.listadeAno.add(2013 + i);        
}
  • In the end, I ended up using this version with for even.

2

If your preference is always two years earlier, current and five years later, a solution like this, I think 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;
}
  • The rule would be to start from 2013 until 5 years after the current year. It has to be 2013 because it is the landmark year.

  • I edited the post, check :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.