Example usage for javax.swing JProgressBar setBackground

List of usage examples for javax.swing JProgressBar setBackground

Introduction

In this page you can find the example usage for javax.swing JProgressBar setBackground.

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "The background color of the component.")
public void setBackground(Color bg) 

Source Link

Document

Sets the background color of this component.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JProgressBar bar = new JProgressBar(JProgressBar.VERTICAL);
    bar.setEnabled(true);/*from w  ww  . j a va  2s  .co  m*/

    bar.setBackground(Color.YELLOW);
    bar.setForeground(Color.GREEN);

    bar.setStringPainted(true);
    bar.setString("2000 g");
    bar.setValue(65);
    frame.setLayout(new BorderLayout());
    frame.add(bar, BorderLayout.CENTER);
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:org.kchine.rpf.PoolUtils.java

public static String cacheJar(URL url, String location, int logInfo, boolean forced) throws Exception {
    final String jarName = url.toString().substring(url.toString().lastIndexOf("/") + 1);
    if (!location.endsWith("/") && !location.endsWith("\\"))
        location += "/";
    String fileName = location + jarName;
    new File(location).mkdirs();

    final JTextArea area = ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) ? new JTextArea() : null;
    final JProgressBar jpb = ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) ? new JProgressBar(0, 100) : null;
    final JFrame f = ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) ? new JFrame("copying " + jarName + " ...")
            : null;/*from w w w  .j a  va 2 s  .  c o m*/

    try {
        ResponseCache.setDefault(null);
        URLConnection urlC = null;
        Exception connectionException = null;
        for (int i = 0; i < RECONNECTION_RETRIAL_NBR; ++i) {
            try {
                urlC = url.openConnection();
                connectionException = null;
                break;
            } catch (Exception e) {
                connectionException = e;
            }
        }
        if (connectionException != null)
            throw connectionException;

        InputStream is = url.openStream();
        File file = new File(fileName);

        long urlLastModified = urlC.getLastModified();
        if (!forced) {
            boolean somethingToDo = !file.exists() || file.lastModified() < urlLastModified
                    || (file.length() != urlC.getContentLength() && !isValidJar(fileName));
            if (!somethingToDo)
                return fileName;
        }

        if ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) {

            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        f.setUndecorated(true);
                        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                        area.setEditable(false);

                        area.setForeground(Color.white);
                        area.setBackground(new Color(0x00, 0x80, 0x80));

                        jpb.setIndeterminate(true);
                        jpb.setForeground(Color.white);
                        jpb.setBackground(new Color(0x00, 0x80, 0x80));

                        JPanel p = new JPanel(new BorderLayout());
                        p.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                        p.setBackground(new Color(0x00, 0x80, 0x80));
                        p.add(jpb, BorderLayout.SOUTH);
                        p.add(area, BorderLayout.CENTER);
                        f.add(p);
                        f.pack();
                        f.setSize(300, 80);
                        locateInScreenCenter(f);
                        f.setVisible(true);
                        System.out.println("here");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            if (SwingUtilities.isEventDispatchThread())
                runnable.run();
            else {
                SwingUtilities.invokeLater(runnable);
            }
        }

        if ((logInfo & LOG_PRGRESS_TO_SYSTEM_OUT) != 0) {
            System.out.println("Downloading " + jarName + ":");
            System.out.print("expected:==================================================\ndone    :");
        }

        if ((logInfo & LOG_PRGRESS_TO_LOGGER) != 0) {
            log.info("Downloading " + jarName + ":");
        }

        int jarSize = urlC.getContentLength();
        int currentPercentage = 0;

        FileOutputStream fos = null;
        fos = new FileOutputStream(fileName);

        int count = 0;
        int printcounter = 0;

        byte data[] = new byte[BUFFER_SIZE];
        int co = 0;
        while ((co = is.read(data, 0, BUFFER_SIZE)) != -1) {
            fos.write(data, 0, co);

            count = count + co;
            int expected = (50 * count / jarSize);
            while (printcounter < expected) {
                if ((logInfo & LOG_PRGRESS_TO_SYSTEM_OUT) != 0) {
                    System.out.print("=");
                }
                if ((logInfo & LOG_PRGRESS_TO_LOGGER) != 0) {
                    log.info((int) (100 * count / jarSize) + "% done.");
                }

                ++printcounter;
            }

            if ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) {
                final int p = (int) (100 * count / jarSize);
                if (p > currentPercentage) {
                    currentPercentage = p;

                    final JTextArea fa = area;
                    final JProgressBar fjpb = jpb;
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            fjpb.setIndeterminate(false);
                            fjpb.setValue(p);
                            fa.setText("Copying " + jarName + " ..." + "\n" + p + "%" + " Done. ");
                        }
                    });

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            fa.setCaretPosition(fa.getText().length());
                            fa.repaint();
                            fjpb.repaint();
                        }
                    });

                }
            }

        }

        /*
         * while ((oneChar = is.read()) != -1) { fos.write(oneChar);
         * count++;
         * 
         * final int p = (int) (100 * count / jarSize); if (p >
         * currentPercentage) { System.out.print(p+" % "); currentPercentage =
         * p; if (showProgress) { final JTextArea fa = area; final
         * JProgressBar fjpb = jpb; SwingUtilities.invokeLater(new
         * Runnable() { public void run() { fjpb.setIndeterminate(false);
         * fjpb.setValue(p); fa.setText("\n" + p + "%" + " Done "); } });
         * 
         * SwingUtilities.invokeLater(new Runnable() { public void run() {
         * fa.setCaretPosition(fa.getText().length()); fa.repaint();
         * fjpb.repaint(); } }); } else { if (p%2==0) System.out.print("="); } }
         *  }
         * 
         */
        is.close();
        fos.close();

    } catch (MalformedURLException e) {
        System.err.println(e.toString());
        throw e;
    } catch (IOException e) {
        System.err.println(e.toString());

    } finally {
        if ((logInfo & LOG_PRGRESS_TO_DIALOG) != 0) {
            f.dispose();
        }
        if ((logInfo & LOG_PRGRESS_TO_SYSTEM_OUT) != 0) {
            System.out.println("\n 100% of " + jarName + " has been downloaded \n");
        }
        if ((logInfo & LOG_PRGRESS_TO_LOGGER) != 0) {
            log.info(" 100% of " + jarName + " has been downloaded");
        }
    }

    return fileName;
}