I've been trying to modify the following code so that it only displays one scrollbar for 3 radio buttons but so far I just can get it to work right. After the modification the gui should have 3 radio buttons at the top, one scrollbar for all 3 buttons, and labels underneath the scrollbar. After selecting which color( with radiobuttons), as the scrollbar is moved the background should fill in with the choosen color. I will keep trying to do it but if anyone could help me out with this code I'll appreciate it. My programming isn't that great.

The hint that I was given was this:
Put the buttons into a panel. Call setBackground separately for each button, the panel containing the buttons, and the label underneath the scrollbar. The window should correctly close using the X.

import java.awt.*;
import java.awt.event.*;

//Frame class
class PickColor extends Frame {
	private Label redLabel = 
		new Label("Red = 128", Label.CENTER);
	private Label greenLabel = 
		new Label("Green = 128", Label.CENTER);
	private Label blueLabel = 
		new Label("Blue = 128", Label.CENTER);
	private Scrollbar redBar = 
		new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
	private Scrollbar greenBar = 
		new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
	private Scrollbar blueBar = 
		new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);

	// Constructor
	public PickColor(String title) {
		// Set title, background color, and layout
		super(title);
		setBackground(new Color(128, 128, 128));
		setLayout(new GridLayout(6, 1));
		// Create scrollbar listener
		ScrollbarListener listener = new ScrollbarListener();

		// Add red scrollbar and label to frame; attach
		// listener to scrollbar
		add(redBar);
		redBar.addAdjustmentListener(listener);
		add(redLabel);

		// Add green scrollbar and label to frame; attach
		// listener to scrollbar
		add(greenBar);
		greenBar.addAdjustmentListener(listener);
		add(greenLabel);

		// Add blue scrollbar and label to frame; attach
		// listener to scrollbar
		add(blueBar);
		blueBar.addAdjustmentListener(listener);
		add(blueLabel);

		// Attach window listener
		addWindowListener(new WindowCloser());
	}
	// Driver class	
	public static void main(String[] args) {
		Frame f = new PickColor("Pick Color");
		f.setSize(150, 200);
		f.setVisible(true);
	}
	//Listener for all scrollbars
	class ScrollbarListener implements AdjustmentListener {
		public void adjustmentValueChanged(AdjustmentEvent evt) {
			int red = redBar.getValue();
			int green = greenBar.getValue();
			int blue = blueBar.getValue();

			redLabel.setText("Red = " + red);
			greenLabel.setText("Green = " + green);
			blueLabel.setText("Blue = " + blue);

			Color newColor = new Color(red, green, blue);
			redLabel.setBackground(newColor);
			greenLabel.setBackground(newColor);
			blueLabel.setBackground(newColor);
		}
	}
}

Recommended Answers

All 8 Replies

First suggestion would be to change to using Swing components vs AWT.
Change the Frame to JFrame.

Instead of trying to change this program, I'd suggest you start with a new program and build the GUI from the beginning as per your instructions. When you get the GUI laid out, you can come back to this program and get the logic you need for the color changing.

I'm trying to tied everything to the scroll bar. The radio buttons and colors so that when I choose a radio button and move the scroll bar the background starts to fill up with the appropriate color. Any suggestions?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

//Frame class
class PickColor extends JFrame {
 private JLabel redLabel = 
  new JLabel("Red = 128");
 private JLabel greenLabel = 
  new JLabel("Green = 128");
 private JLabel blueLabel = 
  new JLabel("Blue = 128");
 private Scrollbar colorBar = 
  new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
// private Scrollbar greenBar = 
 // new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
 //private Scrollbar blueBar = 
 // new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
 
 private JRadioButton reds = new JRadioButton("Red", true);
 private JRadioButton greens = new JRadioButton("Green", false);
 private JRadioButton blues = new JRadioButton("Blue", false);
 

 // Constructor
 public PickColor(String title) {
  // Set title, background color, and layout
  super(title);
  setBackground(new Color(128, 128, 128));
  setLayout(new GridLayout(3, 1));
  // Create scrollbar listener
  ScrollbarListener listener = new ScrollbarListener();
  
  // Guarantees that only one button is selected at a time.
  ButtonGroup group = new ButtonGroup();
  group.add(reds);
  group.add(greens);
  group.add(blues);
  
  // Adds the buttons next to each other in a flowLayout
  JPanel panel = new JPanel(new FlowLayout());
  panel.setBackground(Color.WHITE);
  panel.add(reds);
  panel.add(greens);
  panel.add(blues);
  
  // Adds the panel to the north
  add("North", panel);

  // Add colorBar scroll bar to frame; attach
  // listener to scroll bar
  add("Center", colorBar);
  colorBar.addAdjustmentListener(listener);
  
  // Creates a panel for the labels. Puts them
  // right next to each other.
  JPanel panel2 = new JPanel(new FlowLayout());
  panel2.setBackground(Color.WHITE);
  panel2.add(redLabel);
  panel2.add(greenLabel);
  panel2.add(blueLabel);
  
  // adds panel2 to the south of the frame.
  add("South", panel2);

  // Attach window listener
 // addWindowListener(new WindowCloser());
 }
 // Driver class 
 public static void main(String[] args) {
  JFrame f = new PickColor("Pick Color");
  f.setSize(150, 200);
  f.setVisible(true);
 }
 //Listener for scroll bar
 class ScrollbarListener implements AdjustmentListener {
  public void adjustmentValueChanged(AdjustmentEvent evt) {
   int red = colorBar.getValue();
   int green = colorBar.getValue();
   int blue = colorBar.getValue();

   redLabel.setText("Red = " + red);
   greenLabel.setText("Green = " + green);
   blueLabel.setText("Blue = " + blue);
   
   Color newColors = new Color(red, green, blue);
   reds.setBackground(newColors);
   greens.setBackground(newColors);
   blues.setBackground(newColors);
   

   //Color newColor = new Color(red, green, blue);
  // redLabel.setBackground(newColor);
  // greenLabel.setBackground(newColor);
  // blueLabel.setBackground(newColor);
   
  }
 }
}

