Forum Stats

  • 3,750,838 Users
  • 2,250,259 Discussions


Problem in changing forground color of JSlider!!

843805 Member Posts: 49,999
edited Nov 7, 2006 5:08PM in Swing
Hello frnds,

I am adding JSlider to my program.. when I want to change background and foreground color of JSlider, background color changes to black correctly..

but foreground color.. (I want to have numbers with JSlider in white).. does not change to white.. !!
JSlider framesPerSecond  = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT);
Is default numbers of JSlider part of Background??
If yes.. is there any way to change color of numbers.. ??



  • 843805
    843805 Member Posts: 49,999
    can anyone help with this??
  • camickr
    camickr Member Posts: 24,931
    is there any way to change color of numbers.. ??
    You would need to write your own UI.
  • 843805
    843805 Member Posts: 49,999
    i dont hav much idea.. abt that.. !!
    can u tell me how??

    smething like that.. as I do it in Jspinner ??

  • 843805
    843805 Member Posts: 49,999
    ((javax.swing.JSpinner.NumberEditor)jSpinner1.getEditor()).getTextField().setBackground(new Color(150,150,255));
  • camickr
    camickr Member Posts: 24,931
    Well, MetalSliderUI extends BasicSliderUI. The paintLabels() code is found in the BasicSliderUI. So you would need to extend MetalSliderUI and override the paintLabels() method to use the foreground color of the slider. Probably not very easy, which I why I didn't post any code. Any the source code for all these classes is found in the root directory of the JDK (, I beliver). So look at the code and decide if you want to override the method yourself, or just use the default UI.
  • 843805
    843805 Member Posts: 49,999
    its better to have default UI then.. ;)
  • 843805
    843805 Member Posts: 49,999
    One thing more camick.. i needed ur help in scaling JPanel.. to double size..

    what i want .. I have JPanel.. in which I am moving small sprites.. with mouse movement.. I want to show another duplicate screen.. of that JPanel.. with double size.. so double the size of each image.. which is not a problem.. !!

    but what i want is to Move sprites in another JPanel.. in the same way as I am moving small sprites in original one..
    I will put another post for this question..

    I know its possible.. but if u can guide me hw??
    Thx in advance

  • 794342
    794342 Member Posts: 6,422
    is this what you're trying to do?
    import java.awt.*;
    import javax.swing.*;
    class Testing extends JFrame
      public Testing()
        JSlider slider = new JSlider(0,100,0);
        slider.setUI(new MyUI());
        JPanel jp = new JPanel();
      class MyUI extends javax.swing.plaf.metal.MetalSliderUI
        protected void paintHorizontalLabel(Graphics g,int value,Component label)
          JLabel lbl = (JLabel)label;
      public static void main(String[] args){new Testing().setVisible(true);}
  • 843805
    843805 Member Posts: 49,999
    is this what you're trying to do?
    Too gud dude.. u rocks..!!
    Thx a million..
    there are some guys like camickr.. and U.. which really have brilliant mind.. well hw much time it takes to come at ur professional level??.. :)

    Can u pls look into my dis problm.. too.. sorry am just disturbing element.. !!.. doing project in different country.. no friends.. here.. so not getting much help..
  • 843805
    843805 Member Posts: 49,999
    I have an alternate solution that doesn't involve overriding the MetalSliderUI class, if anyone is still interested.

    You can change the colour of the slider labels if you create a custom label table and assign it to the slider. When the slider paints the labels, it uses their individual properties.

          // Initialize the slider, setting its starting value
          JSlider lSlider = new JSlider( JSlider.HORIZONTAL, 1, 3, 1 );
          // Set the slider's background colour
          lSlider .setBackground( Color.BLACK );
          // Set the slider tick spacing
          lSlider .setMajorTickSpacing( 1 );
          // Paint the value labels and ticks on the slider
          lSlider .setPaintLabels( true );
          lUsefulnessSlider.setPaintTicks( true );
          // Snap the input value to the ticks, so that the user can't select a fractional value
          lSlider .setSnapToTicks( true );
          // Initialize the label table
          Hashtable lLabelTable = new Hashtable();
          // Create three labels, and assign them custom foreground colours
          JLabel lLabel1 = new JLabel( "1" );
          lLabel1.setForeground( Color.RED );
          JLabel lLabel2 = new JLabel( "2" );
          lLabel1.setForeground( Color.GREEN );
          JLabel lLabel3 = new JLabel( "3" );
          lLabel1.setForeground( Color.BLUE );
          // Put the custom labels into the table
          lLabelTable.put( new Integer( 1 ), lLabel1 );
          lLabelTable.put( new Integer( 2 ), lLabel2 );
          lLabelTable.put( new Integer( 3 ), lLabel3 );
          // Assign the label table to the slider
          lSlider.setLabelTable( lLabelTable );
    Now, when you display the slider, the three labels should have the colours Red, Green, and Blue, respectively, with a black background.
This discussion has been closed.