According to the topic I have problem with updating GUI elements from actionPerformed method of JButton. I found in that topic: Thread sleep inside of actionPerformed method that to update GUI I have to return from actiomPerformed. So it is the problem for me because my JButton code is:
public class JButtonINVOICES extends JButton
{
private static final long serialVersionUID = 1L;
public JButtonINVOICES(...)
{
addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
mainFrameReference.setInvoiceRegisterPanel();
invoiceRegisterPanelReference.updateInvoiceList(...);
}
});
}
}
and setInvoiceRegisterPanel()
just set my panel visible on the main frame after I click my button. The Panel contain JTable component. Till now everything is ok but the second method updateInvoiceList(...)
is responsible for updating the JTable component on added Panel. Till now also everything is ok. Problem occurs when I add JProgressBar to my panel and try to update that progressBar while the second method is executed. It doesn't work because GUI doeasn't update since I'm in actionPerformed method. So my question is how to deal with that because I can't observe my JProgressBar loading. It appears just after all JTable is loaded and the value of progressBar is 100%. I hope I explained that clear.
updateInvoiceList code:(pb is JProgressBar object)
public void updateInvoiceList(int month, int year)
{
Vector<String> v;
int num = 0;
int amountToLoad = getAmountToLoad(month,year);
pb.setMinimum(0);
pb.setMaximum(amountToLoad);
if(amountToLoad==0)
{
pb.setMaximum(1);
pb.setValue(1);
clearProductTable();
return;
}
Connection conn = Connector.getConnection();
clearProductTable();
try
{
PreparedStatement prep = conn.prepareStatement("select * from Invoices where print_date like ? and print_date like ?;");
prep.setString(1, "%."+month+".%");
prep.setString(2, "%."+year+" %");
ResultSet rs = prep.executeQuery();
while (rs.next())
{
num++;
v = new Vector<>();
v.addElement(Integer.toString(num));
String number = rs.getString(2);
v.addElement("<html><b>FV / "+number+" / "+rs.getString(3)+"</b></html>");
v.addElement(findClient(rs.getString(6)));
String[] date = getSellDate(rs.getInt(1));
v.addElement(date[0]);
v.addElement(date[1]);
v.addElement(rs.getString(7));
v.addElement(rs.getString(8));
v.addElement(number);
pb.setValue(num);
try{Thread.sleep(30);} catch (InterruptedException e){}
model.addRow(v);
}
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}