I am writing a code for a user to choose numbers in a progress bar. For some reason the progress bar does not fill up or empty when the int progress is not exact.

For example: The progress bar value is 5 at the moment. The user chooses 250 Million Cars whose progress value is 7 but the progress bar does not empty. Is there an algorithm or a method that can fix this problem

Thank you.

public class Tester extends JFrame implements PropertyChangeListener
{
    private JButton backJButton;

    private JComboBox carsJComboBox, treesJComboBox;

    private String[] newnumbers = {"Add 250 Million","Add 500 Million","Add 1 billion","Add 1.5 Billion","Add 2 Billion","Remove 250 Million", "Remove 500 Million","Remove 1 billion", "Remove 1.5 Billion", "Remove 2 Billion"};


    private String[] numbers2 = {"Add 100 Million", "Add 500 Million","Add 1 billion","Add 50 Billion"};

    private JLabel carsJLabel, treesJLabel, temperatureJLabel, point1JLabel, point2JLabel, pointmidJLabel, point01JLabel, point02JLabel, point03JLabel, 
    point04JLabel, point06JLabel, point07JLabel, point08JLabel, point09JLabel;

    private JProgressBar progressBar;

    private int currentIndex;

    int time = 0;
    int duration = 150;

    private Task task;
    private int progress = 0;

    class Task extends SwingWorker<Void, Void>
    {
        public Void doInBackground()
        {
            //progress += 1;
            setProgress(progress);
            return null;    
        }

    }

    public void doTask()
    {

        if(carsJComboBox.getSelectedIndex() == 0)
        {
            progress+=7;
            System.out.println(progress);
        }
        if(carsJComboBox.getSelectedIndex() == 1)
        {
            progress+=17;
        }
        if(carsJComboBox.getSelectedIndex() == 2)
        {
            progress+=37;
        }
        if(carsJComboBox.getSelectedIndex() == 3)
        {
            progress+=57;
        }
        if(carsJComboBox.getSelectedIndex() == 4)
        {
            progress+=77;
        }
        if(carsJComboBox.getSelectedIndex() == 5)
        {
            progress-=7;
            System.out.println(progress);
        }


        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    public void doTask2()
    {
        if(treesJComboBox.getSelectedIndex() == 0)
        {
            progress-=17;
        }

        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }


    public Tester()
    {
        createUserInterface();
    }

    private void createUserInterface()
    {
        Container contentPane = getContentPane(); 
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);


        getRootPane().setBorder(BorderFactory.createMatteBorder(6, 6, 6, 6, Color.BLACK));


        backJButton = new JButton("<<Back");
        backJButton.setBounds(0, 0, 100, 30);
        contentPane.add(backJButton);
        backJButton.addActionListener(

                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                            MainWindow window = new MainWindow();
                            closeTester();
                    }

                }

        );

        carsJLabel = new JLabel("Add/Remove Cars");
        carsJLabel.setBounds(142, 420, 135, 21);
        contentPane.add(carsJLabel);

        carsJComboBox = new JComboBox(newnumbers);
        carsJComboBox.setBounds(116,480,175,21);
        carsJComboBox.addActionListener(

                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        doTask();
                    }

                }

        );

        contentPane.add(carsJComboBox);

        treesJLabel = new JLabel("Add/Remove Trees");
        treesJLabel.setBounds(400, 420, 135, 21);
        contentPane.add(treesJLabel);

        treesJComboBox = new JComboBox(numbers2);
        treesJComboBox.setBounds(376,480,175,21);
        treesJComboBox.addActionListener(

                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        doTask2();
                    }

                }

        );




        contentPane.add(treesJComboBox);

        progressBar = new JProgressBar();
        progressBar.setValue(0);
        progressBar.setBounds(97, 120, 500, 250);
        progressBar.setSize(500, 100);
        UIManager.put("ProgressBar.Background", Color.RED);
        contentPane.add(progressBar);

        temperatureJLabel = new JLabel("Change in Temperature (°F)");
        temperatureJLabel.setBounds(240, 80, 300, 50);
        temperatureJLabel.setFont(new Font("Georgia",Font.PLAIN,18));
        contentPane.add(temperatureJLabel);

        point1JLabel = new JLabel("0°");
        point1JLabel.setBounds(97, 180, 100, 30);
        contentPane.add(point1JLabel);

        point2JLabel = new JLabel(".1°");
        point2JLabel.setBounds(580, 180, 100, 30);
        contentPane.add(point2JLabel);

        pointmidJLabel = new JLabel(".05°");
        pointmidJLabel.setBounds(327, 180, 100, 30);
        contentPane.add(pointmidJLabel);

        point01JLabel = new JLabel(".01°");
        point01JLabel.setBounds(127, 180, 100, 30);
        contentPane.add(point01JLabel);

        point02JLabel = new JLabel(".02°");
        point02JLabel.setBounds(177, 180, 100, 30);
        contentPane.add(point02JLabel);

        point03JLabel = new JLabel(".03°");
        point03JLabel.setBounds(227, 180, 100, 30);
        contentPane.add(point03JLabel);

        point04JLabel = new JLabel(".04°");
        point04JLabel.setBounds(277, 180, 100, 30);
        contentPane.add(point04JLabel);

        point06JLabel = new JLabel(".06°");
        point06JLabel.setBounds(377, 180, 100, 30);
        contentPane.add(point06JLabel);

        point07JLabel = new JLabel(".07°");
        point07JLabel.setBounds(427, 180, 100, 30);
        contentPane.add(point07JLabel);

        point08JLabel = new JLabel(".08°");
        point08JLabel.setBounds(477, 180, 100, 30);
        contentPane.add(point08JLabel);

        point09JLabel = new JLabel(".09°");
        point09JLabel.setBounds(527, 180, 100, 30);
        contentPane.add(point09JLabel);


        setSize(700,600);
        setTitle("Climate Tester");
        setVisible(true);
        setResizable(false);

    }


    private void closeTester()
    {
        this.dispose();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt)
    {
        if("progress" == evt.getPropertyName())
        {
            //progress += 1;
            progressBar.setValue(progress);
        }
    }

}

~Problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.