Results 1 to 5 of 5
  1. #1
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Post JRadioButton help (see pasted code)

    Here is my code that I have written for homework. The top portion of the code works and the background colors change when the buttons are clicked. I need the JRadioButtons to change the Foreground only by being clicked on. Every post that I've seen always showed someone clicking on a JButton before the effect of the JRadioButton was done. I need the action to occur as soon as a JRadioButton is selected.

    Hopefully this didn't confuse anyone, but here is the code.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.io.*;
    
    
    public class CaraballoPietri_Omar_lab_07_01 extends JFrame
    {
    	final int wWidth = 500;
    	final int wHeight = 300;
    	private JPanel topPanel;
    	private JPanel bottomPanel;
    	private JPanel centerPanel;
    
    	public CaraballoPietri_Omar_lab_07_01()
    	{
    		setTitle("Color Factory");
    		setSize(wWidth, wHeight);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setLayout(new BorderLayout());
    
    		topPanel();
    		bottomPanel();
    
    		JLabel directions = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color");
    
    		add(topPanel, BorderLayout.NORTH);
    		add(directions, BorderLayout.CENTER);
    		add(bottomPanel, BorderLayout.SOUTH);
    		setVisible(true);
    	}
    
    	private void topPanel()
    	{
    
    		setLayout(new FlowLayout());
    
    		JButton bRed = new JButton("Red");
    		bRed.setBackground(Color.RED);
    		bRed.addActionListener(new ButtonListener());
    		bRed.setActionCommand("red");
    
    		JButton bOrange = new JButton("Orange");
    		bOrange.setBackground(Color.ORANGE);
    		bOrange.setActionCommand("orange");
    		bOrange.addActionListener(new ButtonListener());
    
    		JButton bYellow = new JButton("Yellow");
    		bYellow.setBackground(Color.YELLOW);
    		bYellow.setActionCommand("yellow");
    		bYellow.addActionListener(new ButtonListener());
    
    		topPanel = new JPanel();
    		topPanel.setBackground(Color.WHITE);
    		topPanel.add(bRed);
    		topPanel.add(bOrange);
    		topPanel.add(bYellow);
    	}
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent e)
    		{
    			String actionCommand = e.getActionCommand();
    
    			if(actionCommand.equals("red"))
    			{
    				getContentPane().setBackground(Color.RED);
    			}
    			else if(actionCommand.equals("orange"))
    			{
    				getContentPane().setBackground(Color.ORANGE);
    			}
    			else if(actionCommand.equals("yellow"))
    			{
    				getContentPane().setBackground(Color.YELLOW);
    			}
    		}
    	}
    	private void bottomPanel()
    	{
    		JRadioButton rGreen = new JRadioButton("Green");
    		rGreen.setBackground(Color.GREEN);
    		rGreen.setActionCommand("Green");
    		rGreen.addActionListener(new RadioButtonListener());
    
    		JRadioButton rBlue = new JRadioButton("Blue");
    		rBlue.setBackground(Color.BLUE);
    		rBlue.setActionCommand("Blue");
    		rBlue.addActionListener(new RadioButtonListener());
    
    		JRadioButton rCyan = new JRadioButton("Cyan");
    		rCyan.setBackground(Color.CYAN);
    		rCyan.setActionCommand("Yellow");
    		rCyan.addActionListener(new RadioButtonListener());
    
    		ButtonGroup radio = new ButtonGroup();
    		radio.add(rGreen);
    		radio.add(rBlue);
    		radio.add(rCyan);
    
    		bottomPanel = new JPanel();
    		bottomPanel.setBackground(Color.WHITE);
    		bottomPanel.setLayout(new FlowLayout());
    		bottomPanel.add(rGreen);
    		bottomPanel.add(rBlue);
    		bottomPanel.add(rCyan);
    	}
    	private class RadioButtonListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent e)
    		{
    			String actionCommand = e.getActionCommand();
    			if (actionCommand.equals("Green"))
    			{
    				getContentPane().setForeground(Color.GREEN);
    			}
    			else if (actionCommand.equals("Blue"))
    			{
    				getContentPane().setForeground(Color.BLUE);
    			}
    			else if (actionCommand.equals("Cyan"))
    			{
    				getContentPane().setForeground(Color.CYAN);
    			}
    		}
    	}
    	public static void main (String[ ] args)
    	{
    		new CaraballoPietri_Omar_lab_07_01();
    	}
    }

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    This is irrelevant to your question but I would just like to point out that the subsequent statement (in the code tag) is superfluous because the pane's default layout is BorderLayout:
    Java Code:
    getContentPane().setLayout(new BorderLayout());

    EDIT: The subsequent statement (in the code tag) is also superfluous because a PANEL's default Layout is FlowLayout.
    Java Code:
    bottomPanel.setLayout(new FlowLayout());

    Also, it kind of looks better when you don't change the frame's layout to FlowLayout in topPanel(). Comment it out and see the difference.
    Last edited by Lil_Aziz1; 06-02-2010 at 12:47 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    oo I think I figured out what's wrong. You're changing the container's foreground. You have to change the Label's foreground. Just instantiate directions as a global variable (I think that's what you call it), remove the word "JLabel" before "directions" in the constructor, and change getContentPane() in RadioButtonListener to directions.

    EDIT: Also, you set the setActionCommand to "Yellow" for one of the JButtons when it is suppose to be "Cyan"

    EDIT: Also, the JPanel centerPanel is never used.
    Last edited by Lil_Aziz1; 06-02-2010 at 12:41 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Thumbs up

    You're a genius, I removed the extra comments snippets of code that I had, thanks for the clean-up. I also removed JLabel from in front of directions. I had to add it in my list of variables at the top or I would have received errors. And it worked like a charm. Thank you very much. So this is what my new code looks like.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.io.*;
    
    public class CaraballoPietri_Omar_lab_07_01 extends JFrame
    {
    	final int wWidth = 500;
    	final int wHeight = 300;
    	private JPanel topPanel;
    	private JPanel bottomPanel;
    	JLabel directions;
    
    	public CaraballoPietri_Omar_lab_07_01()
    	{
    		setTitle("Color Factory");
    		setSize(wWidth, wHeight);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		topPanel();
    		bottomPanel();
    
    		directions = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color");
    
    		add(topPanel, BorderLayout.NORTH);
    		add(directions, BorderLayout.CENTER);
    		add(bottomPanel, BorderLayout.SOUTH);
    		setVisible(true);
    	}
    
    	private void topPanel()
    	{
    
    		setLayout(new FlowLayout());
    
    		JButton bRed = new JButton("Red");
    		bRed.setBackground(Color.RED);
    		bRed.addActionListener(new ButtonListener());
    		bRed.setActionCommand("red");
    
    		JButton bOrange = new JButton("Orange");
    		bOrange.setBackground(Color.ORANGE);
    		bOrange.setActionCommand("orange");
    		bOrange.addActionListener(new ButtonListener());
    
    		JButton bYellow = new JButton("Yellow");
    		bYellow.setBackground(Color.YELLOW);
    		bYellow.setActionCommand("yellow");
    		bYellow.addActionListener(new ButtonListener());
    
    		topPanel = new JPanel();
    		topPanel.setBackground(Color.WHITE);
    		topPanel.add(bRed);
    		topPanel.add(bOrange);
    		topPanel.add(bYellow);
    	}
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent e)
    		{
    			String actionCommand = e.getActionCommand();
    
    			if(actionCommand.equals("red"))
    			{
    				getContentPane().setBackground(Color.RED);
    			}
    			else if(actionCommand.equals("orange"))
    			{
    				getContentPane().setBackground(Color.ORANGE);
    			}
    			else if(actionCommand.equals("yellow"))
    			{
    				getContentPane().setBackground(Color.YELLOW);
    			}
    		}
    	}
    	private void bottomPanel()
    	{
    		JRadioButton rGreen = new JRadioButton("Green");
    		rGreen.setBackground(Color.GREEN);
    		rGreen.setActionCommand("Green");
    		rGreen.addActionListener(new RadioButtonListener());
    
    		JRadioButton rBlue = new JRadioButton("Blue");
    		rBlue.setBackground(Color.BLUE);
    		rBlue.setActionCommand("Blue");
    		rBlue.addActionListener(new RadioButtonListener());
    
    		JRadioButton rCyan = new JRadioButton("Cyan");
    		rCyan.setBackground(Color.CYAN);
    		rCyan.setActionCommand("Cyan");
    		rCyan.addActionListener(new RadioButtonListener());
    
    		ButtonGroup radio = new ButtonGroup();
    		radio.add(rGreen);
    		radio.add(rBlue);
    		radio.add(rCyan);
    
    		bottomPanel = new JPanel();
    		bottomPanel.setBackground(Color.WHITE);
    		bottomPanel.add(rGreen);
    		bottomPanel.add(rBlue);
    		bottomPanel.add(rCyan);
    	}
    	private class RadioButtonListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent e)
    		{
    			String actionCommand = e.getActionCommand();
    			if (actionCommand.equals("Green"))
    			{
    				directions.setForeground(Color.GREEN);
    			}
    			else if (actionCommand.equals("Blue"))
    			{
    				directions.setForeground(Color.BLUE);
    			}
    			else if (actionCommand.equals("Cyan"))
    			{
    				directions.setForeground(Color.CYAN);
    			}
    		}
    	}
    	public static void main (String[ ] args)
    	{
    		new CaraballoPietri_Omar_lab_07_01();
    	}
    }//

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    np. I still think you should remove the setLayout(new FlowLayout()); in topPanel() and make all your global variables private because it would be consistent with the JLabel and because it's convention. Also, if that's all your code, then you should remove the following imports:
    Java Code:
    import java.text.DecimalFormat;
    import java.io.*;
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. JRadioButton Event Handling Help???
    By CS313e in forum AWT / Swing
    Replies: 3
    Last Post: 03-27-2010, 05:35 PM
  2. Replies: 7
    Last Post: 11-23-2009, 08:42 AM
  3. BackGround JRadioButton
    By antonello12 in forum AWT / Swing
    Replies: 1
    Last Post: 10-10-2009, 06:33 AM
  4. Regarding JTree with JCheckBox and JRadioButton
    By partha_adt in forum AWT / Swing
    Replies: 0
    Last Post: 11-11-2008, 10:03 AM
  5. JRadioButton set object color
    By King8654 in forum AWT / Swing
    Replies: 4
    Last Post: 04-24-2008, 09:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •