Results 1 to 4 of 4
  1. #1
    CS313e is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default 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

    Java Code:
    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
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  3. #3
    CS313e is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Got it figured out. I knew it was something dumb. I did not create an instance of my Listener class in my main method, thus it was not being called.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CS313e View Post
    Got it figured out. I knew it was something dumb. I did not create an instance of my Listener class in my main method, thus it was not being called.
    I'm glad that you have solved the problem and that you have posted a solution here. Next time though, please post links to cross-posts, and you'll likely get helpful replies here and elsewhere more quickly. Best of luck with your Java coding!

Similar Threads

  1. Event handling... help?
    By kevzspeare in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 09:46 PM
  2. Event handling in JSF
    By java08 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-24-2009, 07:42 AM
  3. rmi and event handling
    By darkhorse in forum Advanced Java
    Replies: 0
    Last Post: 03-15-2009, 09:20 AM
  4. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 01:21 PM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 07:05 PM

Posting Permissions

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