javaswingif-statementjslider

I'm trying to get Slide.Get_Value() to print "Five" whenever it gets the number 5 as a test


package com.company.Iguana;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.InputEvent;

public class MyCPS implements ChangeListener {
    JSlider Slider;
    JPanel panel;
    JLabel label;
    JFrame frame;
    JComboBox Combo;

    MyCPS() {
        frame = new JFrame("CPS");
        panel = new JPanel();
        label = new JLabel();
        Slider = new JSlider();
        String[] Buttons = {"Left Click", "Right Click"};
        JComboBox combo = new JComboBox(Buttons);
        ImageIcon image = new ImageIcon("BOAROR.png");
        frame.setIconImage(image.getImage());
        frame.getContentPane().setBackground(Color.YELLOW);
        frame.setTitle("Iguana.exe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 420);
        Slider = new JSlider(0, 23, 12);
        Slider.setPreferredSize(new Dimension(400, 200));
        Slider.setPaintTrack(true);
        Slider.setMajorTickSpacing(4);
        Slider.setPaintLabels(true);
        Slider.setOrientation(SwingConstants.VERTICAL);
        label.setText("CPS" + Slider.getValue());
        label.setIcon(image);
        Slider.addChangeListener(this);
        panel.add(combo);
        panel.add(Slider);
        panel.add(label);
        frame.add(panel);
        frame.setSize(420,420);
        frame.setVisible(true);
        frame.pack();


    }

    @Override
    public void stateChanged(ChangeEvent e) {
        label.setText("CPS" + Slider.getValue());
        if(e.equals(Slider.getValue()));              <<<<<<<< Problem
        System.out.println("Five");

}

}

Whenever I try putting if(e.equals(Slider.getValue())); == 5 It says Operator '==' cannot be applied to 'java.lang.Object', 'int'

I'm just trying to get it whenever I put the CPS as 5 It would print "Five" Back as a test I'm quite new so I probably did some silly mistake


Solution

  • The error is because you are trying to compare the object, e of type, ChangeEvent with Slider.getValue() which returns an int.

    You should write it as

    if(Slider.getValue() == 5) {            
        System.out.println("Five");
    }
    

    Check JSlider#getValue to learn more about it.