Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:46:30+00:00 2026-06-15T14:46:30+00:00

So I have a program that is due tomorrow and I have held out

  • 0

So I have a program that is due tomorrow and I have held out as long as I can trying to figure out what is wrong with my code. First and biggest issue that has had me stumped for quiet a while is my reset buttons action listener. It just wont compile properly and says value not found? I commented it out so I could at least run the program you should be able to see clearly where I commented them out at. Also second issue is I have to make one text field display the MAX value between the two sliders when moving them and the other give me the TOTAL value between the two sliders. I honestly don’t know what to do for the logical end of that and just simply attached both jtextfields to the left slider. For this I would appreciate a push in the right direction? If you give me code I would be much appreciated but I would also like to explain why it works/ what was wrong with my code. Thanks!

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


/**
   This class displays a window with a slider component.
   The user can slide the left or right slider. As the 
    sliders are adjusted it displays the maximum sound 
    level coming from either slider as well as the total.
*/

public class SoundLevels extends JFrame
{
   private JLabel label1, label2, label3, label4;     // Message labels
   private JTextField maxSound;     // Max Sound Level
   private JTextField totalSound;   // Total Sound Level
   private JPanel mpanel;           // Max sound level panel
   private JPanel tpanel;           // Total sound level panel
   private JPanel sliderPanel1;     // Slider panel 1
    private JPanel sliderPanel2;     // Slider panel 2
    private JPanel resetpanel;       // Reset button panel
   private JSlider slider1;         // Left sound adjuster
    private JSlider slider2;         // Right sound adjuster
    private JButton resetButton;     // Reset button
   /**
      Constructor
   */

   public SoundLevels()
   {
      // Set the title.
      setTitle("Sound Levels");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Creates reset button
        resetButton = new JButton("Reset");

      // Create the message labels.
      label1 = new JLabel("Left:  ");
      label2 = new JLabel("Right: ");
        label3 = new JLabel("Max:   ");
        label4 = new JLabel("Total: ");

      // Create the read-only text fields.
      maxSound = new JTextField("0", 10);
      maxSound.setEditable(false);
      totalSound = new JTextField("0", 10);
      totalSound.setEditable(false);

      // Create the slider.
      slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
        slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
      slider1.addChangeListener(new SliderListener());

      // Create panels and place the components in them.
      mpanel = new JPanel();
        resetpanel = new JPanel();
        tpanel = new JPanel();
        sliderPanel1 = new JPanel();
        sliderPanel2 = new JPanel();

        //Add components to panels
        mpanel.add(label1);
      mpanel.add(maxSound);

        tpanel.add(label2);
      tpanel.add(totalSound);

        sliderPanel1.add(label1);
      sliderPanel1.add(slider1);

        sliderPanel2.add(label2);
        sliderPanel2.add(slider2);

        resetpanel.add(resetButton);           

      // Initialize event listener
//      resetButton.addActionListener(new ResetButtonListener());


        // Sets window to a border layout format.
      setLayout(new GridLayout(1, 5));


      // Add the panels to the content pane.
      add(sliderPanel1);
        add(sliderPanel2);
        add(resetpanel);
        add(mpanel);
      add(tpanel);


      // Pack and display the frame.
      pack();
      setVisible(true);
   }

   /**
      Private inner class that handles the event when
      the user clicks the Reset button.
   */

/* COMMENTED THIS OUT SO IT AT LEAST RUNS   
      private class ResetButtonListener implements ActionListener
      {
         public void actionPerformed(ActionEvent e)
         {
         // Set the panel's background to red.
            max = 0;  //should reset sliders to 0
                total = 0; //should reset sliders to 0
         }
      }
*/   

   /**
      Private inner class to handle the change events
      that are generated when the slider is moved.
   */

   private class SliderListener implements ChangeListener
   {
      public void stateChanged(ChangeEvent e)
      {
         int max, total;             

         // Get the slider value.
         max = slider1.getValue();
            total = slider1.getValue();

         // Store the total sound level in its display field.
         totalSound.setText(Integer.toString(total));

         // Store the max sound level in its display field.
         maxSound.setText(Integer.toString(max));
      }
   }

   /*
      The main method creates an instance of the
      class, which displays a window with a slider.
   */

