Dawkins
asked on
Simple JSlider display issue
When creating a slider with the following attributes and windows look and feel, there is a display problem where the "tip" of the slider gets left behind when it is moved, creating a fairly nasty looking effect.
JSlider slider = new JSlider(1, 10, 5);
slider.setMinorTickSpacing (1);
slider.setMajorTickSpacing (5);
slider.setPaintTicks(true) ;
slider.setSnapToTicks(true );
slider.setPaintLabels(true );
Can anyone suggest a way to avoid this problem (keeping window L&F and ticks painted)?
JSlider slider = new JSlider(1, 10, 5);
slider.setMinorTickSpacing
slider.setMajorTickSpacing
slider.setPaintTicks(true)
slider.setSnapToTicks(true
slider.setPaintLabels(true
Can anyone suggest a way to avoid this problem (keeping window L&F and ticks painted)?
In what kind of component did you put the slider?
<*>
<*>
Hi Dawkins,
I went out looking for a working sample to put your sample into for a test:
Found:
http://java.sun.com/developer/JDCTechTips/2004/tt0122.html
and I replaced your sample into the sample code so it now reads:
public static JSlider getSlider(
final JOptionPane optionPane) {
JSlider slider = new JSlider(1, 10, 5);
slider.setMinorTickSpacing (1);
slider.setMajorTickSpacing (5);
slider.setPaintTicks(true) ;
slider.setSnapToTicks(true );
slider.setPaintLabels(true );
// JSlider slider = new JSlider();
//slider.setMajorTickSpaci ng (10);
//slider.setPaintTicks(tru e);
// slider.setPaintLabels(true );
ChangeListener changeListener =
new ChangeListener() {
public void stateChanged(
ChangeEvent changeEvent) {
JSlider theSlider = (
JSlider)changeEvent.getSou rce();
if (!theSlider.getValueIsAdju sting()) {
optionPane.setInputValue(n ew Integer(
theSlider.getValue()));
}
}
};
slider.addChangeListener(c hangeListe ner);
return slider;
}
I don't see any real problem here. But might share some other details about the
problem you are having.
Delphi3
I went out looking for a working sample to put your sample into for a test:
Found:
http://java.sun.com/developer/JDCTechTips/2004/tt0122.html
and I replaced your sample into the sample code so it now reads:
public static JSlider getSlider(
final JOptionPane optionPane) {
JSlider slider = new JSlider(1, 10, 5);
slider.setMinorTickSpacing
slider.setMajorTickSpacing
slider.setPaintTicks(true)
slider.setSnapToTicks(true
slider.setPaintLabels(true
// JSlider slider = new JSlider();
//slider.setMajorTickSpaci
//slider.setPaintTicks(tru
// slider.setPaintLabels(true
ChangeListener changeListener =
new ChangeListener() {
public void stateChanged(
ChangeEvent changeEvent) {
JSlider theSlider = (
JSlider)changeEvent.getSou
if (!theSlider.getValueIsAdju
optionPane.setInputValue(n
theSlider.getValue()));
}
}
};
slider.addChangeListener(c
return slider;
}
I don't see any real problem here. But might share some other details about the
problem you are having.
Delphi3
ASKER
I've written an executable demo to show the problem. Dragging the slider from left to right leaves the tip of it behind. Dragging it from right to left causes a "ghosting" effect. I'm using Windows XP.
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.JFrame;
public class Demo extends JFrame {
public Demo() {
JSlider slider = new JSlider(0, 10, 5);
slider.setMinorTickSpacing (1);
slider.setMajorTickSpacing (5);
slider.setPaintTicks(true) ;
slider.setSnapToTicks(true );
slider.setPaintLabels(true );
getContentPane().add(slide r);
setSize(400, 200);
show();
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.w indows.Win dowsLookAn dFeel");
}
catch (Exception e) {}
new Demo();
}
}
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.JFrame;
public class Demo extends JFrame {
public Demo() {
JSlider slider = new JSlider(0, 10, 5);
slider.setMinorTickSpacing
slider.setMajorTickSpacing
slider.setPaintTicks(true)
slider.setSnapToTicks(true
slider.setPaintLabels(true
getContentPane().add(slide
setSize(400, 200);
show();
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.w
}
catch (Exception e) {}
new Demo();
}
}
Hi Dawkins,
What is it like when the sliders are going vertical?
What are you using for java IDE? Your computer is a ?
There may be a refresh rate problem. Are you running many programs at the same time that would slow the system down?
Here's a demo the you might consider.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class ColorTest extends JFrame implements ActionListener,
ChangeListener {
JWindow window;
jpanel panel;
JSlider redSlider, greenSlider, blueSlider;
JTextField redField, greenField, blueField;
Color color = Color.BLACK;
public ColorTest() {
super("ColorTest");
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
Container CP = getContentPane();
CP.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = c.gridy = 0; c.insets = new Insets(5,10,10,5);
JLabel l = new JLabel("Red");
l.setForeground(Color.RED) ;
CP.add(l,c);
++c.gridx;
l = new JLabel("Green");
l.setForeground(new Color(0,225,0));
CP.add(l,c);
++c.gridx;
l = new JLabel("Blue");
l.setForeground(Color.BLUE );
CP.add(l,c);
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(0), new JLabel("0"));
labelTable.put(new Integer(64), new JLabel("64"));
labelTable.put(new Integer(128), new JLabel("128"));
labelTable.put(new Integer(192), new JLabel("192"));
labelTable.put(new Integer(255), new JLabel("255"));
c.gridx = 0; ++c.gridy;
redSlider = new JSlider(JSlider.VERTICAL,0 ,255,0);
redSlider.addChangeListene r(this);
redSlider.setLabelTable(la belTable);
redSlider.setPaintLabels(t rue);
redSlider.setMajorTickSpac ing(64);
redSlider.setMinorTickSpac ing(16);
redSlider.setPaintTicks(tr ue);
CP.add(redSlider,c);
++c.gridx;
greenSlider = new JSlider(JSlider.VERTICAL,0 ,255,0);
greenSlider.addChangeListe ner(this);
greenSlider.setLabelTable( labelTable );
greenSlider.setPaintLabels (true);
greenSlider.setMajorTickSp acing(64);
greenSlider.setMinorTickSp acing(16);
greenSlider.setPaintTicks( true);
CP.add(greenSlider,c);
++c.gridx;
blueSlider = new JSlider(JSlider.VERTICAL,0 ,255,0);
blueSlider.addChangeListen er(this);
blueSlider.setLabelTable(l abelTable) ;
blueSlider.setPaintLabels( true);
blueSlider.setMajorTickSpa cing(64);
blueSlider.setMinorTickSpa cing(16);
blueSlider.setPaintTicks(t rue);
CP.add(blueSlider,c);
c.gridx = 0; ++c.gridy;
redField = new JTextField("0",3);
redField.addActionListener (this);
redField.setForeground(Col or.RED);
CP.add(redField,c);
++c.gridx;
greenField = new JTextField("0",3);
greenField.addActionListen er(this);
greenField.setForeground(n ew Color(0,225,0));
CP.add(greenField,c);
++c.gridx;
blueField = new JTextField("0",3);
blueField.addActionListene r(this);
blueField.setForeground(Co lor.BLUE);
CP.add(blueField,c);
window = new JWindow();
panel = new jpanel();
window.getContentPane().ad d(panel);
window.pack();
window.setVisible(true);
pack();
setVisible(true);
}
public void actionPerformed(ActionEven t ae) {
try {
int red = Integer.parseInt(redField. getText()) ;
int green = Integer.parseInt(greenFiel d.getText( ));
int blue = Integer.parseInt(blueField .getText() );
color = new Color(red,green,blue);
redSlider.setValue(red);
greenSlider.setValue(green );
blueSlider.setValue(blue);
} catch (NumberFormatException nfe) {
panel.repaint();
redField.setText(Integer.t oString(re dSlider.ge tValue())) ;
greenField.setText(Integer .toString( greenSlide r.getValue ()));
blueField.setText(Integer. toString(b lueSlider. getValue() ));
} catch (IllegalArgumentException iae) {
redField.setText(Integer.t oString(re dSlider.ge tValue())) ;
greenField.setText(Integer .toString( greenSlide r.getValue ()));
blueField.setText(Integer. toString(b lueSlider. getValue() ));
}
}
public void stateChanged(ChangeEvent ce) {
int red = redSlider.getValue();
int green = greenSlider.getValue();
int blue = blueSlider.getValue();
color = new Color(red,green,blue);
redField.setText(Integer.t oString(re d));
greenField.setText(Integer .toString( green));
blueField.setText(Integer. toString(b lue));
panel.repaint();
}
class jpanel extends JPanel {
public Dimension getPreferredSize() {
return Toolkit.getDefaultToolkit( ).getScree nSize();
}
public void paintComponent(Graphics g) {
g.setColor(color);
g.fillRect(0,0,jpanel.this .getWidth( ),jpanel.t his.getHei ght());
}
}
public static void main(String[] args) {
new ColorTest();
}
}
Still have the same problem when the sliders ar vertical?
D3
What is it like when the sliders are going vertical?
What are you using for java IDE? Your computer is a ?
There may be a refresh rate problem. Are you running many programs at the same time that would slow the system down?
Here's a demo the you might consider.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class ColorTest extends JFrame implements ActionListener,
ChangeListener {
JWindow window;
jpanel panel;
JSlider redSlider, greenSlider, blueSlider;
JTextField redField, greenField, blueField;
Color color = Color.BLACK;
public ColorTest() {
super("ColorTest");
setDefaultCloseOperation(J
Container CP = getContentPane();
CP.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = c.gridy = 0; c.insets = new Insets(5,10,10,5);
JLabel l = new JLabel("Red");
l.setForeground(Color.RED)
CP.add(l,c);
++c.gridx;
l = new JLabel("Green");
l.setForeground(new Color(0,225,0));
CP.add(l,c);
++c.gridx;
l = new JLabel("Blue");
l.setForeground(Color.BLUE
CP.add(l,c);
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(0), new JLabel("0"));
labelTable.put(new Integer(64), new JLabel("64"));
labelTable.put(new Integer(128), new JLabel("128"));
labelTable.put(new Integer(192), new JLabel("192"));
labelTable.put(new Integer(255), new JLabel("255"));
c.gridx = 0; ++c.gridy;
redSlider = new JSlider(JSlider.VERTICAL,0
redSlider.addChangeListene
redSlider.setLabelTable(la
redSlider.setPaintLabels(t
redSlider.setMajorTickSpac
redSlider.setMinorTickSpac
redSlider.setPaintTicks(tr
CP.add(redSlider,c);
++c.gridx;
greenSlider = new JSlider(JSlider.VERTICAL,0
greenSlider.addChangeListe
greenSlider.setLabelTable(
greenSlider.setPaintLabels
greenSlider.setMajorTickSp
greenSlider.setMinorTickSp
greenSlider.setPaintTicks(
CP.add(greenSlider,c);
++c.gridx;
blueSlider = new JSlider(JSlider.VERTICAL,0
blueSlider.addChangeListen
blueSlider.setLabelTable(l
blueSlider.setPaintLabels(
blueSlider.setMajorTickSpa
blueSlider.setMinorTickSpa
blueSlider.setPaintTicks(t
CP.add(blueSlider,c);
c.gridx = 0; ++c.gridy;
redField = new JTextField("0",3);
redField.addActionListener
redField.setForeground(Col
CP.add(redField,c);
++c.gridx;
greenField = new JTextField("0",3);
greenField.addActionListen
greenField.setForeground(n
CP.add(greenField,c);
++c.gridx;
blueField = new JTextField("0",3);
blueField.addActionListene
blueField.setForeground(Co
CP.add(blueField,c);
window = new JWindow();
panel = new jpanel();
window.getContentPane().ad
window.pack();
window.setVisible(true);
pack();
setVisible(true);
}
public void actionPerformed(ActionEven
try {
int red = Integer.parseInt(redField.
int green = Integer.parseInt(greenFiel
int blue = Integer.parseInt(blueField
color = new Color(red,green,blue);
redSlider.setValue(red);
greenSlider.setValue(green
blueSlider.setValue(blue);
} catch (NumberFormatException nfe) {
panel.repaint();
redField.setText(Integer.t
greenField.setText(Integer
blueField.setText(Integer.
} catch (IllegalArgumentException iae) {
redField.setText(Integer.t
greenField.setText(Integer
blueField.setText(Integer.
}
}
public void stateChanged(ChangeEvent ce) {
int red = redSlider.getValue();
int green = greenSlider.getValue();
int blue = blueSlider.getValue();
color = new Color(red,green,blue);
redField.setText(Integer.t
greenField.setText(Integer
blueField.setText(Integer.
panel.repaint();
}
class jpanel extends JPanel {
public Dimension getPreferredSize() {
return Toolkit.getDefaultToolkit(
}
public void paintComponent(Graphics g) {
g.setColor(color);
g.fillRect(0,0,jpanel.this
}
}
public static void main(String[] args) {
new ColorTest();
}
}
Still have the same problem when the sliders ar vertical?
D3
ASKER
Yep when I make the example you just posted windows LAF I get the same problem. Have you tried it with windows LAF?
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.w indows.Win dowsLookAn dFeel");
}
catch (Exception e) {}
new ColorTest();
}
I'm running on a 800Mhz PC with windows XP - should be enough processor power there to handle it (I hope!).
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.w
}
catch (Exception e) {}
new ColorTest();
}
I'm running on a 800Mhz PC with windows XP - should be enough processor power there to handle it (I hope!).
Hi Dawkins,
Could it be a settings problem with XP? Is this a recent installation of XP?
I have Win2k with NT and a less than powerful machine to run the JAVA.
Recommended do first
arrow thru:
MenuBar at the bottom> Start>Help>"cursor">"blink rate" and see what help you have there....
It that is no help then consider:
I have a setting control item that allows me to set the tool tips following.
arrow thru:
MenuBar at the bottom> Start>Settings > ControlPanel>DisplayProper ties>Effec ts tab> visual effects and notice what changes are possible, make a change and see if you notice anything different after you press [apply]
Keep in mind what changes you made in these adjustments, if any, so that you can return to the same settings should that not work :(
If you notice any change then that is the problem?
Other than that I really have no further clues.
D3
Could it be a settings problem with XP? Is this a recent installation of XP?
I have Win2k with NT and a less than powerful machine to run the JAVA.
Recommended do first
arrow thru:
MenuBar at the bottom> Start>Help>"cursor">"blink
It that is no help then consider:
I have a setting control item that allows me to set the tool tips following.
arrow thru:
MenuBar at the bottom> Start>Settings > ControlPanel>DisplayProper
Keep in mind what changes you made in these adjustments, if any, so that you can return to the same settings should that not work :(
If you notice any change then that is the problem?
Other than that I really have no further clues.
D3
ASKER
Nope nothing seemed to change it. I have an LCD monitor so there is a chance that is the problem I guess. Never mind, the important thing is that it looks ok on other people's systems!
So just to make 100% certain: you have run that code *using the windows look and feel* and didn't see any problems?
So just to make 100% certain: you have run that code *using the windows look and feel* and didn't see any problems?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dawkins,
I did some converation with an XP guru:
You have some speed issues with only 800Mhz PC and XP
To (maybe??) remedy it: shut of your JAVA IDE, go to your blank desktop area. Right click on that blank desk top area. This should bring up a panel that lists active desktop and other choices. Down at the bottom of that list you should see [Properties] as a selection. High light it. Wait for it to appear. Then the tabbed choices to the right include Background, Screen Saver, Appearance....
Select the Appearance tab. And amidst the listing there there is "Windows and Buttons"
You should have 2 choices there: XP style and Window's Classic style. If you select "Windows Classic" rather than XP, this should remove a lot of the overhead that goes into making XP show as XP and should cut the overhead loaded system.
Click the [Apply] and click [OK].
Restart your computer. Run your JAVA IDE and note any changes. Now test you program again.
Hopefully a fix?
D3
I did some converation with an XP guru:
You have some speed issues with only 800Mhz PC and XP
To (maybe??) remedy it: shut of your JAVA IDE, go to your blank desktop area. Right click on that blank desk top area. This should bring up a panel that lists active desktop and other choices. Down at the bottom of that list you should see [Properties] as a selection. High light it. Wait for it to appear. Then the tabbed choices to the right include Background, Screen Saver, Appearance....
Select the Appearance tab. And amidst the listing there there is "Windows and Buttons"
You should have 2 choices there: XP style and Window's Classic style. If you select "Windows Classic" rather than XP, this should remove a lot of the overhead that goes into making XP show as XP and should cut the overhead loaded system.
Click the [Apply] and click [OK].
Restart your computer. Run your JAVA IDE and note any changes. Now test you program again.
Hopefully a fix?
D3
ASKER
I don't think it's a perfomance problem - this PC runs very well for office tasks and I don't see any problems other than this 1 specific problem with the JSlider. I already have it on windows classic - haven't managed to get used to the new win XP style desktop yet! :)
I tried a different monitor and that didn't fix it so it really is a mystery!!
I don't really have any more time spend trying to fix it because I've got to have it finished by monday. But since you can't see the problem then it is most likely just a problem at my end - which is fine because the only thing I care about is that it looks ok to the people who are going to be using this software. :)
Thanks for your time!
I tried a different monitor and that didn't fix it so it really is a mystery!!
I don't really have any more time spend trying to fix it because I've got to have it finished by monday. But since you can't see the problem then it is most likely just a problem at my end - which is fine because the only thing I care about is that it looks ok to the people who are going to be using this software. :)
Thanks for your time!
Good luck with your project. Hope that eventually you find out what is happening.
Thanks for the points and grade.
D3
Thanks for the points and grade.
D3
ASKER