1

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();
    }
}
Community
  • 1
  • 1
user2374573
  • 59
  • 1
  • 3
  • 11
  • Can you show the code with your JProgressBar? – Behe Jul 06 '13 at 16:35
  • 3
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Jul 07 '13 at 02:07
  • Thank you that was helpful. I have got one more question. I start my SwingWorker Thread like that: new MyThread().execute(); Is it a proper way? I wonder if doInBackground method is finished if I should destroy thread object myself or it is destroyed automatically? Thx – user2374573 Jul 07 '13 at 15:38

0 Answers0