Java JProgressBar showProgressBar(String message, int steps)

Here you can find the source of showProgressBar(String message, int steps)

Description

Shows the progress bar.

License

Open Source License

Parameter

Parameter Description
message Message displayed in progress bar.
steps Steps

Return

JProgressBar with message

Declaration

public static JProgressBar showProgressBar(String message, int steps) 

Method Source Code

//package com.java2s;
/** (C) Copyright 1998-2009 Hewlett-Packard Development Company, LP
    //from  w  ww  . j  a va2  s . c o  m
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
    
 This library 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
 Lesser General Public License for more details.
    
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
 For more information: www.smartfrog.org
    
 */

import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.border.SoftBevelBorder;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import java.awt.Rectangle;

public class Main {
    /**
     * Shows the progress bar.
     *
     * @param message Message displayed in progress bar.
     * @param steps   Steps
     * @return JProgressBar with message
     */
    public static JProgressBar showProgressBar(String message, int steps) {
        JProgressBar jpb = new JProgressBar();
        jpb.setMinimum(0);
        jpb.setMaximum(steps);

        JWindow pBarWin = new JWindow();
        JLabel jl = new JLabel(message);
        JPanel jp = new JPanel();
        jp.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
        jp.add(jl);
        jp.add(jpb);
        jp.setSize(jl.getPreferredSize());
        pBarWin.setContentPane(jp);
        pBarWin.setSize(jp.getPreferredSize());
        center(null, pBarWin);
        pBarWin.validate();
        pBarWin.toFront();
        pBarWin.setVisible(true);

        return jpb;
    }

    /**
     * Centres a component inside an AWT container. <p> 
     * If the container is null the component is centered in the
     * screen.</p>
     *
     * @param parent The AWT container
     * @param comp   The component
     */
    public static void center(Container parent, Component comp) {
        int x;
        int y;
        Rectangle parentBounds;
        Dimension compSize = comp.getSize();

        // If Container is null or smaller than the component
        // then our bounding rectangle is the
        // whole screen
        if ((parent == null) || (parent.getBounds().width < compSize.width)
                || (parent.getBounds().height < compSize.height)) {
            parentBounds = new Rectangle(comp.getToolkit().getScreenSize());
            parentBounds.setLocation(0, 0);
        }
        // Else our bounding rectangle is the Container
        else {
            parentBounds = parent.getBounds();
        }

        // Place the component so its center is the same
        // as the center of the bounding rectangle
        x = parentBounds.x + (((parentBounds.width) - (compSize.width)) / 2);
        y = parentBounds.y + (((parentBounds.height) - (compSize.height)) / 2);

        comp.setLocation(x, y);
    }
}

Related

  1. excutePost(String targetURL, String urlParameters, JProgressBar progress)
  2. generateProgressBarFor(Object... objs)
  3. incProgressBar(JProgressBar jpb)
  4. printProgress(JProgressBar prBar, int progressValue)
  5. SetProgressBar(JProgressBar jpb)
  6. updateProgressBase(JProgressBar progressBar, int done, int maximum)