Example usage for javax.swing JProgressBar setValue

List of usage examples for javax.swing JProgressBar setValue

Introduction

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

Prototype

@BeanProperty(bound = false, preferred = true, description = "The progress bar's current value.")
public void setValue(int n) 

Source Link

Document

Sets the progress bar's current value to n .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyTreeNode root = new MyTreeNode(1, 0);
    DefaultTreeModel model = new DefaultTreeModel(root);
    JProgressBar bar = new JProgressBar();
    PropertyChangeListener progressListener = new PropertyChangeListener() {
        @Override/*from w  ww  . j  a v  a2 s  .  c o m*/
        public void propertyChange(PropertyChangeEvent evt) {
            bar.setValue((Integer) evt.getNewValue());
        }
    };
    JTree tree = new JTree();
    tree.setShowsRootHandles(true);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            TreePath path = event.getPath();
            if (path.getLastPathComponent() instanceof MyTreeNode) {
                MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                node.loadChildren(model, progressListener);
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {

        }
    });
    tree.setModel(model);
    root.loadChildren(model, progressListener);
    frame.add(new JScrollPane(tree));
    frame.add(bar, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JProgressBar pb = new JProgressBar(0, 100);
    ReadFileWorker worker = new ReadFileWorker();
    worker.addPropertyChangeListener(new PropertyChangeListener() {
        @Override/*from ww w . ja va  2 s. co  m*/
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
                pb.setValue(worker.getProgress());
            }
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(pb);
    f.pack();
    f.setVisible(true);
    worker.execute();
}

From source file:Main.java

public static void main(String[] args) {
    final JProgressBar progressBar = new JProgressBar(0, 10);

    final CounterTask task = new CounterTask();
    task.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equals(evt.getPropertyName())) {
                progressBar.setValue((Integer) evt.getNewValue());
            }/* w w  w  . ja v  a2 s  . c  om*/
        }
    });
    JButton startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.execute();
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.cancel(true);
        }
    });

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);
    buttonPanel.add(cancelButton);

    JPanel cp = new JPanel();
    LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS);
    cp.setLayout(layout);
    cp.add(buttonPanel);
    cp.add(progressBar);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextArea textArea = new JTextArea(10, 20);
    final JProgressBar progressBar = new JProgressBar(0, 10);

    final CounterTask task = new CounterTask();
    task.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equals(evt.getPropertyName())) {
                progressBar.setValue((Integer) evt.getNewValue());
            }/*  w  ww.j  a  v a2  s. co  m*/
        }
    });
    JButton startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.execute();
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.cancel(true);
        }
    });

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);
    buttonPanel.add(cancelButton);

    JPanel cp = new JPanel();
    LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS);
    cp.setLayout(layout);
    cp.add(buttonPanel);
    cp.add(new JScrollPane(textArea));
    cp.add(progressBar);

    JFrame frame = new JFrame("SwingWorker Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame parentFrame = new JFrame();
    parentFrame.setSize(500, 150);//from   w  ww . j a va  2  s  .  co  m
    JLabel jl = new JLabel();
    jl.setText("Count : 0");

    parentFrame.add(BorderLayout.CENTER, jl);
    parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    parentFrame.setVisible(true);

    final JDialog dlg = new JDialog(parentFrame, "Progress Dialog", true);
    JProgressBar dpb = new JProgressBar(0, 500);
    dlg.add(BorderLayout.CENTER, dpb);
    dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.setSize(300, 75);
    dlg.setLocationRelativeTo(parentFrame);

    Thread t = new Thread(new Runnable() {
        public void run() {
            dlg.setVisible(true);
        }
    });
    t.start();
    for (int i = 0; i <= 500; i++) {
        jl.setText("Count : " + i);
        dpb.setValue(i);
        if (dpb.getValue() == 500) {
            dlg.setVisible(false);
            System.exit(0);

        }
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    dlg.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);
    // Get the current value
    int value = progress.getValue();

    // Get the minimum value
    int min = progress.getMinimum();

    // Get the maximum value
    int max = progress.getMaximum();

    // Change the minimum value
    int newMin = 0;
    progress.setMinimum(newMin);// www. j ava2s .com

    // Change the maximum value
    int newMax = 256;
    progress.setMaximum(newMax);

    // Set the value; the new value will be forced into the bar's range
    int newValue = 33;
    progress.setValue(newValue);

}

From source file:Main.java

public static void main(String[] args) {
    JProgressBar progressBar = new JProgressBar();
    JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);/*from w  w w .ja va2 s  .  c om*/
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    Timer timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final Timer timer;
    final JProgressBar progressBar = new JProgressBar();
    final JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);/* w ww. ja  va 2 s  . com*/
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JProgressBar pb = new JProgressBar();
    JTextArea ta = new JTextArea(10, 20);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(ta));
    frame.add(pb, BorderLayout.SOUTH);
    frame.pack();//  ww  w  .ja v a 2 s . c o m
    frame.setVisible(true);

    Timer timer = new Timer(250, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            index++;
            if (index >= 100) {
                ((Timer) (e.getSource())).stop();
            }
            ta.append("Line " + index + "\n");
            pb.setValue(index);
        }
    });
    timer.setRepeats(true);
    timer.setCoalesce(true);
    timer.start();
}

From source file:org.jdesktop.swingworker.AccumulativeRunnable.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Prime Numbers Demo");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JTextArea textArea = new JTextArea();
    textArea.setEditable(false);//  www  .  jav a 2s  .  com
    frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
    PrimeNumbersTask task = new PrimeNumbersTask(textArea, 10000);
    final JProgressBar progressBar = new JProgressBar(0, 100);
    progressBar.setIndeterminate(true);
    frame.add(progressBar, BorderLayout.NORTH);

    frame.setSize(500, 500);
    frame.setVisible(true);

    task.addPropertyChangeListener(
        new PropertyChangeListener() {
            public  void propertyChange(PropertyChangeEvent evt) {
                if ("progress".equals(evt.getPropertyName())) {
                    progressBar.setIndeterminate(false);
                    progressBar.setValue((Integer)evt.getNewValue());
                }
            }
        });
    task.execute();

    return;
}