   public static void main(String[] args)
   {
      new SoundLevels();
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:46:32+00:00Added an answer on June 15, 2026 at 2:46 pm

    What I changed was adding a the listener to both sliders-

    slider1.addChangeListener(new SliderListener());
    slider2.addChangeListener(new SliderListener());
    

    And also making a change to the slider listener –

    private class SliderListener implements ChangeListener {
            public void stateChanged(ChangeEvent e) {
                int max = 0;
                int total = 0;
                // Get the slider value.
                int slider1Val = slider1.getValue();
                int slider2Val = slider2.getValue();
                if (slider1Val > slider2Val) {
                    max = slider1Val;
                } else {
                    max = slider2Val;
                }
                total = slider2Val + slider1Val;
                // Store the total sound level in its display field.
                totalSound.setText(Integer.toString(total));
    
                // Store the max sound level in its display field.
                maxSound.setText(Integer.toString(max));
            }
        }
    

    Below is the way the entire edited code –

    package org.dchan.orm;
    
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.JTextField;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    /**
     * This class displays a window with a slider component. The user can slide the
     * left or right slider. As the sliders are adjusted it displays the maximum
     * sound level coming from either slider as well as the total.
     */
    
    public class SoundLevels extends JFrame {
        private JLabel label1, label2, label3, label4; // Message labels
        private JTextField maxSound; // Max Sound Level
        private JTextField totalSound; // Total Sound Level
        private JPanel mpanel; // Max sound level panel
        private JPanel tpanel; // Total sound level panel
        private JPanel sliderPanel1; // Slider panel 1
        private JPanel sliderPanel2; // Slider panel 2
        private JPanel resetpanel; // Reset button panel
        private JSlider slider1; // Left sound adjuster
        private JSlider slider2; // Right sound adjuster
        private JButton resetButton; // Reset button
    
        /**
         * Constructor
         */
    
        public SoundLevels() {
            // Set the title.
            setTitle("Sound Levels");
    
            // Specify an action for the close button.
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Creates reset button
            resetButton = new JButton("Reset");
    
            // Create the message labels.
            label1 = new JLabel("Left:  ");
            label2 = new JLabel("Right: ");
            label3 = new JLabel("Max:   ");
            label4 = new JLabel("Total: ");
    
            // Create the read-only text fields.
            maxSound = new JTextField("0", 10);
            maxSound.setEditable(false);
            totalSound = new JTextField("0", 10);
            totalSound.setEditable(false);
    
            // Create the slider.
            slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
            slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
            slider1.addChangeListener(new SliderListener());
            slider2.addChangeListener(new SliderListener());
            // Create panels and place the components in them.
            mpanel = new JPanel();
            resetpanel = new JPanel();
            tpanel = new JPanel();
            sliderPanel1 = new JPanel();
            sliderPanel2 = new JPanel();
    
            // Add components to panels
            mpanel.add(label1);
            mpanel.add(maxSound);
    
            tpanel.add(label2);
            tpanel.add(totalSound);
    
            sliderPanel1.add(label1);
            sliderPanel1.add(slider1);
    
            sliderPanel2.add(label2);
            sliderPanel2.add(slider2);
    
            resetpanel.add(resetButton);
    
            // Initialize event listener
            // resetButton.addActionListener(new ResetButtonListener());
    
            // Sets window to a border layout format.
            setLayout(new GridLayout(1, 5));
    
            // Add the panels to the content pane.
            add(sliderPanel1);
            add(sliderPanel2);
            add(resetpanel);
            add(mpanel);
            add(tpanel);
    
            // Pack and display the frame.
            pack();
            setVisible(true);
        }
    
        /**
         * Private inner class that handles the event when the user clicks the Reset
         * button.
         */
    
        /*
         * COMMENTED THIS OUT SO IT AT LEAST RUNS
         */private class ResetButtonListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                // Set the panel's background to red.
                // max = 0; // should reset sliders to 0
                // total = 0; // should reset sliders to 0
                slider1.setValue(0);
                slider2.setValue(0);
            }
        }
    
        /*
    */
    
        /**
         * Private inner class to handle the change events that are generated when
         * the slider is moved.
         */
    
        private class SliderListener implements ChangeListener {
            public void stateChanged(ChangeEvent e) {
                int max = 0;
                int total = 0;
                // Get the slider value.
                int slider1Val = slider1.getValue();
                int slider2Val = slider2.getValue();
                if (slider1Val > slider2Val) {
                    max = slider1Val;
                } else {
                    max = slider2Val;
                }
                total = slider2Val + slider1Val;
                // Store the total sound level in its display field.
                totalSound.setText(Integer.toString(total));
    
                // Store the max sound level in its display field.
                maxSound.setText(Integer.toString(max));
            }
        }
    
        /*
         * The main method creates an instance of the class, which displays a window
         * with a slider.
         */
    
        public static void main(String[] args) {
            new SoundLevels();
        }
    }
    

    Creation of the listener requires a bit of logical understanding. But you need to understand that to get an event you from both sliders the listener should be applied to both. The gist for the entire code is available at – https://gist.github.com/4193279.

    For future reference I would suggest you to break your question to few parts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ program that crashed due to a bug. It would not
I have a downloaded C++ utility program that dies due to an error while
I have a program that I want to distribute, without giving the source code
We have a C# program that performs some timing measurements. Unfortunately, the first time
On Windows 7, I have a command-line program that fails due to file write permission
I have a python program that uses a custom-built DLL. This DLL crashes due
I have a rather large program that uses Hibernate for its ORM needs. Due
I have a java program that stops often due to errors which is logged
I have a working python 2.7 program that calls a DLL. I am trying
I have program that requires Python 3, but I develop Django and it uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.