Forum Stats

  • 3,750,838 Users
  • 2,250,259 Discussions
  • 7,867,174 Comments

Discussions

Problem in changing forground color of JSlider!!

843805
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);
framesPerSecond.setBackground(Color.black);
framesPerSecond.setForeground(Color.white);
Is default numbers of JSlider part of Background??
If yes.. is there any way to change color of numbers.. ??

gervini
«1

Comments

  • 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 ??

    ((javax.swing.JSpinner.NumberEditor)jSpinner1.getEditor()).getTextField().setOpaque(false);
  • 843805
    843805 Member Posts: 49,999
    ((javax.swing.JSpinner.NumberEditor)jSpinner1.getEdito
    r()).getTextField().setOpaque(false);
    ((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 (src.zip, 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

    gervini
  • 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()
      {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(200,100);
        JSlider slider = new JSlider(0,100,0);
        slider.setUI(new MyUI());
        slider.setBackground(Color.black);
        slider.setForeground(Color.white);
        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        JPanel jp = new JPanel();
        jp.add(slider);
        getContentPane().add(slider);
        pack();
      }
      class MyUI extends javax.swing.plaf.metal.MetalSliderUI
      {
        protected void paintHorizontalLabel(Graphics g,int value,Component label)
        {
          JLabel lbl = (JLabel)label;
          lbl.setForeground(Color.WHITE);
          super.paintHorizontalLabel(g,value,lbl);
        }
      }
      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..
    http://forum.java.sun.com/thread.jspa?threadID=702244&messageID=4072843#4072843
  • 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.

    Example:
          // 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.