如何使用JProgressBar显示加载JFrame

时间:2013-04-01 08:31:18

标签: java swing swingworker

我是java的新手。我编写了一个应用程序,它使用来自访问数据库的数据加载JTable。我还创建了一个SwingWorker的子类来在后台加载Jtable日期,由进度条指示如下:

/**
 *
 * @author Mike
 */
public class StockProgressBar extends JPanel {

    private final JProgressBar progressBar;
    private final JDialog progressDialog;

    public StockProgressBar() {
        Window thisWin = SwingUtilities.getWindowAncestor(this);
        progressDialog = new JDialog(thisWin, "Please Wait...");

        progressBar = new JProgressBar(0, 100);
        progressBar.setBorder(BorderFactory.createEmptyBorder(5, 50, 0, 50));
        progressBar.setValue(0);
        progressBar.setIndeterminate(true);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(20, 10, 20, 10));
        contentPane.setPreferredSize(new Dimension(300, 100));
        contentPane.add(progressBar);

        progressDialog.setContentPane(contentPane);
        progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        progressDialog.setIconImage(Toolkit.getDefaultToolkit().getImage(
                this.getClass().getResource("Resources/Load.png")));

        progressDialog.pack();
        progressDialog.setLocationRelativeTo(null);

        progressDialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
        final Task task = new Task("StockControl");
        task.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equalsIgnoreCase("progress")) {
                    int progress = (Integer) evt.getNewValue();
                    if (progress == 0) {
                        progressBar.setIndeterminate(true);
                    } else {
                        progressBar.setIndeterminate(false);
                        progressBar.setString(null);
                        progressBar.setValue(progress);
                        progressDialog.dispose();
                    }
                }
            }
        });
        progressDialog.setVisible(true);
        task.execute();

    }

    private class Task extends SwingWorker<Void, Void> {

        private static final int SLEEP_TIME = 1000;
        private String text;

        public Task(String text) {
            this.text = text;
        }

        @Override
        public Void doInBackground() {
            Random random = new Random();
            int progress = 0;
            setProgress(0);
            try {
                Thread.sleep(random.nextInt(SLEEP_TIME));
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
            }
            new StockHome().setVisible(true);//I would like to show that this JFrame is loading
            progress += random.nextInt(10);
            setProgress(Math.min(progress, 100));
            return null;
        }

        @Override
        public void done() {
            System.out.println(text + " is done Loading");
            Toolkit.getDefaultToolkit().beep();
        }
    }
}

这是正确的方法吗?还是有更好的方法?

0 个答案:

没有答案