Ok in this snippet of code using NB 6.0.1 I have create a swing List box and I want to fill it from the results of a query. The List box can actually go ahead and list the results of the query by using the toArray() function. But I wanted to clean up the data first and get rid of the square brackets.
In this example I wanted to list 2 fields, the record id followed by a comma then the problem in the knowledgebase table.
// Connect EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("PHJSupport-app-clientPU");
EntityManager em = emf.createEntityManager();
lstRESULTS.removeAll();
try {
Query q = em.createNativeQuery("select id,problem from Knowledgebase");
Object[] results=q.getResultList().toArray();
int icount;
// now clean them up
for (icount=0;icount<results.length;++icount) {
results[icount]=results[icount].toString().replace("[", "");
results[icount]=results[icount].toString().replace("]", "");
}
lstRESULTS.setListData(results.clone());
} catch (Exception e) {
System.out.println("Error " + e.getMessage());
}
Note that I could replace the code
results[icount]=results[icount].toString().replace("[", "");
results[icount]=results[icount].toString().replace("]", "");
with
results[icount]=results[icount].toString().replace("[", "").replace("]","");
Remember loops are costly and so we want to make things as efficient as possible.