Reflecting the value on a JSlider based on clicking JButtons

142 Views Asked by At

I am working on GUIs in my Java class. We had to create a Car class that held a brake and accelerate method as well as setters and getters for make, model and color. We then had to create a GUI and I have everything EXCEPT getting the slider to reflect the speed when I click the JButtons. The text field will change but not the knob. I figured out how to use the knob to change the text field, but not vice versa. Any help will be amazing.

public class CarView extends JFrame
{
    //Declare a new panel to hold contents
    private JPanel panel;
    private JColorChooser colorChooser;
    //Declare a new instance variable from Car class
    private Car car;
    //Create a constructor that will use object from Car class with new car parameter
    private Color color = (Color.WHITE);
    public CarView(Car newCar)
    {
        //reference the current object and initialize to newCar
        this.car = newCar;
        //Give the window a title
        setTitle("The Car");
        //Set the size of the window
        setSize(400, 400);
        //Give the window instruction for program to exit when closed
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout layout = new GridLayout (8,1);
        //Create a new panel 
        JPanel contents = new JPanel();
        contents.setLayout(layout);
        
        //Create and add new label to panel
        contents.add(new JLabel("Enter the year of your car: " ));
        //Create a new textfield to hold user input for model year
        JTextField modelYearTextField = new JTextField();
        modelYearTextField.setEditable(false);
        modelYearTextField.setText(String.valueOf(newCar.getModelYear()));
        //Set the size of characters to be held in text field
        modelYearTextField.setColumns(10);
        //Add text field to panel
        contents.add(modelYearTextField);
        
        //Create and add new label to panel
        contents.add(new JLabel("Enter the make of your car: " ));
        //Create a new text field to hold user input for make
        JTextField makeTextField = new JTextField();
        makeTextField.setEditable(false);
        makeTextField.setText(String.valueOf(newCar.getMake()));
        //Set the size of characters to be held in text field
        makeTextField.setColumns(10);
        //Add text field to panel
        contents.add(makeTextField);
        
        //Create and add new label to panel
        contents.add(new JLabel("The speed of your car is: "));
        //Create a new text field  to hold user input for speed
        JTextField speedTextField = new JTextField();
        //Set the size of characters to be held in text field
        speedTextField.setColumns(5);
        //Add text field to panel
        contents.add(speedTextField);
        
        JSlider speedSlider = new JSlider (JSlider.HORIZONTAL, 0, 100, 0);
        speedSlider.setMajorTickSpacing(10);
        speedSlider.setMinorTickSpacing(5);
        speedSlider.setPaintTicks(true);
        speedSlider.setPaintLabels(true);
        JPanel sliderPanel = new JPanel();
        //speedSlider.addChangeListener(new SliderListener());
        contents.add(speedSlider);
        contents.add(sliderPanel);
        speedSlider.addChangeListener(new ChangeListener() 
        {
            public void stateChanged(ChangeEvent e) { 
                
                // when manually changing the slider via mouse, set textfield text accordingly
                speedTextField.setText(speedSlider.getValue() + "");
            }
        });
       
        
        
        
        
        //Create a new button for accelerate
        JButton accelerateButton = new JButton("Accelerate");
        //Add the action listener to the button that will hold the same parameters as listener
        accelerateButton.addActionListener(new AccelerateListener(newCar, speedTextField));
        //Add button to panel
        contents.add(accelerateButton);
        
        //Create a new button for brake
        JButton brakeButton = new JButton("Brake");
        //Add the action listener to the button that will hold the same parameters as listener
        brakeButton.addActionListener(new BrakeListener(newCar, speedTextField));
        //Add button to panel
        contents.add(brakeButton);
        
        
        
        
        JButton colorButton = new JButton("Choose car color");
        
        contents.add(colorButton);
        JTextField colorTextField = new JTextField();
        //Set the size of characters to be held in text field
        colorTextField.setColumns(5);
        contents.add(colorTextField);
        colorButton.addActionListener(new ColorListener(newCar, colorTextField));
        
        //Add panel to window
        add(contents);
        
        //Set window visbility to true
        setVisible(true);
        
        pack();
        
        
        
    }

And for my ChangeListener for accelerate and brake (brake looks the same but with car.brake called instead) And the methods in my Car class accelerate at 8 mph and brake at 5 mph.

public class AccelerateListener implements ActionListener
{
    //Declare a new instance variable from Car class
    private Car car;
    //Declare the text field to be read
    private JTextField speedTextField;
    
    //Create a constructor
    public AccelerateListener(Car newCar, JTextField newSpeedTextField)
    {
        //reference the current object and initialize to newCar
        this.car= newCar;
        //Initialize the text field to the paramater
        speedTextField = newSpeedTextField;
        
        
        
    }
    public void actionPerformed(ActionEvent e)
    {
        //Call the accelerate method on the reference object
        this.car.accelerate();
        //Get the speed from the text box and convert to a string
        speedTextField.setText(String.valueOf(car.getSpeed()));
        
        
    }
}

I understand that I have to create an ActionListener for the slider but I can't get the values on the slider to reflect the value when I click the JButtons for brake or accelerate. The text field changes but not the knob.

0

There are 0 best solutions below