Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JRadioButton Event Handling Help?

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation JRadioButton Event Handling Help?

    Hello. I have been staring at this program and I cannot figure out what is wrong with it. Basically it is a stoplight whose lights are controlled by radio buttons. The program runs with no errors but the buttons do not work. I have a listener and have defined the actionperformed class but it is not working. I will post the code. Any help would be greatly appreciated.
    Thanks,
    Austin

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Lights extends JPanel
    {
      public JRadioButton btnR;
      public JRadioButton btnY;
      public JRadioButton btnG;
      public JRadioButton btnO;
      public Color rColor;
      public Color yColor;
      public Color gColor;
      public Color general;
     
      public void paintComponent ( Graphics g)
      {
     
        super.paintComponent ( g );
     
     
        general= getBackground();
        // Set foreground color
        g.setColor (Color.black);
     
        // get the size of the drawing area		
        Dimension size = this.getSize();
     
        // Bounding rectangle for the traffic lights
        int rectWidth = (int) (0.4 * size.width);
        int rectHeight = (int) (0.8 * size.height);
     
        // Location where the rectangle is to be drawn
        int xint = (int) (0.3 * size.width);
        int yint = (int) (0.1 * size.height);
     
        // Draw bounding rectangle
        g.drawRect (xint, yint, rectWidth, rectHeight);
     
        // Draw three circles inside the rectangle
        int circleHeight = (int) (0.9 * rectHeight / 3);
        int circleWidth = (int) (0.8 * rectWidth);
        int circleDia = (circleHeight < circleWidth)? circleHeight : circleWidth;
        int xOffset = (rectWidth - circleDia) / 2;
        int yOffset = (rectHeight - 3 * circleDia) / 4;
     
        xint = xint + xOffset; 
        yint = yint + yOffset;
     
        // Draw the red light 
        g.setColor(getBackground());
        g.setColor(rColor);
        g.fillOval (xint, yint, circleDia, circleDia);
        g.setColor(Color.black);
        g.drawOval (xint, yint, circleDia, circleDia);
        g.setColor(getBackground());
        rColor= general;
     
        // Draw the yellow light
        yint = yint + circleDia + yOffset;
        g.setColor(yColor);
        g.fillOval (xint, yint, circleDia, circleDia);
        g.setColor(Color.black);
        g.drawOval (xint, yint, circleDia, circleDia);
        g.setColor(getBackground());
        yColor= general;
     
        // Draw the green light
        yint = yint + circleDia + yOffset;
        g.setColor(gColor);
        g.fillOval (xint, yint, circleDia, circleDia);
        g.setColor(Color.black);
        g.drawOval (xint, yint, circleDia, circleDia);
        g.setColor(getBackground());
        gColor= general;
     
      }
     
     
    }
     
    public class TrafficLights
    {
      public static void main (String[] args)
      {
     
    	Lights lightPanel = new Lights();
    	lightPanel.setBorder ( BorderFactory.createEmptyBorder (10, 20, 10, 20 ));
     
        // Create buttons to simulate switches
        lightPanel.btnR = new JRadioButton ("Red");
        lightPanel.btnY = new JRadioButton ("Yellow");
        lightPanel.btnG = new JRadioButton ("Green");
        lightPanel.btnO = new JRadioButton ("Off");
        ButtonGroup switches = new ButtonGroup ();
        switches.add (lightPanel.btnR);
        switches.add (lightPanel.btnY);
        switches.add (lightPanel.btnG);
        switches.add (lightPanel.btnO);
     
        JPanel switchPanel = new JPanel();
        switchPanel.setBorder ( BorderFactory.createEmptyBorder (10, 20, 10, 20 ));
        switchPanel.setLayout (new GridLayout (1, 4));
        switchPanel.add (lightPanel.btnR);
        switchPanel.add (lightPanel.btnY);
        switchPanel.add (lightPanel.btnG);
        switchPanel.add (lightPanel.btnO);
     
     
     
        JFrame frame = new JFrame ("Traffic Lights");
        frame.getContentPane().add( lightPanel, BorderLayout.CENTER);
        frame.getContentPane().add( switchPanel, BorderLayout.SOUTH);
        frame.setSize (400, 600);
        frame.setVisible (true);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
      }
    }
     
    class LightButtonListener implements ActionListener
    {
      private Lights theLight;
      //public Color c;
      public LightButtonListener ( Lights aLight )
      {
        theLight = aLight;
        //create listener and add buttons to it
        theLight.btnR.addActionListener ( this );
        theLight.btnY.addActionListener ( this );
        theLight.btnG.addActionListener ( this );
        theLight.btnO.addActionListener ( this );
      }
     
      public void actionPerformed ( ActionEvent evt )
      {
        if ( evt.getSource() == theLight.btnR)
        {
          theLight.rColor= Color.red;
          theLight.repaint();
        }
        else if (evt.getSource() == theLight.btnY)
        {
        	theLight.yColor= Color.yellow;
            theLight.repaint();
        }
        else if (evt.getSource() == theLight.btnG)
        {
        	theLight.rColor= Color.green;
            theLight.repaint();
        }
        else if (evt.getSource() == theLight.btnO)
        {
            theLight.repaint();
        }
     
      }
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JRadioButton Event Handling Help?

    I do not see any code which actually creates the LightButtonListener, eg in your main you must create this Object passing the Lights instance to it, otherwise the code which adds the listeners never gets called.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JRadioButton Event Handling Help?

    Oh yes. Thanks so much. I knew it was something dumb like that.

Similar Threads

  1. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  2. Replies: 0
    Last Post: February 23rd, 2010, 01:12 PM
  3. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 09:29 AM
  4. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM
  5. Buttons don't work on using JRadioButton
    By neo_2010 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 11th, 2009, 11:37 AM