0

Hello I have a java program here and I am trying to make a progress bar. The while loop is within another class and will take a while to run so this GUI is supposed to show progress. While the while loop is running, however, the bar does not move until the while loop is finished then the bar is at 100%. Inside the while loop I have my normal stuff and then I added

WindowWin.pb.setValue(getPercent());

This, however doesnt seem to actually initiate until the loop is over or on the final run through of the loop. While searching on here I've seen suggestions to use

public static void progressUpdate() {
     SwingUtilities.invokeLater(new Runnable() {
         public void run() {
             WindowWin.pb.setValue(getPercent());
         }
     });
}

Im probably not using this right, or this is probably not the fix for my situation. All help appreciated. Thanks.

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class WindowWin extends JFrame implements ActionListener {
    int max=100;
    static int min=0;
    public static boolean stop=false;
    String out;
    JPanel[] row = new JPanel[5];
    JButton[] button = new JButton[5];
    String[] buttonString = { "Go", "Copy to Clipboard", "Back", "Info", "Stop"};

    int[] dimW = { 400, 200, 72, 328};
    int[] dimH = { 40, 100, 25};

    Dimension keyDim = new Dimension(dimW[0], dimH[0]);
    Dimension displayDimension = new Dimension(dimW[0], dimH[1]);
    Dimension butDim = new Dimension(dimW[1], dimH[0]);
    Dimension infoDim = new Dimension(dimW[2], dimH[0]);
    Dimension pbDim = new Dimension(dimW[3], dimH[2]);
    Dimension stopDim = new Dimension(dimW[2], dimH[2]);

    JEditorPane display = new JEditorPane();
    JTextField keyIn = new JTextField(22);
    JEditorPane msgIn = new JEditorPane();
    static JProgressBar pb = new JProgressBar();

    Font font = new Font("Calibri", Font.PLAIN, 14);

    JScrollPane scrollerD = new JScrollPane(display,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    JScrollPane scrollerM = new JScrollPane(msgIn,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        WindowWin c = new WindowWin();
    }

    WindowWin() {
        super("Test");

        // setDesign();
        // setSize(380,250);
        // setSize(460,500);

        // setResizable(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // GridLayout grid = new GridLayout(4,3);

        GridBagLayout grid = new GridBagLayout();
        setLayout(grid);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weighty = 1;

        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
        // FlowLayout South

        for (int i = 0; i < 5; i++)
            row[i] = new JPanel();

        row[0].setLayout(f1);

        for (int i = 1; i < 5; i++) {
            row[i].setLayout(f2);
        }
        for (int i = 0; i < 5; i++) {
            button[i] = new JButton();
            button[i].setText(buttonString[i]);
            button[i].setFont(font);
            button[i].addActionListener(this);
        }
        scrollerD.setBorder(new CompoundBorder(new TitledBorder(
                new EmptyBorder(0, 0, 0, 0), "Out"), scrollerD
                .getBorder()));
        scrollerM.setBorder(new CompoundBorder(new TitledBorder(
                new EmptyBorder(0, 0, 0, 0), "In"), scrollerM
                .getBorder()));
        keyIn.setBorder(new CompoundBorder(new TitledBorder(new EmptyBorder(0,
                0, 0, 0), "Temp"), keyIn
                .getBorder()));

        /*
         * for(int i = 0; i < 2; i++) { label[i] = new JLabel(labelString[i]);
         * row[i].add(label[i]); //label[i].setVerticalAlignment(JLabel.TOP);
         * //label[i].setHorizontalAlignment(JLabel.CENTER);
         * label[i].setText(labelString[i]); }
         */

        display.setFont(font);
        display.setEditable(false);
        display.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        display.setPreferredSize(displayDimension);


        keyIn.setFont(font);
        keyIn.setEditable(true);
        keyIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        keyIn.setPreferredSize(keyDim);

        msgIn.setFont(font);
        msgIn.setEditable(true);
        msgIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        msgIn.setPreferredSize(displayDimension);

        pb.setMinimum(min);
        pb.setMaximum(max);
        pb.setStringPainted(true);
        pb.setPreferredSize(pbDim);

        for (int i = 0; i < 2; i++)
            button[i].setPreferredSize(butDim);
        for (int i = 2; i < 4; i++)
            button[i].setPreferredSize(infoDim);

        button[4].setPreferredSize(stopDim);

        row[0].add(scrollerM);

        add(row[0], gbc);

        row[1].add(button[0]);
        row[1].add(button[1]);
        add(row[1], gbc);

        row[2].add(button[2]);
        row[2].add(keyIn);
        row[2].add(button[3]);
        add(row[2], gbc);

        row[3].add(scrollerD);
        add(row[3], gbc);

        row[4].add(pb);
        row[4].add(button[4]);
        add(row[4], gbc);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);

    }

    /*
     * public final void setDesign() { try { UIManager.setLookAndFeel(
     * "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e)
     * { } }
     */
    public static void updateBar(int newValue) {
        pb.setValue(newValue);
      }
    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == button[0]) {

        }

        if (ae.getSource() == button[1]) {

        }
        if (ae.getSource() == button[2]) {

        }

        if (ae.getSource() == button[3]) {
            updateBar(50);
        }

        if (ae.getSource() == button[4]) {

        }
    }

    public void clear() {
        try {
            display.setText("");
        } catch (NullPointerException e) {
        }
    }

    public void outd() {
        display.setText("");
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Meeesh
  • 992
  • 1
  • 8
  • 15
  • `static` is not a mechanism for cross object communication – MadProgrammer Dec 09 '14 at 04:01
  • 3
    *"however, the bar does not move until the while loop is finished then the bar is at 100%. Inside the while loop I have my normal stuff and then I added"* - You're blocking the EDT, preventing it from updating the screen. Take a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – MadProgrammer Dec 09 '14 at 04:05

0 Answers0