Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSlider not updating?

I'm quite the beginner regarding the whole drawing stuff in windows and I'm kinda stuck at the moment. At the moment I'm just testing the things out.

import javax.swing.*;
import java.awt.*; 
import javax.swing.event.*;

public class test extends JFrame
{
    JSlider slider1; 

    public test()
    {
        slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0);
        setLayout(new FlowLayout(FlowLayout.TRAILING));

        add(slider1);
    }

    public void changeValue () 
    {
        slider1.setValue(25);
    }

    public static void main(String args[]) {
        test gui = new test();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(550,250);
        gui.setVisible(true);
    }
}

So I create a JSlider that I call slider1, I give it its orientation and values. When I call the changeValue method it obviously changes the slider1 value. But there's no change on the GUI. Could someone point me into the correct direction? Does it have something with refreshing the GUI?

like image 718
swebonny Avatar asked Mar 07 '12 03:03

swebonny


3 Answers

After initializing your slider1 variable in test() constructor, add these lines so that jslider tick values could be set and visible in the GUI:

slider1.setMajorTickSpacing( 5 );
slider1.setPaintLabels( true );

You can change tick spacing yourself which is set to 5. Adding slider using add() method is not a good practice, use getContentPane().add() instead, so your constructor should look like this:

public test2()
{
    slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0);
    setLayout(new FlowLayout(FlowLayout.TRAILING));
    slider1.setMajorTickSpacing( 5 );
    slider1.setPaintLabels( true );
    this.getContentPane().add(slider1);
}

I have noticed that you are not calling changeValue() method in the main() method. As your method name implies, it seems to be a setter, however you do not give the setting value parametrically, is this a good practice? In my opinion, it is not. And also changeValue() does the same thing with setValue(), why to create a redundant method?. Anyway, you can use this:

public void changeValue (int newValue) 
{
    slider1.setValue(newValue);
}

In your main method, use these statements:

test2 gui = new test2();
gui.changeValue( 25 );

To see the immediate effect of changing slider value, i mean updating it, use a button or some other component, add an ActionListener to it so that you can update slider value upon a click, for example.

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        changeValue( 25 ); // change 25 to desired value.
    }   
});
like image 66
Juvanis Avatar answered Oct 30 '22 04:10

Juvanis


You need to call changeValue somehow:

I just added a JButton with an ActionListener to do this, and managed to make it work for me with the following code:

package testswing;

import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JSliderTest extends JFrame
{
    JSlider slider1; 
    JButton button1; 

    public JSliderTest()
    {
        setLayout(new FlowLayout());

        slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0);
        add(slider1);

        button1 = new JButton("Centre JSlider!");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                changeValue();
            }   
        });
        add(button1);
    }

    public void changeValue () 
    {
        slider1.setValue(25);
    }

    public static void main(String args[]) {
        JSliderTest gui = new JSliderTest();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(550,250);
        gui.setVisible(true);
    }
}
like image 32
mikera Avatar answered Oct 30 '22 04:10

mikera


You are not calling the changeValue() in your main method. Make the changeValue() method and slider1 static and then call changeValue() in main...

static JSlider slider1; 

public Test() {
    slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0);
    setLayout(new FlowLayout(FlowLayout.TRAILING));

    add(slider1);
}

public static void changeValue () {
    slider1.setValue(35);
}

public static void main(String args[]) {
    Test gui = new Test();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(550,250);
    gui.setVisible(true);

    changeValue();
}

And it is best practice to make the the first letter of every word capitalised for Java Class names.

like image 29
neo108 Avatar answered Oct 30 '22 03:10

neo108