#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    GUI - Adding an Action Listener


    Hi, I'm trying to write a color factory program.
    I get red squiggles under "ButtonListener" when I try to make it a private class implementing Action Listener.

    If you could help me figure out what's wrong, it'd be much appreciated. Thanks!

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ColorFactory extends JFrame {
    	
    	final int WIDTH = 500;
    	final int HEIGHT = 300;
    	
    	private JButton redButton, orangeButton, yellowButton;
    	private JRadioButton greenRadioButton, blueRadioButton, cyanRadioButton;
    	private ButtonGroup radioButtonGroup;
    	private JPanel topPane;
    	private JPanel centerPane;
    	private JPanel bottomPane;
    	private JLabel label;
    
    	public ColorFactory() 
    	{
    		setTitle("Color Factory");
    		setSize(WIDTH,HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		getContentPane();
    		setLayout(new BorderLayout());
    		
    		buildTopPanel();
    		add(topPane, BorderLayout.NORTH);
    		
    		add(centerPane, BorderLayout.CENTER);
    		
    		buildBottomPanel();
    		add(bottomPane, BorderLayout.SOUTH);
    		
    		label = new JLabel ("Top buttons change the panel color and bottom radio buttons change the text color.");
    		centerPane.add(label);
    	}
    	
    	private void buildTopPanel()
    	{
    		JPanel topPane = new JPanel();
    		topPane.setLayout(new FlowLayout());
    		topPane.setBackground(Color.WHITE); // set top panel's background color to white
    		
    		//create buttons
    		redButton = new JButton("Red");
    		orangeButton = new JButton("Orange");
    		yellowButton = new JButton("Yellow");
    		
    		
    		//set button color to corresponding text color
    		redButton.setBackground(Color.RED);
    		orangeButton.setBackground(Color.ORANGE);
    		yellowButton.setBackground(Color.YELLOW);
    		
    		topPane.add(redButton);
    		topPane.add(orangeButton);
    		topPane.add(yellowButton);
    		
    		private class ButtonListener implements ActionListener
    		{
    			public void actionPerformed (ActionEvent buttonClicked)
    			{
    				if (buttonClicked.getSource() == redButton)
    					centerPane.setBackground(Color.RED);
    				if (buttonClicked.getSource() == orangeButton)
    					centerPane.setBackground(Color.ORANGE);
    				if (buttonClicked.getSource() == yellowButton)
    					centerPane.setBackground(Color.YELLOW);			
    			}
    		} // end of private class ButtonListener
    		
            redButton.addActionListener(new ButtonListener());
            orangeButton.addActionListener(new ButtonListener());
            yellowButton.addActionListener(new ButtonListener());
    		
    
    	} // end of topPanel()
    	
    
    	private void buildBottomPanel()
    	{
    		JPanel bottomPane = new JPanel();
    		bottomPane.setLayout(new FlowLayout());
    		bottomPane.setBackground(Color.WHITE); // set top panel's background color to white
    		
    		//create buttons
    		greenRadioButton = new JRadioButton("Green");
    		blueRadioButton = new JRadioButton("Blue");
    		cyanRadioButton = new JRadioButton("Cyan");
    		
    		radioButtonGroup = new ButtonGroup();
    		radioButtonGroup.add(greenRadioButton);
    		radioButtonGroup.add(blueRadioButton);
    		radioButtonGroup.add(cyanRadioButton);
    		
    		bottomPane.add(greenRadioButton);
    		bottomPane.add(blueRadioButton);
    		bottomPane.add(cyanRadioButton);
    		
    		greenRadioButton.setForeground(Color.GREEN);
    		blueRadioButton.setForeground(Color.BLUE);
    		cyanRadioButton.setForeground(Color.CYAN);
    		
    		private class RadioButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent radioSelected)
    			{
    				if (radioSelected.getSource() == greenRadioButton)
    					centerPane.setForeground(Color.GREEN);
    				if (radioSelected.getSource() == blueRadioButton)
    					centerPane.setForeground(Color.BLUE);
    				if (radioSelected.getSource() == cyanRadioButton)
    					centerPane.setForeground(Color.CYAN);
    			}
    		}
    		
    		greenRadioButton.addActionListener(new RadioButtonListener());
    		blueRadioButton.addActionListener(new RadioButtonListener());
    		cyanRadioButton.addActionListener(new RadioButtonListener());
    		
    	}
    	
    	public static void main(String[] args)
    	{
    		ColorFactory factory1 = new ColorFactory();
    		
    		factory1.setVisible(true);
    
    	}
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Are You Basically Trying to do this ?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class ColorFactory extends JFrame {
    	
    	final int WIDTH = 500;
    	final int HEIGHT = 300;
    	Color clr;
    	private JButton redButton, orangeButton, yellowButton;
    	private JRadioButton greenRadioButton, blueRadioButton, cyanRadioButton;
    	private JPanel topPane;
    	private JPanel centerPane;
    	private JPanel bottomPane;
    public void paint(Graphics g){
    	
    	super.paint(g);
    	g.setColor(clr);
    	//g.fillRect(00, 00, 400, 400);
    	 System.out.println(g.getColor());
    	
    }
    	public ColorFactory() 
    	{
    		final JPanel topPanel=new JPanel();
    		topPanel.setSize(500, 500);
    		topPanel.setPreferredSize(new Dimension(400,400));
    		setVisible(true);
    		setTitle("Color Factory");
    		setSize(WIDTH,HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new FlowLayout());
    		//buildTopPanel();
    		//create buttons
    				redButton = new JButton("Red");
    				orangeButton = new JButton("Orange");
    				yellowButton = new JButton("Yellow");
    				
    				//set button color to corresponding text color
    				redButton.setBackground(Color.RED);
    				orangeButton.setBackground(Color.ORANGE);
    				yellowButton.setBackground(Color.YELLOW);
    				topPanel.add(redButton);
    				topPanel.add(orangeButton);
    				topPanel.add(yellowButton);
    				add(topPanel);
    		//add(centerPane);
    				redButton.addActionListener(new ActionListener(){
    
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    					
     						
    						clr=(Color.RED);
    						topPanel.setBackground(clr);
    							
    						repaint();
    						
    					}
    				
    						
    						
    						
    						
    						
    				});
    				orangeButton.addActionListener(new ActionListener(){
    
    					@Override
    					public void actionPerformed(ActionEvent e) {
    						// TODO Auto-generated method stub
    					
    				
    			clr=Color.ORANGE;
    			topPanel.setBackground(clr);
    
    			
    			repaint();
    						}
    				
    						});
    						
    						
    						
    					
    					
    					
    			 
    				yellowButton.addActionListener(new ActionListener(){
    
    					@Override
    					public void actionPerformed(ActionEvent e) {
    						// TODO Auto-generate
    			clr=Color.YELLOW;
    			topPanel.setBackground(clr);
    
    					repaint();	
    					}
    					
    					
    	 				
    						
    						
    						
    				});
    	}
    	
    	private void buildTopPanel()
    	{
    		JPanel topPanel = new JPanel();
    		topPanel.setLayout(new FlowLayout());
    		topPanel.setBackground(Color.WHITE); // set top panel's background color to white
    		
    		
    	}
    		 
    
    	public static void main(String[] args) {
    
    new ColorFactory();
    	}
     
    
     
    
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    I edited my first post to contain all the code that I have (so far).

    And almost. My GUI is similar, except there are also 3 radio buttons that you can select at the bottom to change the text color.

    I am unfamiliar with the @Override and therefore doubt I can use it in my program.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Originally Posted by jayemes22
    I edited my first post to contain all the code that I have (so far).

    And almost. My GUI is similar, except there are also 3 radio buttons that you can select at the bottom to change the text color.

    I am unfamiliar with the @Override and therefore doubt I can use it in my program.
    I Can Only help this much. Good Luck !


    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class ColorFactory extends JFrame {
    	
    	final int WIDTH = 500;
    	final int HEIGHT = 300;
    	Color clr;
    	private JButton redButton, orangeButton, yellowButton;
    	private JRadioButton greenRadioButton, blueRadioButton, cyanRadioButton;
    	private JPanel topPane;
    	private JPanel centerPane;
    	private JPanel bottomPane;
    public void paint(Graphics g){
    	
    	super.paint(g);
    	g.setColor(clr);
    	//g.fillRect(00, 00, 400, 400);
    	 System.out.println(g.getColor());
    	
    }
    	public ColorFactory() 
    	{
    		final JPanel pnl=new JPanel();
    		pnl.setPreferredSize(new Dimension(200,200));
    		  cyanRadioButton=new JRadioButton("CYAN");
    		  greenRadioButton=new JRadioButton("GREEN");
    		  blueRadioButton=new JRadioButton("BLUE");
    		  cyanRadioButton.addActionListener(new ActionListener(){
    
    			 
    			public void actionPerformed(ActionEvent arg0) {
    				// TODO Auto-generated method stub
    				if (arg0.getSource() == cyanRadioButton)
    					clr=(Color.CYAN);
    				 pnl.setBackground(clr);
    					
    				repaint();
    
    			}
    			  
    			  
    		  });
    		  greenRadioButton.addActionListener(new ActionListener(){
    
    				 
    				public void actionPerformed(ActionEvent arg0) {
    					// TODO Auto-generated method stub
    					if (arg0.getSource() == greenRadioButton)
    						
    						
    						clr=(Color.GREEN);
    					pnl.setBackground(clr);
    						
    					repaint();
    						  
    				}
    				  
    				  
    			  });
    		  blueRadioButton.addActionListener(new ActionListener(){
    
    				 
    				public void actionPerformed(ActionEvent arg0) {
    					// TODO Auto-generated method stub
    					if (arg0.getSource() == blueRadioButton)
    						clr=(Color.BLUE);
    					pnl.setBackground(clr);
    						
    					repaint();
    						
    					 
    				}
    				  
    				  
    			  });
    		  
    		 pnl. add(cyanRadioButton);
    		  pnl.add(greenRadioButton);
    		  pnl.add(blueRadioButton);
    		  add(pnl);
    		final JPanel topPanel=new JPanel();
    		topPanel.setSize(500, 500);
    		topPanel.setPreferredSize(new Dimension(400,400));
    		setVisible(true);
    		setTitle("Color Factory");
    		setSize(WIDTH,HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new FlowLayout());
    		//buildTopPanel();
    		//create buttons
    				redButton = new JButton("Red");
    				orangeButton = new JButton("Orange");
    				yellowButton = new JButton("Yellow");
    				
    				//set button color to corresponding text color
    				redButton.setBackground(Color.RED);
    				orangeButton.setBackground(Color.ORANGE);
    				yellowButton.setBackground(Color.YELLOW);
    				topPanel.add(redButton);
    				topPanel.add(orangeButton);
    				topPanel.add(yellowButton);
    				add(topPanel);
    		//add(centerPane);
    				redButton.addActionListener(new ActionListener(){
    
    					@Override
    					public void actionPerformed(ActionEvent arg0) {
    						// TODO Auto-generated method stub
    					
     						
    						clr=(Color.RED);
    						topPanel.setBackground(clr);
    							
    						repaint();
    						
    					}
    				
    						
    						
    						
    						
    						
    				});
    				orangeButton.addActionListener(new ActionListener(){
    
    					 
    					public void actionPerformed(ActionEvent e) {
    						// TODO Auto-generated method stub
    					
    				
    			clr=Color.ORANGE;
    			topPanel.setBackground(clr);
    
    			
    			repaint();
    						}
    				
    						});
    						
    						
    						
    					
    					
    					
    			 
    				yellowButton.addActionListener(new ActionListener(){
    
    					 
    					public void actionPerformed(ActionEvent e) {
    						// TODO Auto-generate
    			clr=Color.YELLOW;
    			topPanel.setBackground(clr);
    
    					repaint();	
    					}
    					
    					
    	 				
    						
    						
    						
    				});
    	}
    	
    	private void buildTopPanel()
    	{
    		JPanel topPanel = new JPanel();
    		topPanel.setLayout(new FlowLayout());
    		topPanel.setBackground(Color.WHITE); // set top panel's background color to white
    		
    		
    	}
    		 
    
    	public static void main(String[] args) {
    
    new ColorFactory();
    	}
     
    
     
    
    }
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    unfamiliar with the @Override
    Using @Override is a request to the compiler to check if the method definition following the the @Override statement overrides a method in the super class. It is useful to know if your coding is correct when you are trying to override a method.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    I was able to figure it out.
    Thank you for all the replies/information!

IMN logo majestic logo threadwatch logo seochat tools logo