1

我正在使用 JProgressBar 来显示从 DataBase 加载的数据的进度。我正在使用 SwingWorker 类使用教程在后台线程中加载数据!

我的应用程序 ProgressBar 的属性更改事件未触发。

请指导我我做错了什么???。

下面是我尝试使用的代码的 SSCCE 如下。

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;
    }
}

编辑:@mKorbel 回答

我使用以下功能

 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();

}

而不是使用 SwingWorker 对象之前。

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

获取以下异常

    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)

编辑代码生成异常

示例异常1

    .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)

发生异常的代码

    DefaultTableModel dtm = new DefaultTableModel(data_RcvGoodsFromFilerDt, header_RcvGoodsFromFilerDt);

     tblDetailInfo.setModel(dtm);

示例异常 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)

发生异常的代码

   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)
4

2 回答 2

3
于 2012-11-24T10:26:01.730 回答
3

@mKorbel引用了许多有启发性的示例,但关键是setProgress()从工人的doInBackground(). SwingWorker处理所需的同步。在这个相关的问答中,几个有用的答案扩展了这个主题,包括对这个例子的引用。

于 2012-11-24T13:28:45.553 回答