1

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?

Amol
  • 27
  • 1
  • 2
  • 8
  • 1
    Why not use a [`JSlider`](http://docs.oracle.com/javase/tutorial/uiswing/components/slider.html) instead? – MadProgrammer Mar 28 '13 at 00:14
  • I want to do it using JScrollBar only.I want to know what is the reason why the background color is not changing? – Amol Mar 28 '13 at 00:16

2 Answers2

1

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.!!!

AVI
  • 5,516
  • 5
  • 29
  • 38
0

I ran your code as you posted it and it sort of works, the background color does change.

Issue 1: The cr, cg, and cb need to be class variables. In this way, the colors from your selections will mix together. The way it is written, only one color will change at a time.

Issue 2: To get the full range of color, you will need to change your selections so that the range goes from 0 to 255. With the JScrollBar approach, I am only seeing values from 0 to 90. That could easily be fixed with a move to JSlider

Issue 3: You must set the color on the content pane of a JFrame. (See comment)

Comment: Java convention is to name classes with an uppercase, FYI.

Here is the change which may give the result you are looking for:

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

class changeColor extends JFrame implements AdjustmentListener
{
    JScrollBar red;
    JScrollBar green;
    JScrollBar blue;
    int cr=0;
    int cg=0;
    int cb=0;


    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)
    {

        if(ae.getSource()==red)
            cr=ae.getValue();
        else if(ae.getSource()==green)
            cg=ae.getValue();
        else if(ae.getSource()==blue)
            cb=ae.getValue();
        System.out.println(cr + ":" + cg + ":" + cb);

        // add color to content pane
        this.getContentPane().setBackground(new Color(cr,cg,cb)); 
    }


    public static void main(String args[])
    {
        changeColor obj=new changeColor();  
    }
}
Beatty
  • 456
  • 3
  • 10
  • Color is still not changing.I want only one color at a time. – Amol Mar 28 '13 at 00:46
  • That is really strange because your code, as written worked for me. What system are you working on? Windows, Mac? – Beatty Mar 28 '13 at 00:47
  • Conventions i know about it first next time i will follow naming conventions – Amol Mar 28 '13 at 00:47
  • What do you see? Do you see the Frame? So you see the ScrollBars? Can you slide the Scrollbars? If you can slide them, what color is the background? White / Black, other? – Beatty Mar 28 '13 at 00:48
  • i see the frame.i also see the scrollbars.I am able to slide them.The color is grey i think default of frame.Everything is happening but the color is not changing? – Amol Mar 28 '13 at 00:51
  • My main system is a Macbook pro and it works there. I switched over to Win7 and it does not work. I will take a quick look. – Beatty Mar 28 '13 at 00:58
  • Here is the issue: this.getContentPane().setBackground(new Color(cr,cg,cb)); – Beatty Mar 28 '13 at 01:01
  • Color is still not changing. – Amol Mar 28 '13 at 01:06
  • i am also seeing values from 0 to 90.Color is changing but not as per the desire. – Amol Mar 28 '13 at 01:11
  • I updated the code example in my original reply. This code now works in my Win7 system. Try running that, exactly. Beyond that I am not sure what else to do. Your example works for me in Mac and Windows :) Good luck. – Beatty Mar 28 '13 at 01:13