The Property Change Event Of Progress Bar Not Firing
Asked Answered
O

2

1

I am using JProgressBar for Showing The Progress Of Data Loaded From from DataBase . I am using SwingWorker Class To Load The Data In BackGround Thread using Tutorial!

The Property Change Event of ProgressBar Of my application is not firing.

Please guide me what i am doing wrong???.

Below is the SSCCE of the code i am trying to use is as follows.

public final class JProgressBarApplication {

    //private static org.jdesktop.swingx.JXDatePicker dpDate;
    javax.swing.JPanel pnlDate;
    JLabel label;
    JProgressBar pb;
    boolean taskDone=false;
    LongRunProcess lrpTask;

    public static void main(String... aArgs) {
        JProgressBarApplicationapp = new JProgressBarApplication();
        app.buildAndDisplayGui();
    }

    // PRIVATE //
    private void buildAndDisplayGui() {

        JFrame frame = new JFrame("Test Frame");
        buildContent(frame);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static final class showDialog implements ActionListener {

        /**
        * Defining the dialog's owner JFrame is highly recommended.
        */
        showDialog(JFrame aFrame) {    
            Date serverDate = null;
            fFrame = aFrame;    

        }

        public void GetData() {    
        // Code To Get Data From DB    
        }

        public void actionPerformed(ActionEvent aEvent) {

            JOptionPane.showMessageDialog(fFrame, "This is a dialog");
        }
        private JFrame fFrame;
    }

    private void buildContent(JFrame aFrame) {

        JPanel panel = new JPanel();

        panel.add(new JLabel("Hello"));

        JButton ok = new JButton("OK");
        ok.addActionListener(new MinimalSwingApplication.showDialog(aFrame));
        panel.add(ok);

        pb = new JProgressBar(0, 20);
        pb.setValue(0);
        pb.setStringPainted(true);
        aFrame.addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        pb.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                pbPropertyChange(evt);
            }
        });         

        aFrame.getContentPane().add(panel);
        aFrame.getContentPane().add(pb);
        aFrame.getContentPane().add(label);
    }

    private void formWindowOpened(java.awt.event.WindowEvent evt) {

        // TODO add your handling code here:
        lrpTask = new LongRunProcess(this, "1", 0);
        taskDone =false;
        lrpTask.execute();
        lrpTask.addPropertyChangeListener(pb.getPropertyChangeListeners()[0]);
    }

    private void pbPropertyChange(java.beans.PropertyChangeEvent evt) {
        // TODO add your handling code here:
        if (!taskDone) {
            if (lrpTask !=null) {
                int progress = lrpTask.getProgress();
                pb.setValue(progress);
                String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
                String.format( "Completed %d%% of task.\n", progress) + "</b>" + "</font>" + "</html>";
                if (lrpTask.getProgress()==100) {
                    done() ;
                }
            }
        }
    }

    public void done() {
        //Tell progress listener to stop updating progress bar.
        taskDone = true;
        Toolkit.getDefaultToolkit().beep();
        pb.setValue(pb.getMinimum());
        String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + " Form Loading completed." + "</b>" + "</font>" + "</html>";
        label.setText(str);
    }      
}

class LongRunProcess extends SwingWorker {
    /**
     * @throws Exception
     */
    MinimalSwingApplication frm;
    String mID=null;
    int rowNo=0 ;
    LongRunProcess(MinimalSwingApplication jframe,String mID_,int rowNo_) {
        frm =jframe;
        mID=mID_;
        rowNo=rowNo_ ;
    }
    @Override
    protected Object doInBackground() throws Exception {
        Integer result = 0;               

        try {                        
            frm.GetData();                       
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

Edit: with @mKorbel answer

I use the following function

 private void getLongRunningTask(String id_ ,int row_){
  final String id=id_;
  final int row=row_;
    Thread process=  new Thread(new Runnable() {
      @Override
        public void run() {
      GetData(id,row);
      }
  });
  process.start();

}

Instead Of using SwingWorker object Before.

  lrpTask = new LongRunProcess(this, "1", 0);
  taskDone =false;
  lrpTask.execute();

Get The following Exceptions

    debug:
    /JewelleryERPApplication/build/classes/java.lang.ArrayIndexOutOfBoundsException: 8 >= 8
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at Utilities.Utility.ChangeJTableColumnSize(Utility.java:304)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.RefreshReplicateDetailJTable(RcvGoodsFromFiler.java:2850)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetDetailData(RcvGoodsFromFiler.java:496)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:638)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3323)
at java.lang.Thread.run(Thread.java:722)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at sun.swing.SwingUtilities2.convertColumnIndexToModel(SwingUtilities2.java:1841)
at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2585)
at javax.swing.JTable.getColumnClass(JTable.java:2701)
at javax.swing.plaf.synth.SynthTableUI$SynthTableCellRenderer.getTableCellRendererComponent(SynthTableUI.java:790)
at javax.swing.JTable.prepareRenderer(JTable.java:5735)
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at                        javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at   java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 14 >= 0
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:648)
at javax.swing.plaf.synth.SynthTableHeaderUI.paint(SynthTableHeaderUI.java:173)
at javax.swing.plaf.synth.SynthTableHeaderUI.update(SynthTableHeaderUI.java:144)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at  javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at  java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "Thread-44" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.removeElementAt(Vector.java:554)
at javax.swing.table.DefaultTableColumnModel.removeColumn(DefaultTableColumnModel.java:151)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1286)
at javax.swing.JTable.tableChanged(JTable.java:4389)
at javax.swing.JTable.setModel(JTable.java:3691)
at  jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetDetailData(RcvGoodsFromFiler.java:582)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:642)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3323)
at java.lang.Thread.run(Thread.java:722)
    java.lang.IllegalArgumentException: Identifier not found
