0

I'm using Jena. I created an interface that allows to add, modify and remove instances in rdf file. I have a problem with Button Next. It works but not perfectly. I would like that it returns to the first instance when it reaches the last instance. But it does not do this, when it reaches the last instances, it repeats this last instance every time button Next is pressed. How can I solve this?

Here is my fragment code for button next:

//Button Next
class ActionSuivant implements ActionListener
{
    public void actionPerformed(ActionEvent evt)
    {

        ++indice; 

        ExtendedIterator instances=onto.personne.listInstances();

        Individual instance = null;
                for(p = 0; p < indice && instances.hasNext(); p++)
                {
                   instance = (Individual) instances.next();

                  }    
                tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
                tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
                tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
                tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());

    }
} 
ARH
  • 81
  • 10

1 Answers1

3

At the end of your for loop, if p is still less than indice, then it means that you have reached the end of the list. Reset the indice to 1, and return the first element of the iterator.

public void actionPerformed(ActionEvent evt)
{
    ++indice;
    ExtendedIterator instances = onto.personne.listInstances();
    Individual instance = null;
    Individual firstInstance = null;
    for (p = 0; p < indice && instances.hasNext(); p++) {
        instance = (Individual) instances.next();
        if (firstInstance == null) {
            firstInstance = instance;
        }
    }    
    if (p < indice) {
        indice = 1;
        instance = firstInstance;
    }
    tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
    tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
    tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
    tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());
}

You should also make sure that instance is not null before getting its properties.

It would be easier if you had a List rather than an Iterator: you could simply access the element by index.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you very much for your help JB Nizet. For the button "Previous" the approach is similar I suppose? – ARH Aug 14 '11 at 10:40
  • Yes. But if the Previous button consists, when being at the beginning of the list, in going to the last element, it means that the list is reasonably short, and that you should certainly iterate through all the elements and store them in an ArrayList. This way, your buttons could access the ArrayList by index rather than iterating each time. – JB Nizet Aug 14 '11 at 10:51