Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.ripco.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "clusardi2k" Subject: Can you get this SwingWorker code to work more than once Message-ID: <5023FE40.56503.calajapr@time.synchro.net> X-Comment-To: All Newsgroups: comp.lang.java.programmer X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 722d940f Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 149 Date: Thu, 09 Aug 2012 18:44:43 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1344537883 69.21.70.65 (Thu, 09 Aug 2012 13:44:43 CDT) NNTP-Posting-Date: Thu, 09 Aug 2012 13:44:43 CDT Organization: tds.net Xref: csiph.com comp.lang.java.programmer:17554 From: clusardi2k@aol.com Here is a project that works perfectly only the first time. This is what it does on the first button press: It starts-up with only a "Start" button. (1) Pressing start displays "Hello World". (2) The three "for" loops are executed in the code. (3) "Hello World" disappears. But, when you press the "Start" button a second time this happens: (1) Displays "Hello World", How do you modify the below code so that the second button press matches the first button press. The code has a button and a label. After answering the above question, another question that I have is: can you make this code better in any way imaginable. Thank you, //Code: package Test_SwingWorker; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.List; import javax.swing.SwingWorker; public class Test_SwingWorker extends javax.swing.JFrame { public Test_SwingWorker() { initComponents(); //The "Hello World" label that is not seen on Start-up jLabel1.setVisible (false); final Non_GUI_Stuff task = new Non_GUI_Stuff(); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //The "Hello World" label that appears when button is pressed jLabel1.setVisible (true); task.execute(); } }); } private void initComponents() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Start"); jLabel1.setText("Hello World"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment .LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(167, 167, 167) .addComponent(jButton1) .addGap(51, 51, 51) .addComponent(jLabel1) .addContainerGap(55, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment .LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLa yout.Alignment.LEADING) .addComponent(jButton1) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1))) .addContainerGap(283, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN G) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN G) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Test_SwingWorker().setVisible(true); } }); } class Non_GUI_Stuff extends SwingWorker { protected Integer doInBackground() throws Exception { //"for" loops mentioned above for (int i = 0;i < 100000; i++) for (int i2 = 0;i2 < 100000; i2++); for (int i3 = 0;i3 < 100000; i3++); return 0; } protected void done() { //The "Hello World" label that disappears jLabel1.setVisible (false); } } private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; } --- BBBS/Li6 v4.10 Dada-1 * Origin: Prism bbs (1:261/38) --- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24