simo1.java

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


public class simo2 extends JFrame implements ActionListener{
	private JRadioButton red;
	private JRadioButton yellow;
	private JRadioButton blue;
	private JRadioButton green;
private JRadioButton magenta;

public simo2()
{
	setLayout(new FlowLayout());
	setVisible(true);
	setSize(400,250);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	red=new JRadioButton(" red");
	yellow=new JRadioButton(" yellow");
	blue=new JRadioButton(" blue");
	green=new JRadioButton(" green");
	magenta=new JRadioButton(" magenta");
	ButtonGroup group=new ButtonGroup();
	group.add(red);
	group.add(yellow);
	group.add(blue);
	group.add(green);
	group.add(magenta);
	red.addActionListener(this);
	yellow.addActionListener(this);
	blue.addActionListener(this);
	green.addActionListener(this);
	magenta.addActionListener(this);
	add(red);
	add(yellow);
	add(blue);
	add(green);
	add(magenta);
}
public void actionPerformed(ActionEvent e)
{
	if(e.getSource()== red)
	{
		setBackground(Color.red);
	}
	if(e.getSource()== yellow)
	{
		setBackground(Color.yellow);
	}
	if(e.getSource()== blue)
	{
		setBackground(Color.blue);
	}
	if(e.getSource()== green)
	{
		setBackground(Color.green);
	}
	if(e.getSource()== magenta)
	{
		setBackground(Color.magenta);
	}
	
	}
	
}

In simo1.java

public class simo1 {
	public static void main(String[] args)
	{
		simo2 s=new simo2();
				}

}

Everything is compiling fine and in output radio button is displayed well with all color text displayed in it but when clicking red, no background color is displayed and also for all similar colors. Can't get background color when clicking selective button??

Add a Container object to the code and use that to set the color:

Container contr;

public SetColorWithButton()  {
   contr = getContentPane();

  ...
		contr.setBackground(Color.yellow);
   ...
   repaint();  // after setting the color at end of listener

Edited 6 Years Ago by NormR1: n/a

Add a Container object to the code and use that to set the color:

Container contr;

public SetColorWithButton()  {
   contr = getContentPane();

  ...
		contr.setBackground(Color.yellow);
   ...
   repaint();  // after setting the color at end of listener

Yeah added ,but color is not displaying when clicking particular radio button

I guess you did not follow NormR1's instruction precisely. I have tested your program. It works.

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

class simo2 extends JFrame implements ActionListener{
	private JRadioButton red;
	private JRadioButton yellow;
	private JRadioButton blue;
	private JRadioButton green;
    private JRadioButton magenta;
    Container container;

public simo2()
{
	super("Radio Button Test");
	container = getContentPane();
	container.setLayout(new FlowLayout());
	setVisible(true);
	setSize(400,250);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	red=new JRadioButton(" red");
	yellow=new JRadioButton(" yellow");
	blue=new JRadioButton(" blue");
	green=new JRadioButton(" green");
	magenta=new JRadioButton(" magenta");
	ButtonGroup group=new ButtonGroup();
	group.add(red);
	group.add(yellow);
	group.add(blue);
	group.add(green);
	group.add(magenta);
	red.addActionListener(this);
	yellow.addActionListener(this);
	blue.addActionListener(this);
	green.addActionListener(this);
	magenta.addActionListener(this);
	container.add(red);
	container.add(yellow);
	container.add(blue);
	container.add(green);
	container.add(magenta);
}
public void actionPerformed(ActionEvent e)
{
	if(e.getSource()== red)
	{
		container.setBackground(Color.red);
	}
	if(e.getSource()== yellow)
	{
		container.setBackground(Color.yellow);
	}
	if(e.getSource()== blue)
	{
		container.setBackground(Color.blue);
	}
	if(e.getSource()== green)
	{
		container.setBackground(Color.green);
	}
	if(e.getSource()== magenta)
	{
		container.setBackground(Color.magenta);
	}
	
	}
	
}
public class simo1 {
	public static void main(String[] args)
	{
		simo2 s=new simo2();
				}

}

Edited 6 Years Ago by tong1: n/a

JButtons, JRadioButtons, JLabels, and so on come from lightweight package swing hence not are shown (overlapped) when meeting (conflicting with) heavyweight components, e.g. buttons, labels,... from java.awt. The following class extends Frame (heavyweight) rather than JFrame. Therefore, the JRadioButtons must be placed in one subframe: BorderLayout.NORTH. If they were placed directedly in the Frame, they would all disappear.
The following code also works.

import javax.swing.*;  // lightweight package
import java.awt.*;  // heavyweight package
import java.awt.event.*;
// Frame from java.awt. its heavy
class simo2 extends Frame implements ActionListener{
	private JRadioButton red;  // lightweight component
	private JRadioButton yellow; // lightweight component
	private JRadioButton blue; // lightweight component
	private JRadioButton green; // lightweight component
    private JRadioButton magenta;  // lightweight component


public simo2(){
	setLayout(new BorderLayout());
	setVisible(true);
	setSize(400,250);
	JPanel p = new JPanel();
	red=new JRadioButton(" red");
	yellow=new JRadioButton(" yellow");
	blue=new JRadioButton(" blue");
	green=new JRadioButton(" green");
	magenta=new JRadioButton(" magenta");
	ButtonGroup group=new ButtonGroup();
	group.add(red);
	group.add(yellow);
	group.add(blue);
	group.add(green);
	group.add(magenta);
	red.addActionListener(this);
	yellow.addActionListener(this);
	blue.addActionListener(this);
	green.addActionListener(this);
	magenta.addActionListener(this);
	p.add(red);   // place the light component in a saparate container.
	p.add(yellow);
	p.add(blue);
	p.add(green);
	p.add(magenta);
	add(p,BorderLayout.NORTH);	
}
public void actionPerformed(ActionEvent e){
	if(e.getSource()== red){
		setBackground(Color.red);
	}
	if(e.getSource()== yellow){
		setBackground(Color.yellow);
	}
	if(e.getSource()== blue){
		setBackground(Color.blue);
	}
	if(e.getSource()== green){
		setBackground(Color.green);
	}
	if(e.getSource()== magenta){
		setBackground(Color.magenta);
	}	
	}
	
}
public class simo0 {
	public static void main(String[] args){
		simo2 s=new simo2();
		s.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
			});
	}			
}
This article has been dead for over six months. Start a new discussion instead.