at javax.swing.table.DefaultTableColumnModel.getColumnIndex(DefaultTableColumnModel.java:282)
at javax.swing.JTable.getColumn(JTable.java:2564)
at Utilities.Utility.AddingExtraColumnsAndComboForViews(Utility.java:2513)
at Utilities.Utility.RefreshReplicatedDataAndHeader(Utility.java:2307)
at  jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.RefreshReplicateDetailJTable(RcvGoodsFromFiler.java:2804)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetDetailData(RcvGoodsFromFiler.java:496)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:638)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3323)
at java.lang.Thread.run(Thread.java:722)
    Exception in thread "Thread-49" java.lang.NumberFormatException: For input string: "E2856D+RWN"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at  jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.CalcWeight(RcvGoodsFromFiler.java:774)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:652)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3323)
at java.lang.Thread.run(Thread.java:722)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7 >= 1
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:577)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:725)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at  java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "Thread-50" java.lang.NumberFormatException: For input string: "E2856D+RWN"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.CalcWeight(RcvGoodsFromFiler.java:774)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:652)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3323)
at java.lang.Thread.run(Thread.java:722)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8 >= 4
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:577)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at  javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Exception in thread "Thread-56" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.removeElementAt(Vector.java:554)
at javax.swing.table.DefaultTableColumnModel.removeColumn(DefaultTableColumnModel.java:151)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1286)
at javax.swing.JTable.tableChanged(JTable.java:4389)
at javax.swing.JTable.setModel(JTable.java:3691)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetDetailData(RcvGoodsFromFiler.java:582)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:642)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3323)
at java.lang.Thread.run(Thread.java:722)

Edit For Code Generating Exceptions

Sample Exception1

    .ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.removeElementAt(Vector.java:554)
at javax.swing.table.DefaultTableColumnModel.removeColumn(DefaultTableColumnModel.java:151)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1286)
at javax.swing.JTable.tableChanged(JTable.java:4389)
at javax.swing.JTable.setModel(JTable.java:3691)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetDetailData(RcvGoodsFromFiler.java:493)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.GetData(RcvGoodsFromFiler.java:638)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler$13.run(RcvGoodsFromFiler.java:3380)

Code At Which Exception occured

    DefaultTableModel dtm = new DefaultTableModel(data_RcvGoodsFromFilerDt, header_RcvGoodsFromFilerDt);

     tblDetailInfo.setModel(dtm);

Sample Exception 2

   Exception in thread "Thread-6" java.lang.NumberFormatException: For input string: "12.950"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at jewelleryerpapplication.GUI.Gold.RcvGoodsFromFiler.CalcWeight(RcvGoodsFromFiler.java:774)

Code At Which Exception occured

   qt += Integer.parseInt( tblDetailInfo.getValueAt(r, 8).toString() );

    Some Exceptions occured at unknown Code Point

    at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
