Java JFrame Parent runProgressDialog(Runnable runnable, Frame parentFrame, String title, int count)

Here you can find the source of runProgressDialog(Runnable runnable, Frame parentFrame, String title, int count)

Description

Run progress dialog.

License

Open Source License

Parameter

Parameter Description
runnable the runnable
parentFrame the parent frame
title the title
count the count

Declaration

public static void runProgressDialog(Runnable runnable, Frame parentFrame, String title, int count) 

Method Source Code


//package com.java2s;
/*/* w  w w.j a  va2  s . c om*/
 *-------------------
 * The ProgressBarUtil.java is part of ASH Viewer
 *-------------------
 * 
 * ASH Viewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ASH Viewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ASH Viewer.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright (c) 2009, Alex Kardapolov, All rights reserved.
 *
 */

import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JProgressBar;

public class Main {
    /**
     * Run progress dialog.
     * 
     * @param runnable the runnable
     * @param parentFrame the parent frame
     * @param title the title
     * @param count the count
     */
    public static void runProgressDialog(Runnable runnable, Frame parentFrame, String title, int count) {
        JProgressBar progressBar = createJProgressBar(count);
        JDialog dialog = createProgressDialog(parentFrame, title, progressBar);
        runProgressBar(runnable, dialog);
    }

    /**
     * Run progress dialog.
     * 
     * @param runnable the runnable
     * @param parentFrame the parent frame
     * @param title the title
     */
    public static void runProgressDialog(Runnable runnable, Frame parentFrame, String title) {
        JProgressBar progressBar = createJProgressBar(title);
        JDialog dialog = createProgressDialog(parentFrame, title, progressBar);
        runProgressBar(runnable, dialog);
    }

    /**
     * Creates the jprogress bar.
     * 
     * @param count the count
     * 
     * @return the j progress bar
     */
    public static JProgressBar createJProgressBar(int count) {
        JProgressBar progressBar = new JProgressBar(0, count);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        return progressBar;
    }

    /**
     * Creates the j progress bar.
     * 
     * @param title the title
     * 
     * @return the j progress bar
     */
    public static JProgressBar createJProgressBar(String title) {
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        progressBar.setStringPainted(true);
        progressBar.setString(title);
        return progressBar;
    }

    /**
     * Creates the progress dialog.
     * 
     * @param parentFrame the parent frame
     * @param title the title
     * @param progressBar the progress bar
     * 
     * @return the j dialog
     */
    public static JDialog createProgressDialog(Frame parentFrame, String title, JProgressBar progressBar) {
        JDialog dialog = new JDialog(parentFrame, true);
        dialog.setContentPane(progressBar);
        dialog.setTitle(title);
        char c = '\u012C';
        byte byte0 = 50;
        int x = (parentFrame.getX() + parentFrame.getWidth() / 2) - 150;
        int y = (parentFrame.getY() + parentFrame.getHeight() / 2) - 25;
        dialog.setBounds(x, y, 300, 50);
        return dialog;
    }

    /**
     * Run progress bar.
     * 
     * @param runnable the runnable
     * @param dialog the dialog
     */
    private static void runProgressBar(final Runnable runnable, final JDialog dialog) {
        Thread worker = new Thread() {

            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    dialog.dispose();
                }
                return;
            }
        };
        worker.start();
        dialog.setVisible(true);
    }
}

Related

  1. installOperation(final RootPaneContainer frame, final int condition, final KeyStroke keyStroke, final String actionKey, Action action)
  2. isFrameModified(RootPaneContainer frame)
  3. okToWriteFile(Frame parent, String fileName)
  4. oops(Frame parent, String s)
  5. rootFrame(@Nonnull Component component)
  6. setFrameParentTitle(Container c, String s)
  7. showDialog(Frame aParent, String aMsg, String aTitle, int aType)
  8. showDialog(Frame parent, String title, JComponent c)
  9. showDoneDialog(Frame parent, String title, JComponent c)