8 Replies Latest reply on Aug 21, 2008 9:24 AM by 843806

    JSlider Problem

    800378
      Hi,

      I want to disable a JSlider so that user cannot change its value at run time but at the same time i want that my JSlider doesn't grey out because i am using it to display values and it look very ugly in grey out form.

      If not the pointer atleast the labels must be in black.

      Any suggestions?
        • 1. Re: JSlider Problem
          843806
          Ravi_Gupta wrote:

          Any suggestions?
          There may be a simpler method but you could just remove all the mouse and keyboard listeners.
          • 2. Re: JSlider Problem
            800378
            There may be a simpler method but you could just remove all the mouse and keyboard listeners. 
            But i have not added any listener to JSlider
            • 3. Re: JSlider Problem
              843806
              Ravi_Gupta wrote:
              There may be a simpler method but you could just remove all the mouse and keyboard listeners. 
              But i have not added any listener to JSlider
              But behind the scenes there must have been listeners added since otherwise how would the JSlider get it's mouse and keyboard input! JSlider is derived from JComponent and there are methods on JComponent for getting the list of mouse listeners, mouse motion listeners, mouse wheel listeners and keyboard listeners. Iterate through these lists and use the appropriate removeXXXX(listener) methods.
              • 4. Re: JSlider Problem
                800378
                I had removed all the listeners as you told me but still i can change its value using keyboard (arrow keys)

                Here is the code which i am using
                        MouseListener[] mls = (MouseListener[])(slider.getListeners(MouseListener.class));
                        for(MouseListener listener : mls)
                            slider.removeMouseListener(listener);
                        
                        MouseMotionListener[] mmls = (MouseMotionListener[])(slider.getListeners(MouseMotionListener.class));
                        for(MouseMotionListener listener : mmls)
                            slider.removeMouseMotionListener(listener);
                        
                        MouseWheelListener [] mwls = (MouseWheelListener[])(slider.getListeners(MouseWheelListener.class));
                        for(MouseWheelListener listener : mwls)
                            slider.removeMouseWheelListener(listener);
                        
                        KeyListener [] kls = (KeyListener[])(slider.getListeners(KeyListener.class));
                        for(KeyListener listener : kls)
                            slider.removeKeyListener(listener);
                Can you tell me what is the problem in my code?
                • 5. Re: JSlider Problem
                  843806
                  Ravi_Gupta wrote:
                  I had removed all the listeners as you told me but still i can change its value using keyboard (arrow keys)
                  <snip/>
                  Can you tell me what is the problem in my code?
                  Not at this moment! I have just tried removing all the listeners but, like you, the arrow keys still function. More research is required. It might be worth you looking at the source.

                  Of course, some of the more knowledgeable forum members might be able to suggest an alternative or a fix for this approach.
                  • 6. Re: JSlider Problem
                    800774
                    The slider is using key bindings for the keyboard, not a key listener.
                    You can use reflection to remove all the listeners:
                    JSlider slider = new JSlider(...);
                    Class sliderUIClass = BasicSliderUI.class;
                    Method uninstallListeners = sliderUIClass.getDeclaredMethod("uninstallListeners", new Class[] {JSlider.class});
                    Method uninstallKeyboardActions = sliderUIClass.getDeclaredMethod("uninstallKeyboardActions", new Class[] {JSlider.class});
                    uninstallListeners.setAccessible(true);
                    uninstallKeyboardActions.setAccessible(true);
                    uninstallListeners.invoke(slider.getUI(), slider);
                    uninstallKeyboardActions.invoke(slider.getUI(), slider);
                    • 7. Re: JSlider Problem
                      800378
                      @ sabre150 and Rodney_McKay

                      Thanks for helping me.

                      If we disable the focusable property of JSlider then the approach given by sabre150 also works.
                      • 8. Re: JSlider Problem
                        843806
                        Ravi_Gupta wrote:
                        @ sabre150 and Rodney_McKay

                        Thanks for helping me.

                        If we disable the focusable property of JSlider then the approach given by sabre150 also works.
                        I like this. You can now simplify the code because by not allowing focus you don't need to remove the keyboard listeners since without getting focus the JSlider can't receive any keyboard events.