How do I switch between the next element and the previous element of a ListIterator with just one click?

5

I have a list of words stored in my listaPalavra variable already initialized with values, of type ArrayList<T> :

listaPalavra.add("Palavra 1");
listaPalavra.add("Palavra 2");
listaPalavra.add("Palavra 3");
listaPalavra.add("Palavra 4");
listaPalavra.add("Palavra 5");

From this list of words I get the itarator list as follows:

listIterator = listaPalavra.listIterator();

I intend to use my list of iterator listIterator to navigate the elements using the next() and previous() methods and display them on the form.

This is the form:

NavigationisdonebythePreviousandNextbuttonsasfollows:

ButtonroutinePrevious:

privatevoidbtnAnteriorActionPerformed(java.awt.event.ActionEventevt){if(listIterator.hasPrevious()){lbValor.setText(listIterator.previous());}}

ButtonroutineNext:

privatevoidbtnProximoActionPerformed(java.awt.event.ActionEventevt){if(listIterator.hasNext()){lbValor.setText(listIterator.next());}}

CompletecodeoftheformIcreatedasanexampletobereproducedbyyou:

importjava.util.ArrayList;importjava.util.List;importjava.util.ListIterator;publicclassMainFrameExemploextendsjavax.swing.JFrame{List<String>listaPalavra=newArrayList<>();ListIterator<String>listIterator;publicMainFrameExemplo(){initComponents();listaPalavra.add("Palavra 1");
        listaPalavra.add("Palavra 2");
        listaPalavra.add("Palavra 3");
        listaPalavra.add("Palavra 4");
        listaPalavra.add("Palavra 5");

        listIterator = listaPalavra.listIterator();

        String primeiroElemento = listaPalavra.get(0);

        lbValor.setText(primeiroElemento);
    }

    //Método para inicializar os componentes visuais, o NetBeans gera para você.        
    private void initComponents() {
    ...
    }

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {                                            
        if (listIterator.hasPrevious()) {
            lbValor.setText(listIterator.previous());
        }
    }                                           

    private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (listIterator.hasNext()) {
            lbValor.setText(listIterator.next());
        }        
    }                                          

    public static void main(String args[]) {                
        java.awt.EventQueue.invokeLater(() -> 
        {
            new MainFrameExemplo().setVisible(true);
        });
    }

    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnProximo;
    private javax.swing.JLabel lbValor;    
}

My problem

Every time I switch between the next element and the previous element I have to double-click the button as well as the Next button as in the Previous . I would like to switch between the next and previous elements by just clicking the button.

See this gif that shows my problem:

How can I solve this problem?

    
asked by anonymous 31.10.2016 / 02:11

1 answer

6

Hello, young man!

Man, this is an easier question to explain by drawing, but I will try for text.

The iterator references the list in question and also references the current item. When you reach one of the extremes, for example in the item "Word 5", when giving a next, it will automatically go to the next item. It turns out that this item does not exist, but the reference pointer in the list has moved. So when you give a listIterator.previous() it will keep the value "Word 5" because it was outside the range of the list, then it goes back to the last item in the list.

The key to fixing is to check whether you have reached any of these extremes and already "rewind" or "advance" to the end item.

private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (listIterator.hasPrevious()) {
        String previous = listIterator.previous();
        lbValor.setText(previous);
        if ( !listIterator.hasPrevious()  ) listIterator.next();
    }
}                                           

private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if ( listIterator.hasNext()  ) {
        lbValor.setText(listIterator.next());
        if ( !listIterator.hasNext()  ) listIterator.previous();
    }        
}  

Another change is at the moment the frame is instantiated. In it you are getting item 0 from the vector. The cool thing is that you already use the iterator itself:

    listIterator = listaPalavra.listIterator();
    String primeiroElemento = listIterator.next();
    lbValor.setText(primeiroElemento);

Greetings!

Issue 01

After placing the gif, it was noticed that it was necessary to take another direction and use the index to access the message.

No main, it's better to use it

   listIterator = listaPalavra.listIterator();
   listIterator.next();
   String primeiroElemento = listaPalavra.get(listIterator.nextIndex() - 1);
   lbValor.setText(primeiroElemento);

And in the listiners like this:

private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (listIterator.hasPrevious() && listIterator.previousIndex() > 0) {
        listIterator.previous();
        lbValor.setText( listaPalavra.get(listIterator.nextIndex() - 1) );
    }
}                                           

private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if ( listIterator.hasNext()  ) {
        listIterator.next();
        lbValor.setText( listaPalavra.get(listIterator.nextIndex() - 1) );
    }        

}      
    
31.10.2016 / 02:55