0

I have just made a java program with eclipse to change a Jprogress bar with a jslider but the sliders value doesn't change It keeps constant, here is the code:

First class:

package pro;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class pro1 {

int value;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //setting the objects
    final pro1 p = new pro1();
    final pro2 p2 = new pro2();


    //adding the JFrame.
    JFrame fr = new JFrame();
    fr.setVisible(true);
    fr.setSize(380, 80);
    fr.setLayout(new FlowLayout(FlowLayout.LEFT));
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //adding the JProgressBar.
    JProgressBar pb = new JProgressBar(0,100);
    pb.setOpaque(false);
    Color c = new Color (0,200,0);
    pb.setForeground(c);        
    pb.setValue(p.value);


    //adding the JPanel
    JPanel panel = new JPanel();
    panel.add(pb);
    panel.add(p2.slider());

    //adding the panel to the frame.
    fr.add(panel);
    fr.revalidate();

}
}

Second Class:

package pro;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class pro2 {

//setting the object
        pro1 p = new pro1();

public JSlider slider(){


//adding the slider.
        final JSlider s = new JSlider ();
        s.setMinimum(0);
        s.setMaximum(100);
        s.setValue(0);
        s.setMajorTickSpacing(10);
        s.setPaintTicks(true);
        s.addChangeListener(new ChangeListener(){

            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                p.value = s.getValue();
            }

        });

        return s;
}

I have also tried to assign the value of a slider to a value and print it out but it keeped at the default value I set it to which was 0......

anything wrong thing in the code??

1 Answers1

0

In your main method, you are creating an instance of pro1. In pro2 you are creating a new instance of pro1. Each of these instances has its own value.

Additionally, when you change the value, then the progress bar will not notice this. You have to call JProgressBar#setValue explicitly.

There have been some additional issues with the code, here is a slightly modified version that shows ONE possible way of doing it...

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderProgressTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        //adding the JFrame.
        JFrame fr = new JFrame();
        fr.setSize(380, 80);
        fr.setLayout(new FlowLayout(FlowLayout.LEFT));
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //adding the JProgressBar.
        JProgressBar pb = new JProgressBar(0,100);
        pb.setOpaque(false);
        Color c = new Color (0,200,0);
        pb.setForeground(c);        

        //adding the JPanel
        JPanel panel = new JPanel();
        panel.add(pb);
        panel.add(createSlider(pb));

        //adding the panel to the frame.
        fr.add(panel);
        fr.setVisible(true);
    }

    private static JSlider createSlider(final JProgressBar pb)
    {
        //adding the slider.
        final JSlider s = new JSlider ();
        s.setMinimum(0);
        s.setMaximum(100);
        s.setValue(0);
        s.setMajorTickSpacing(10);
        s.setPaintTicks(true);
        s.addChangeListener(new ChangeListener(){

            @Override
            public void stateChanged(ChangeEvent e) {
                pb.setValue(s.getValue());
            }

        });

        return s;
    }
}
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • wow thanks so much it really worked, I didn't understand what was the problem tho.. – user3314952 Feb 16 '14 at 13:18
  • @user3314952 The slider changed the `value` of the instance `pro1 p = new pro1();` that was created in `pro2`. But the progress bar showed the `value` from the `pro1 p = new pro1();` that was created in the main method. So the `value` that was modified with the slider was a different one than the `value` that was shown in the progress bar. (And additionally, the progress bar was not updated) – Marco13 Feb 16 '14 at 15:23