Color changing using Scrollbar in java

Amol picture Amol · Mar 28, 2013 · Viewed 9.8k times · Source

i want to change the background color of frame as per the value selected in 3 scrollbar? But it is not happening here is my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class changeColor extends JFrame implements AdjustmentListener
{
    JScrollBar red;
    JScrollBar green;
    JScrollBar blue;


    changeColor()
    {
        super("SCROLLBAR DEMO");
        setLayout(new FlowLayout());
        setVisible(true);
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        red=new JScrollBar(JScrollBar.HORIZONTAL);
        green=new JScrollBar(JScrollBar.HORIZONTAL);
        blue=new JScrollBar(JScrollBar.HORIZONTAL);
        add(red);
        add(green);
        add(blue);
        red.addAdjustmentListener(this);
        green.addAdjustmentListener(this);
        blue.addAdjustmentListener(this);   
    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        int cr=0;
        int cg=0;
        int cb=0;
        if(ae.getSource()==red)
            cr=ae.getValue();
        else if(ae.getSource()==green)
            cg=ae.getValue();
        else if(ae.getSource()==blue)
            cb=ae.getValue();

        setBackground(new Color(cr,cg,cb)); 
    }


    public static void main(String args[])
    {
        changeColor obj=new changeColor();  
    }
}

The problem is that the background color is not changing. I want to know what is the problem and how can i fix it?

Answer

AVI picture AVI · Oct 3, 2015

This is a good solution. First you are creating JFrame with it's normal methods such as setDefaultCloseOperation(), setBounds(), getContentPane(). Then create an object from your class then use that to call all the other methods through out the program, in this case I created object called app. One thing you have to keep in mind is that don't forget to use AdjustmentEvent e instead of ActionListener e :).

Also all the color changes must go with this panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(), sbar3.getValue()in AdjustmentEvent, because once the Scrollbar gets changed, it's value gets by the getValue() method and added to the new Color() method with in the setBackground() method.

    import javax.swing.*;  
    import java.awt.event.*;
    import java.awt.*;

    public class Main implements AdjustmentListener {

        private static void createAndShowGUI() {
            // make frame..
          JFrame frame = new JFrame("JScrollBar");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setBounds(20,30,200,250);
          frame.getContentPane().setLayout(null);
          Main app = new Main();
          app.sbar1 = new JScrollBar(java.awt.Adjustable.VERTICAL, 127, 1,0,255);
          app.sbar1.setBounds(10,20, 10, 200);
          app.sbar1.setBackground(Color.red);
          app.sbar1.addAdjustmentListener(app);
          frame.getContentPane().add(app.sbar1);
          app.sbar2 = new JScrollBar(java.awt.Adjustable.VERTICAL, 127, 1,0,255);
          app.sbar2.setBounds(30,20, 10, 200);
          app.sbar2.setBackground(Color.green);
          app.sbar2.addAdjustmentListener(app);
          frame.getContentPane().add(app.sbar2);
          app.sbar3 = new JScrollBar(java.awt.Adjustable.VERTICAL, 127, 1,0,255);
          app.sbar3.setBounds(50,20, 10, 200);
          app.sbar3.setBackground(Color.blue);
          app.sbar3.addAdjustmentListener(app);
          frame.getContentPane().add(app.sbar3);

          app.panel = new JPanel();
          app.panel.setBounds(80,20,50,200);
          app.panel.setBackground(new Color(0,0,0));
          frame.getContentPane().add(app.panel);

          frame.setVisible(true); 
      }

      public void adjustmentValueChanged(AdjustmentEvent e)
      {
          panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(), sbar3.getValue()));
      }

      public static void main(String[] args) {
        // start off..
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
      }

      // application object fields    
      JScrollBar sbar1;
      JScrollBar sbar2;
      JScrollBar sbar3;
      JPanel panel;
}

I hope this helped you alot.!!!