Sap Ashish
Sap Ashish

Reputation: 19

JSlider getValue() condition

I've been trying to make a feedback system with JSlider. I want JLabel set to different text according to different JSlider value. For example, if the value is less than 25 I want JLabel to display text as bad, if value is between 25 and 50 Fine, if value is between 50 and 75 then Good, and if the value is above 75 then best. Here is the code:

'''

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

public class slider {

JFrame frame;
JPanel panel;
JLabel label;
JLabel rating;
JSlider slider;

slider() {

    frame = new JFrame("Slider Demo");
    panel = new JPanel();
    label = new JLabel();
    slider = new JSlider(JSlider.VERTICAL, 0, 100, 50);
    rating = new JLabel();
    slider.setPreferredSize(new Dimension(400, 200));
    slider.setPaintTicks(true);
    slider.setMinorTickSpacing(10);
    slider.setPaintTrack(true);
    slider.setMajorTickSpacing(25);
    slider.setPaintLabels(true);
    slider.setFont(new Font("MV Boli", Font.PLAIN, 15));
    label.setFont(new Font("MV Boli", Font.PLAIN, 25));
    rating.setFont(new Font("MV Boli", Font.PLAIN, 25));
    label.setText("Please rate us!  ");
    rating.setText("Fine");

    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            if (value < 25)
                rating.setText("Bad!");
            if (value > 25 && value < 50)
                ;
            rating.setText("Fine!");

            if (value > 50 && value < 75)
                ;
            rating.setText("Good!");

            if (value > 75)
                ;
            rating.setText("Best!");
        }
    });
    panel.setLayout(new FlowLayout());
    panel.add(label);
    panel.add(slider);
    panel.add(rating);
    frame.add(panel);
    frame.setSize(420, 420);
    frame.setVisible(true);

}

public static void main(String args[]) {
    new slider();
}

}

''' The problem is that the label doesn't change text as I wanted. It starts with value 50 as I set and the label reads Fine which is actually fine but when I move the slider up or down the label is set to Best and it won't change no matter how I move my slider. Please help me out. I wrote this code based on my knowledge. I'm not actually aware if there are other methods to make this rating system work.

Upvotes: 0

Views: 352

Answers (1)

Chris
Chris

Reputation: 34109

You have extra semicolons ; at the end of each if statement, which you shouldn't. Instead, it should look like this:

Update:

As @c0der correctly pointed out, it would be more efficient to use else if, rather than having the program to go through each if statement in the code. Thus, the below is modified in accordance with that.

public void stateChanged(ChangeEvent e) {
    int value = slider.getValue();
    System.out.println(value);
    if (value < 25)
        rating.setText("Bad!");
    else if (value > 25 && value < 50)
        rating.setText("Fine!");
    else if (value > 50 && value < 75)
        rating.setText("Good!");
    else if (value > 75)
        rating.setText("Best!");
}

Upvotes: 2

Related Questions