You need to know which RadioButton is selected and only change the color value that it indicates.

Member Avatar for hfx642

A couple of things wrong with this;
a) Your not keeping track of your colours OUTSIDE of ScrollbarListener
b) You need a listener for your JRadioButtons to reset the values of your Scrollbar

Everyone, I appreciate the help. I worked on this today (since it was the only day I had time for this) and haven't been able to get it to work (my fault). Looking by the code below is obvious that is wrong. I tried to work on this by doing (when I tried different ways of doing it, all were wrong) all the code myself because I really want to get this and be at least a little bit better at java coding. Since this program is due is the next few days, can someone help me with the code? I have to work on another program that I also have to turn in with this one and need the remaining time to finish it. If no one can help me with the code I'll just turn in what I have.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

//Frame class
class PickColor extends JFrame implements ActionListener {
private JLabel redLabel = 
  new JLabel("Red = 128");
 private JLabel greenLabel = 
  new JLabel("Green = 128");
 private JLabel blueLabel = 
  new JLabel("Blue = 128");
 private Scrollbar colorBar = 
  new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
// private Scrollbar greenBar = 
 // new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
 //private Scrollbar blueBar = 
 // new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
 
 private JRadioButton reds = new JRadioButton("Red", false);
 private JRadioButton greens = new JRadioButton("Green", false);
 private JRadioButton blues = new JRadioButton("Blue", false);
 

 // Constructor
 public PickColor(String title) {
  // Set title, background color, and layout
  super(title);
  setBackground(new Color(128, 128, 128));
  setLayout(new GridLayout(3, 1));
  // Create scrollbar listener
  ScrollbarListener listener = new ScrollbarListener();
  
  reds.addActionListener(this);
  greens.addActionListener(this);
  blues.addActionListener(this);
  
  // Guarantees that only one button is selected at a time.
  ButtonGroup group = new ButtonGroup();
  group.add(reds);
  group.add(greens);
  group.add(blues);
  
  // Adds the buttons next to each other in a flowLayout
  JPanel panel = new JPanel(new FlowLayout());
  panel.setBackground(Color.WHITE);
  panel.add(reds);
  panel.add(greens);
  panel.add(blues);
  
  // Adds the panel to the north
  add("North", panel);

  // Add colorBar scroll bar to frame; attach
  // listener to scroll bar
  add("Center", colorBar);
  colorBar.addAdjustmentListener(listener);
  
  // Creates a panel for the labels. Puts them
  // right next to each other.
  JPanel panel2 = new JPanel(new FlowLayout());
  panel2.setBackground(Color.WHITE);
  panel2.add(redLabel);
  panel2.add(greenLabel);
  panel2.add(blueLabel);
  
  // adds panel2 to the south of the frame.
  add("South", panel2);

  // Attach window listener
 // addWindowListener(new WindowCloser());
 }
 public void actionPerformed(ActionEvent e){
	 JRadioButton cb = (JRadioButton) e.getSource();
	 
	 if(reds.isSelected()){
		 cb.setBackground(Color.RED);
	 }
	 if(greens.isSelected()){
		 cb.setBackground(Color.GREEN);
	 }
	 if(blues.isSelected()){
		 cb.setBackground(Color.BLUE);
	 }
 }
 // Driver class 
 public static void main(String[] args) {
  JFrame f = new PickColor("Pick Color");
  f.setSize(150, 200);
  f.setVisible(true);
 }
 //Listener for scroll bar
 class ScrollbarListener implements AdjustmentListener{
  public void adjustmentValueChanged(AdjustmentEvent evt) {
   int red = colorBar.getValue();
   int green = colorBar.getValue();
   int blue = colorBar.getValue();

   redLabel.setText("Red = " + red);
   greenLabel.setText("Green = " + green);
   blueLabel.setText("Blue = " + blue);
   
  // Color newColors = new Color(red, green, blue);
  // reds.setBackground(newColors); 
  // greens.setBackground(newColors); 
  // blues.setBackground(newColors); 
   

   Color newColor = new Color(red, green, blue);
   redLabel.setBackground(newColor);
   greenLabel.setBackground(newColor);
   blueLabel.setBackground(newColor);
   
  }
 }
}

Where is the new selected color being shown?
The newColors variable is commented out.

You need to use which radio button is selected to determine which color component to change. Your code changes all three to the same value.

thanks for the help I solved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.