Obligatory answered 24/11, 2012 at 4:29 Comment(16)
What part are you stuck on? For what it's worth, your code above doesn't comply with the SSCCE standard since it requires at least one non-core Java class and has much code unrelated to the main problem, the latter making it more difficult to see and understand your main problem. Also, I don't see any background threading or use of a SwingWorker just yet.Raker
Actually the progress bar starts when my Data is being loaded not fiulfilling my requirement.Obligatory
I want that the Progress Bar works in parallel to the getData() function what shall i do for thatObligatory
I remove un related code nowObligatory
Again, are you using a SwingWorker to load the data in a background thread?Raker
No i am not using SwingWorkerObligatory
Then that's likely the solution that you are looking for. If you desire concurrency and also desire to not freeze your GUI, then you must use background threading. A SwingWorker simplifies this and also ties in nicely with JProgressBars. Please check out the tutorials that show just this.Raker
let us continue this discussion in chatObligatory
You never set any progress in your SwingWorker. Use the setProgress method and it will fire eventsCranny
@SyedMuhammadMubashir Take a look at the code in this example: #13525671 . The problem is that your are not publishing any intermediate result.Genova
Please edit your question and format the code to make it at least readable (following java naming conventions might help its readability as well ;-) - and don't forget to make it an SSCCE, as mentioned already!Thrombokinase
Possible duplicate of JProgressBar update from SwingWorkerSaveloy
@GuillaumePolet how can i use your example as you have used SwingWorker<void,Integer> example also the process takes List<Integer> as param which is not in my case.Obligatory
@Cranny can you tell how can i use the loop you used to simulate long running task if my Long Running Task is DB select transaction. as used in your example!Obligatory
@SyedMuhammadMubashir Read the Javadoc of SwingWorker. The method process is typed process(List<V> chunks) where V is the first type argument of SwingWorker. Although you can use that type and the process method to indicate the current progress of your work, you can also use setProgress and attach a PropertyChangelistener to the property 'progress' of the SwingWorker to update the UI. The important thing is to ensure that your "task" is reporting intermediate progress.Genova
See my latest edit on question using Runnable Thread and i am getting bunch of exceptions as he mentioned in his answer that Runnable is better than SwingWorkerObligatory
P
3
Paz answered 24/11, 2012 at 10:26 Comment(16)
+1 I was struck by the second bullet, but it's just a flesh wound. :-)Saveloy
"This answer compares SwingWorker and Runnable for similar action" with this example i can not understand how to use this example as u hhave used the wait method , and i am using Slow DataBase Transactions. By use of SwingWorker the speed of my application has been dramatically improved but is generating exceptins in some cases @PazObligatory
@Paz please see the exceptions i edited my question by the runnable.Obligatory
all arrays starting with zero (0 :-), debug that because you put 9 elements to the 8.th model, better could be to edit your question with code generated this issuePaz
@Paz Question has been edited with code generating the exceptions.Obligatory
(I think) never to change model from models event, did you able 1) never to parse objects from JTable or its model change column to Double, testing by add TableModelListener, use System.out.println()Paz
be sure that exceptions this type are only about funny/crazy typos in code, sure I made that 1Mio times,Paz
and they are coming only some times. When i refresh my form more frequently.Obligatory
???? whaaaats :-) :-) When i refresh my form more frequently. then there must be Concurency issue, have to debug and properly timing code from/to, sure in most cases is better to wrote a new code and to forgot about oldPaz
@Paz can you please tell how to update the model if i load the latest data from Database .Obligatory
@Paz can you tell how to handle concurrency in my case.Obligatory
no idea, but you can to start with great code by @camickrPaz
I tried process.sleep(10) and that works for me. Thanks for your valueable guidance.Obligatory
note, process.sleep(10) is under latency proper value is over 15-21 for Windows platform,Paz
which is the best value for 17,18,19,20 or 21Obligatory
I tried 20 and now all the exceptions have been eliminated now thanks once again.Obligatory
S
3

@mKorbel has cited many instructive examples, but the key is invoking setProgress() from the worker's doInBackground(). SwingWorker handles the required synchronization. Several helpful answers expand on the topic in this related Q&A, including a reference to this example.

Saveloy answered 24/11, 2012 at 13:28 Comment(8)
the example u show is using Thread.Sleep() to show Slow Process in doInBackGound method of SwingWorker but i am using simply long running DataBase transaction in which time taken is unknown to me. Can you tell how to cater this problem. @SaveloyObligatory
This example shows executeQuery(). It also adds a little Thread.sleep() to simulate latency.Saveloy
you showed the above example is doing the same thing and it implements the same logic on the loop of the result set . In my case i don't know the time of the completion of my query as it is the result of some heavy stored proc execution. @SaveloyObligatory
your's and this example uses the SwingWorker<List<Double>, Double> as i am using Simple SwingWorker()Obligatory
in my case frm.GetData() is the result of the the execution of many sps and updating many table models in my form.Obligatory
How can i use your for (int i = 1; i <= N; i++) { x = x - (((x * x - 2) / (2 * x))); setProgress(i * (100 / N)); publish(Double.valueOf(x)); Thread.sleep(1000); // simulate latency } return Double.valueOf(x); as instead of Thread.sleep(1000) frm.GetData() in my case contains lots of Stored procs execution with un-known time.Obligatory
That's mean i can not use loopingObligatory
Yes, the examples are contrived to show percent complete. You could query a row count at the beginning of your loop, and use that in the denominator of setProgres().Saveloy

© 2022 - 2024 — McMap. All rights reserved.