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)
SwingWorker
. Use thesetProgress
method and it will fire events – Crannyprocess
is typedprocess(List<V> chunks)
whereV
is the first type argument ofSwingWorker
. Although you can use that type and theprocess
method to indicate the current progress of your work, you can also usesetProgress
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