Question

In my game im currently working on a basic main menu and im looking for a few things.

  1. Currently the only way to see which Jslider is which is by hovering over it.. any suggestions?

  2. When i change the JSlider brightness or the JSlider audio it doesnt actualy update until i switch tabs and return.

  3. When i close the game and reopen it the settings return to default. how to create a variable to preserve settings?

all code is here https://github.com/FeatheredOrcian/Kingdomcraft.git

if you would rather just see the class that the code is partaining to https://github.com/FeatheredOrcian/Kingdomcraft/blob/master/Kingdomcraft/src/com/pointlight/kingdomcraft/render/Render.java

thx

Was it helpful?

Solution

  1. I'd suggest you add a JLabel next to your slider
  2. You need to update the setToolTipText in your statechangelistener. Whenever the state changes, you want you need to let your slider know that it's tooltiptext has changed.
  3. You want to use something like Preferences. Preferences API Overview

*As MadProgrammer mentioned in the comments. Avoid using setBounds, setSize or setLocation. Please review Layout Managers. The only reason I kept them in the below code is so you could see how to implement the suggestions to the 3 questions you asked.

Below is an example of the code changes that I mentioned above. Good luck!

        settings.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                remove(createWorld);
                remove(addServer);
                repaint();

                // Get stored preferences and specific default values if they
                // do not exist
                final Preferences prefs = Preferences.userNodeForPackage(Render.class);
              soundLevel = prefs.getInt("SOUND_LEVEL", 50);
              lightLevel = prefs.getInt("LIGHT_LEVEL", 50);

              // Add sound label
                JLabel soundLabel = new JLabel("Sound: ");
                soundLabel.setBounds(170, 110, 150, 35);
                soundLabel.setForeground(Color.white);

                add(soundLabel);
                add(sound);
                sound.setBounds(210, 110, 150, 35);
                sound.setMinimum(0);
                sound.setMaximum(100);
                sound.setValueIsAdjusting(true);
                sound.setValue(soundLevel);
                sound.setToolTipText("Audio: " + soundLevel + "%");
                sound.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent e) {

                        soundLevel = sound.getValue();

                        // Update tooltip value
                    sound.setToolTipText("Audio: " + soundLevel + "%");

                    // Save sound value in pref
                 prefs.putInt("SOUND_LEVEL", soundLevel);
                    }
                });

                // Add light label
            JLabel lightLabel = new JLabel("Light: ");
            lightLabel.setBounds(370, 110, 150, 35);
            lightLabel.setForeground(Color.white);

            add(lightLabel);
                add(light);
                light.setBounds(405, 110, 150, 35);
                light.setMinimum(0);
                light.setMaximum(100);
                light.setValueIsAdjusting(true);
                light.setValue(lightLevel);
            light.setToolTipText("Brightness: " + lightLevel + "%");

                light.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent e) {
                        lightLevel = light.getValue();

                        // Update tooltip value
                    light.setToolTipText("Brightness: " + lightLevel + "%");

                    // Save light value in pref
                    prefs.putInt("LIGHT_LEVEL", lightLevel);
                    }
                });
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top