Swing - Customizing JSlider

I am trying to use a component that has a bar and pointer (looks like JSlider) but that is not editable (using Component.setEnabled(false) on JSlider has undesirable effects such as the object greying out).

Is anyone aware of such a component? And if not, what is the procedure for customizing a JSlider so that it behaves this way.

Thanks.

[359 byte] By [javaBeeNewba] at [2007-11-26 23:17:29]
# 1
How do you edit a JSlider?
Michael_Dunna at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...
# 2
I am trying to not allow the user to change the value of the slider (sliding the knob back and forth). Instead, the value will be changed in response to other methods.This is what I mean.
javaBeeNewba at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
you should be able to get into the slider's UI and remove the listeners from the knob and track (I'm not at a java pc at present, so can't test)
Michael_Dunna at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

I have since moved on to other things in my program, but now I am back trying to figure out how to customize a JSlider. I have tried something simple, just trying to mimick JSlider.

import java.io.*;

import javax.swing.*;

import javax.swing.JSlider;

import java.util.*;

public class BreakEvenSlider extends JSlider

{

int value;

int maximum;

int minimum;

public BreakEvenSlider(int min, int max, int val)

{

minimum = min;

maximum = max;

value = val;

setValue(val);

setMaximum(maximum);

setMinimum(minimum);

}

public int getValue()

{

//System.out.println(value);

return value();

}

}

It seems though, that by setting it up:

private BreakEvenSlider breakEven = new BreakEvenSlider(-100,100,13);

when I try breakEven.getValue() it is returning 50, and the min and max is set to 0 and 100, respectively. This is what the default constructor does.

I am sure there is something fundamental that I just do not understand. Can someone help me out?

Also, how does one "get into the slider's UI and remove the listeners from the knob and track"? I have tried to find tutorials or code on the subject and I have come up empty.

Thanks

Jeremy

javaBeeNewba at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...
# 5

BreakEvenSlider works OK for me, 1.5.0_05.

displays -100 to 100, knob at 13, prints getValue() as 13

for the 'no user movement' bit, see if this does what you want

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.plaf.basic.BasicSliderUI;

class Testing

{

public void buildGUI()

{

JSlider slider = new JSlider(0,100,50);

slider.setMajorTickSpacing(10);

slider.setMinorTickSpacing(5);

slider.setPaintTicks(true);

slider.setPaintLabels(true);

slider.setUI(new NoListenerUI(slider));

JFrame f = new JFrame();

f.getContentPane().add(slider);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

class NoListenerUI extends BasicSliderUI

{

public NoListenerUI(JSlider js){super(js);}

protected void installListeners(JSlider slider){}

protected BasicSliderUI.ScrollListener createScrollListener(JSlider slider)

{

class MyScrollListener extends BasicSliderUI.ScrollListener{}

return new MyScrollListener();

}

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...
# 6

This seems to do the trick. However, there are some other things I want to implement with this JSlider.

For instance, I want to paint from the minimum value to the pointer one color and from the pointer to the rest of the slider a seperate color.

Is there a tutorial I can work through that will show me how to use the ComponentUI class?

I mean, I'd think adding this to the code you gave me would at least paint the slider black:

//JSlider j is initialized in constructor

public void paintTrack(Graphics g)

{

//Graphics2D g2d = (Graphics2D)g;

g.setColor(Color.BLACK);

g.draw3DRect(j.getX(),j.getY(),j.getWidth(),j.getHeight(),true);

g.fill3DRect(j.getX(),j.getY(),j.getWidth(),j.getHeight(),true);

}

I am clearly wrong.

Thanks

javaBeeNewba at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...
# 7
the 'TripleSlider' here might give you some ideas regarding the color problem http://forum.java.sun.com/thread.jspa?forumID=57&threadID=622941
Michael_Dunna at 2007-7-10 14:18:51 > top of Java-index,Desktop,Core GUI APIs...