Link to home
Start Free TrialLog in
Avatar of Dawkins
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)?
Avatar of Dawkins
Dawkins

ASKER

No ideas?
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.setMajorTickSpacing (10);
    //slider.setPaintTicks(true);
    // slider.setPaintLabels(true);
    ChangeListener changeListener =
      new ChangeListener() {
      public void stateChanged(
                               ChangeEvent changeEvent) {
        JSlider theSlider = (
                             JSlider)changeEvent.getSource();
        if (!theSlider.getValueIsAdjusting()) {
          optionPane.setInputValue(new Integer(  
                                               theSlider.getValue()));
        }
      }
    };
    slider.addChangeListener(changeListener);
    return slider;
  }

I don't see any real problem here. But might share some other details about the
problem you are having.

Delphi3
Avatar of Dawkins

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(slider);
    setSize(400, 200);
    show();
  }
 
  public static void main(String args[]) {
    try {
      UIManager.setLookAndFeel(
          "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    }
    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(JFrame.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.addChangeListener(this);
    redSlider.setLabelTable(labelTable);
    redSlider.setPaintLabels(true);
    redSlider.setMajorTickSpacing(64);
    redSlider.setMinorTickSpacing(16);
    redSlider.setPaintTicks(true);
    CP.add(redSlider,c);
   
    ++c.gridx;
    greenSlider = new JSlider(JSlider.VERTICAL,0,255,0);
    greenSlider.addChangeListener(this);
    greenSlider.setLabelTable(labelTable);
    greenSlider.setPaintLabels(true);
    greenSlider.setMajorTickSpacing(64);
    greenSlider.setMinorTickSpacing(16);
    greenSlider.setPaintTicks(true);
    CP.add(greenSlider,c);
   
    ++c.gridx;
    blueSlider = new JSlider(JSlider.VERTICAL,0,255,0);
    blueSlider.addChangeListener(this);
    blueSlider.setLabelTable(labelTable);
    blueSlider.setPaintLabels(true);
    blueSlider.setMajorTickSpacing(64);
    blueSlider.setMinorTickSpacing(16);
    blueSlider.setPaintTicks(true);
    CP.add(blueSlider,c);
   
    c.gridx = 0;  ++c.gridy;
    redField = new JTextField("0",3);
    redField.addActionListener(this);
    redField.setForeground(Color.RED);
    CP.add(redField,c);
    ++c.gridx;
    greenField = new JTextField("0",3);
    greenField.addActionListener(this);
    greenField.setForeground(new Color(0,225,0));
    CP.add(greenField,c);
    ++c.gridx;
    blueField = new JTextField("0",3);
    blueField.addActionListener(this);
    blueField.setForeground(Color.BLUE);
    CP.add(blueField,c);
   
    window = new JWindow();
    panel = new jpanel();
    window.getContentPane().add(panel);
    window.pack();
    window.setVisible(true);
   
    pack();
    setVisible(true);
  }
 
  public void actionPerformed(ActionEvent ae) {
    try {
      int red = Integer.parseInt(redField.getText());
      int green = Integer.parseInt(greenField.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.toString(redSlider.getValue()));
      greenField.setText(Integer.toString(greenSlider.getValue()));
      blueField.setText(Integer.toString(blueSlider.getValue()));
    } catch (IllegalArgumentException iae) {
      redField.setText(Integer.toString(redSlider.getValue()));
      greenField.setText(Integer.toString(greenSlider.getValue()));
      blueField.setText(Integer.toString(blueSlider.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.toString(red));
    greenField.setText(Integer.toString(green));
    blueField.setText(Integer.toString(blue));
    panel.repaint();
  }
 
  class jpanel extends JPanel {
    public Dimension getPreferredSize() {
      return Toolkit.getDefaultToolkit().getScreenSize();
    }
   
    public void paintComponent(Graphics g) {
      g.setColor(color);
      g.fillRect(0,0,jpanel.this.getWidth(),jpanel.this.getHeight());
    }
  }
 
  public static void main(String[] args) {
    new ColorTest();
  }
}

Still have the same problem when the sliders ar vertical?

D3
Avatar of Dawkins

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.windows.WindowsLookAndFeel");
    }
    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>DisplayProperties>Effects 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

Avatar of Dawkins

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?
ASKER CERTIFIED SOLUTION
Avatar of delphi3
delphi3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Dawkins

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!
Good luck with your project. Hope that eventually you find out what is happening.  

Thanks for the points and grade.

D3