-1

I have a code below

               jSlider1.setValue(0); 
               int i =0;
               while (i <= jSlider1.getMaximum()) {


                    jSlider1.setValue(i);


            // JOptionPane.showMessageDialog(rootPane,"deded");
             Thread.sleep(2000);
              i++;
        }

What I want is I want to move JSlider automatically its min to max value. I have writeen above code. But I can not get the output. But when I remove the comment of "JOptionPane.showMessageDialog(rootPane,"deded"); " , It's work properly with the message. Please help me. I am in big trouble. I like to have a sample code if any one know the sollution. Thank you

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75

1 Answers1

0

I am assuming you have create some JPanel or JFrame where you add this slider so,

final JSlider jSlider1 = new JSlider();
    jSlider1.setValue(0);
    jSlider1.setMaximum(100);
    jSlider1.setBounds(0, 50, 300, 50);
    final Timer increaseValue = new Timer(50, new ActionListener() {// 50 ms interval in each increase.
        public void actionPerformed(ActionEvent e) {
            if (jSlider1.getMaximum() != jSlider1.getValue()) {
                jSlider1.setValue(jSlider1.getValue() + 1);
            } else {
                ((Timer) e.getSource()).stop();
            }
        }
    });
    panel.add(jSlider1);
    JButton moveSlider = new JButton("Start");
    moveSlider.setBounds(10, 106, 55, 30);
    moveSlider.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            increaseValue.start();
        }
    });
    panel.add(moveSlider);
Yogesh
  • 4,546
  • 2
  • 32
  • 41
  • I am sorry. I do this on netbeans and I don't understand to make this code for nsetbeans. I tried. But it does not give any output. Can you show me how this add to netbeans – user3317730 Feb 17 '14 at 05:48
  • if it solves your problem please accept the answer, it seems you are new here so take a quick tour of stackoverflow http://stackoverflow.com/tour – Yogesh Feb 17 '14 at 05:52
  • Yogesh, I have a small problem again. I put a global variable for time(5th row). But that method do not get the value from that variable. How can I put a variable for the time in here? – user3317730 Feb 17 '14 at 08:46
  • can you create one more question posting your code so others can answer, also please accept above answer if it is satisfying! – Yogesh Feb 17 '14 at 08:48
  • OK I accepted it. Please help me here. I don't have much time. – user3317730 Feb 17 '14 at 09:29
  • but where is your code? I mean I understood you made Instance variable for Timer but I still don't know where your getting problem ,create new question or add code to this question so that we can take a look at it! – Yogesh Feb 17 '14 at 09:31
  • final Timer increaseValue = new Timer(sliderUnitVal, new ActionListener() {// 50 ms interval in each increase. public void actionPerformed(ActionEvent e) { if (jSlider1.getValue() != 85) { jSlider1.setValue(jSlider1.getValue()+1); lblShow.setText(Integer.toString(jSlider1.getValue())) ; } else { ((Timer)e.getSource()).stop(); } } }); – user3317730 Feb 17 '14 at 09:33
  • I put the veriable sliderUnitVal, for time. But then method is not workin properly. But I put the exact value, then it works properly – user3317730 Feb 17 '14 at 09:40
  • Make sure sliderUnitVal is initialized. check if it has certain value assigned to it before it is being refered in Timer, and still problem persists then create new Question cause this is comment section and we need to avoid discussions in it! – Yogesh Feb 17 '14 at 09:47
  • OK. Iwill do it next time. Please help me for this time. I initialized it. And also value is pass to the method. but it is not working for given value. – user3317730 Feb 17 '14 at 09:56