根据主题我从JButton的actionPerformed方法更新GUI元素时遇到问题。我在那个主题中找到了: Thread sleep inside of actionPerformed method 要更新GUI,我必须从actiomPerformed返回。所以这对我来说是个问题,因为我的JButton代码是:
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(...);
}
});
}
}
单击我的按钮后,和setInvoiceRegisterPanel()
只是在主框架上显示我的面板。 Panel包含JTable组件。直到现在一切正常,但第二种方法updateInvoiceList(...)
负责更新添加的Panel上的JTable组件。直到现在一切都还可以。当我将JProgressBar添加到我的面板并尝试在执行第二个方法时更新该progressBar时出现问题。它不起作用,因为GUI不会更新,因为我在actionPerformed方法。所以我的问题是如何处理,因为我无法观察我的JProgressBar加载。它在加载所有JTable并且progressBar的值为100%之后出现。我希望我能解释清楚。
updateInvoiceList代码:( pb是JProgressBar对象)
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();
}
}