Results 1 to 5 of 5
Thread: Did I do this THREAD correctly?
- 04-24-2010, 05:03 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Did I do this THREAD correctly?
Someone told me the quickest way to learn a new language was to develop a project using it. That maybe true, but it also might be the quickest way how to learn bad programming skills in your new language as well. I love JAVA, it really seems to be a fantastic language. I am not a programmer by trade but I am developing a program for my coworkers. I have a GUI based application that has an initialization phase that may take 2 to 3 secs depending on the users computer power. I wanted to give some feedback on progress so I wanted to update a progress bar during the 3 sec load period. I'm sure your guessing what I'm going to say next. The bar didn't update during the load process. Correct! So I read about threads....and read some more about threads....came here and read even more about threads. I finally got the nerve up to attempt to move my initialization method into a spun off thread. Here is what I came up with:
Java Code:public void loadTheLines() { task = new LoadTheLinesTask(); task.execute(); } class LoadTheLinesTask extends SwingWorker<Void, Void> { @Override public Void doInBackground() { //public void loadTheLines() { JDialog loadProgressBox = null; if (loadProgressBox == null) { JFrame mainFrame = BiditApp.getApplication().getMainFrame(); loadProgressBox = new BiditLoadLinesProgressJDialog(mainFrame); loadProgressBox.setLocationRelativeTo(mainFrame); } BiditApp.getApplication().show(loadProgressBox); ((BiditLoadLinesProgressJDialog) loadProgressBox).updateProgress(10); try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(BiditView.class.getName()).log(Level.SEVERE, null, ex); } . . .(create some files, load some arrays, read some files) ((BiditLoadLinesProgressJDialog) loadProgressBox).updateProgress(50); try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(BiditView.class.getName()).log(Level.SEVERE, null, ex); . . .(read some more files, load some more arrays and hashtables) ((BiditLoadLinesProgressJDialog) loadProgressBox).updateProgress(90); try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(BiditView.class.getName()).log(Level.SEVERE, null, ex); . . .(finish loading and calculating) ((BiditLoadLinesProgressJDialog) loadProgressBox).updateProgress(100); try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(BiditView.class.getName()).log(Level.SEVERE, null, ex); ((BiditLoadLinesProgressJDialog) loadProgressBox).dispose(); return null; } ------------------------------------------------- separate file: package bidit; /** * * @author USER */ public class BiditLoadLinesProgressJDialog extends javax.swing.JDialog { /** Creates new form BiditLoadLinesProgressJDialog */ public BiditLoadLinesProgressJDialog(java.awt.Frame parent) { super(parent); initComponents(); } public void updateProgress(int percent) { loadLinesProgressBar.setValue(percent); percentageJLable.setText(percent + "% Complete"); } /** 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() { jLabel1 = new javax.swing.JLabel(); loadLinesProgressBar = new javax.swing.JProgressBar(); percentageJLable = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setName("Form"); // NOI18N setResizable(false); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bidit.BiditApp.class).getContext().getResourceMap(BiditLoadLinesProgressJDialog.class); jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N jLabel1.setName("jLabel1"); // NOI18N loadLinesProgressBar.setName("loadLinesProgressBar"); // NOI18N percentageJLable.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); percentageJLable.setText(resourceMap.getString("percentageJLable.text")); // NOI18N percentageJLable.setName("percentageJLable"); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(percentageJLable, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(loadLinesProgressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(loadLinesProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(percentageJLable) .addContainerGap(34, Short.MAX_VALUE)) ); pack(); }// </editor-fold> // Variables declaration - do not modify private javax.swing.JLabel jLabel1; private javax.swing.JProgressBar loadLinesProgressBar; private javax.swing.JLabel percentageJLable; // End of variables declaration }
I place a call to "loadTheLines()" and it spins off the thread and opens the progress bar JDialog box. Works great! But how bad is the code? Next I would like to try the invokeLater technique but I will have to figure out how to implement it on this written code.
Any constructive criticism would be appreciated.
-
I must admit that I didn't look at all of your code, but your SwingWorker seems a bit confused to me as it is doing Swing code from within the doInBackground section, and that shouldn't be.
- 04-24-2010, 05:22 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Oh no!
Well, I get the updated bar correctly.
What exactly do you mean by "that shouldn't be". I shouldn't be able to do that? I shouldn't do that because I might crash the program? I shouldn't do that because it's not syncronised? I shouldn't do that because ...well I can't think of other stuff but I knew it shouldn't work on my 50th attempt. I figured it must be much harder.
-
Among other threads, your code will have an EDT or event dispatch thread where Swing code is run and the background thread that is created by the SwingWorker. Everything called in the doInBackground method is done on the background thread off of the EDT (hence the name "doInBaciground"). This is to allow your code to do long-running tasks without tying up the EDT. By calling Swing code explicitly off of the EDT you risk causing crashes that happen sporadically, and you lose all of the benefits of the background thread. I strongly urge you to read: Concurrency in Swing
-
Also, please look at the progress bar tutorial and especially the sample code that comes with it. How to use Progress Bars. The sample code uses a SwingWorker, a JProgressBar and a PropertyChangeListener to monitor the changes in the state of the SwingWorker.
Similar Threads
-
java run correctly in the upper directory
By dangr in forum New To JavaReplies: 1Last Post: 01-29-2010, 06:52 PM -
how to scale correctly ?
By h9h in forum Java 2DReplies: 10Last Post: 10-29-2009, 07:06 AM -
If statement not executing correctly
By gligor_kot in forum New To JavaReplies: 5Last Post: 08-03-2009, 01:46 AM -
[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 5Last Post: 05-06-2008, 04:45 PM -
How Do I Embed Java Correctly To A Web Page
By abcd in forum Java AppletsReplies: 7Last Post: 01-28-2008, 07:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks