1
I am going through a cycle with values, and in each iteration I call a Jframe to choose certain values, what happens is that all Jframes are opened without being able to complete the operation. In my code the "fetchEditors" calls Jframe but does not wait until the operations are completed and continues the cycle. How do I?
try {
conX3 = connection.getConnection();
stmtX3 = conX3.createStatement();
itens = this.getItens();
for (int k = 0; k < itens.length; k++) {
String sql = "SELECT * FROM ALMEDINA.YITMNET WHERE ISBN_0 = '" + itens[k] + "'";
ResultSet rsX3 = stmtX3.executeQuery(sql);
while (rsX3.next()) {
fetchEditoras(rsX3.getString("EDITORA_0"));
}
}
} catch (SQLServerException ex) {
Logger.getLogger(SQLServerException.class.getName()).log(Level.WARNING, null, ex);
} finally {
conX3.close();
stmtX3.close();
}
public ArrayList<Editora> fetchEditoras(String editora) throws ClassNotFoundException, SQLException {
DatabaseNET connectionNet = new DatabaseNET();
ImportX3Net connectionX3Net = new ImportX3Net();
Connection conX3Net = null, conNet = null;
Statement stmtX3Net = null, stmtNet = null;
ArrayList<Editora> editoras = new ArrayList<>();
if (connectionX3Net.getConnection() != null) {
try {
conX3Net = connectionX3Net.getConnection();
conNet = connectionNet.getConnection();
stmtX3Net = conX3Net.createStatement();
stmtNet = conNet.createStatement();
String sql = "SELECT * FROM importXNet.editora WHERE x3_name LIKE '" + editora + "'";
ResultSet rsX3NETEditora = stmtX3Net.executeQuery(sql);
if (stmtX3Net.getFetchSize() == 0) {
sql = "SELECT * FROM loja_online.editoras WHERE editoras_name LIKE '%" + editora + "%'";
ResultSet rsNet = stmtNet.executeQuery(sql);
while (rsNet.next()) {
System.out.println(rsNet.getString("editoras_name"));
editoras.add(new Editora(null, rsNet.getString("editoras_name")));
}
assocEditoras assoc = new assocEditoras(editoras, editora);
assoc.setVisible(true);
rsNet.close();
}
} catch (MySQLDataException ex) {
Logger.getLogger(MySQLDataException.class.getName()).log(Level.SEVERE, null, ex);
} finally {
stmtNet.close();
stmtX3Net.close();
}
}
return editoras;
}
Your problem is in the while cycle? I think you could post your code
fechEditoras
that’s where you open the frame and close without giving time to complete the right operations?– jsantos1991
I just upgraded it, put in the code. It is here : assocAssoc Writers = new assocEditors(publishers, publisher); Assoc.setVisible(true);
– Ricardo Costa
the problem is that it opens a frame by iterating the cycle while, does not wait for the first Jframe to close and complete the works.
– Ricardo Costa
If the problem is synchronization, you can use the reserved word
synchronized
in his methodfetchEditoras
. http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html– Renan Gomes
I guess it’s not really synchronous
– Ricardo Costa
It seems to me a good solution so the cycle only access for the second time to
fetchEditoras
when the former has completed its function.– jsantos1991
I’ll read the documentation. Synchronized stood on the siclo while?
– Ricardo Costa
does not work, Jframe opens, the function continues its execution, but Jframe is open, and will continue the cycle because it has already executed the function.
– Ricardo Costa
It must be a bad resolution but try to see if it solves your problem, create a cycle that only comes out when the frame is no longer visible, do not know the best way to do it, try to use the
.isvisivel
or.isactive
in the while condition and only when returning false is it out– jsantos1991
I know little of java, but with threads will solve the problem?
– Ricardo Costa
I don’t think I quite understand what your real problem is yet, but I’m sure you do, because you could just start the next thread when the last one is over...
– jsantos1991
my problem is basically my complaint here : http://www.coderanch.com/t/338922/GUI/java/opening-multiple-frames-loop
– Ricardo Costa
But there is the solution, even with the code.
– Franchesco
I used a Jdialog instead of a JFRAME with the following Modalitytype: Dialog.ModalityType.APPLICATION_MODAL. I think it is the best solution
– Ricardo Costa