API Jena - Button "Next" of my interface is not working properly
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 < ind开发者_JAVA百科ice && 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());
}
}
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.
精彩评论