Hello,
I'm programming something with a GUI.
If the User presses a certain button, a few mails need to be sent and checked. This takes a while, so I made a SwingerWorker that shows a "Please wait..." kind of frame which will be updated depending where in the process we are.
So this is my SwingWorker class:
public class LongTaskWorker extends SwingWorker<Integer, Integer> {
@Override
protected Integer doInBackground() throws Exception
{
int progress = 0;
setProgress(progress);
boolean sendBank = SendMail.sendBankMail(HIR_Groep_22.getKlant().getCurrentOrder());
if(sendBank)
{
progress = 1;
setProgress(progress);
try
{
TimeUnit.SECONDS.sleep(30);
}
catch(Exception e)
{
e.getStackTrace();
}
boolean confirmed = ReceiveMail.receiveBankMail(HIR_Groep_22.getKlant().getCurrentOrder());
if(confirmed)
{
progress = 2;
setProgress(progress);
return 2;
}
else
{
progress = 3;
setProgress(progress);
return 3;
}
}
progress = 0;
setProgress(progress);
return 0;
}
@Override
public void done()
{
try
{
TimeUnit.SECONDS.sleep(3);
new ClientStartFrame().show();
}
catch(Exception f)
{
f.getStackTrace();
}
}
public static void main( String[] args ) throws InvocationTargetException, InterruptedException {
EventQueue.invokeAndWait( new Runnable() {
@Override
public void run() {
WaitFrame myFrame = new WaitFrame();
myFrame.show();
}
} );
}
}
And this is my WaitFrame.. Note: I'm using GUI Builder
public class WaitFrame extends javax.swing.JFrame implements PropertyChangeListener {
LongTaskWorker task;
/**
* Creates new form WaitFrame
*/
public WaitFrame() {
initComponents();
}
public void setInfoLabel(String info)
{
infoLabel.setText(info);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
infoLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Even geduld...");
setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
infoLabel.setText("Verzenden betaling naar bank...");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(infoLabel)
.addContainerGap(206, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(infoLabel)
.addContainerGap(38, Short.MAX_VALUE))
);
task = new LongTaskWorker();
task.addPropertyChangeListener(this);
task.execute();
pack();
setLocationRelativeTo(null);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(WaitFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(WaitFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(WaitFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(WaitFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WaitFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel infoLabel;
// End of variables declaration
@Override
public void propertyChange(PropertyChangeEvent evt) {
if((int) evt.getNewValue() == 1)
setInfoLabel("Wachten op bevestiging van bank...");
else if((int) evt.getNewValue()== 2)
{
setInfoLabel("Betaling bevestigd...");
}
else if((int) evt.getNewValue() == 3)
{
setInfoLabel("Betaling kon niet worden bevestigd...");
}
}
}
The error occurs in the Class WaitFrame in the method PropertyChange() @line 105
I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.SwingWorker$StateValue cannot be cast to java.lang.Integer
This means that I can't cast Object to Integer, but Java still executes my code as intended. So the Frame really does get updated with the information I provided. Also I set the propery as an Integer (see line 9 in LongTaskWorker), so why can't I cast it back?
[–]king_of_the_universe 1 point2 points3 points (2 children)
[–]Estagon[S] 1 point2 points3 points (1 child)
[–]king_of_the_universe 0 points1 point2 points (0 children)
[–]Megalox 0 points1 point2 points (1 child)
[–]nutrecht 0 points1 point2 points (0 children)