Results 1 to 4 of 4
  1. #1
    b2271916 is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Trying to change a JPanel's backcolor...

    I'm using Eclipse.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.border.BevelBorder;
    
    
    public class mainForm extends JFrame {
    
    	private JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					mainForm frame = new mainForm();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public mainForm() {
    		setTitle("Test");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		 JPanel panel = new JPanel();
    		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    		panel.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    				panel.setBackground(Color.black);
    			
    			}
    		});
    		panel.setBounds(112, 62, 91, 72);
    		contentPane.add(panel);
    	}
    }
    On the line: "panel.setBackground(Color.black)", I'm getting the error: "Cannot refer to a non-final variable panel inside an inner class defined in a different method."

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

    Default Re: Trying to change a JPanel's backcolor...

    That error is correct: you cannot refer to a non-final local variable from within your anonymous MouseListener inner class. The reasons for this is due to the inner class making a local copy of the variable before using it, and if the variable is non-final then logic errors might occur. The simple solution: declare the panel JPanel variable final.

  3. #3
    b2271916 is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Trying to change a JPanel's backcolor...

    How do I get all panels on the contentPane?
    Current code is something like this:
    Java Code:
    for(int i=0;i<=contentPane.getComponentCount()-1;i++)
    		{
    					contentPane.getComponent(i).addMouseListener(ma);
    		}
    How do I check the type of the Component? I only want the MouseListener to be added to JPanels.
    Last edited by b2271916; 01-29-2013 at 05:53 AM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Trying to change a JPanel's backcolor...

    Use instanceof to determine the type
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 03:47 AM
  2. Change JPanel background color
    By snaquetime in forum AWT / Swing
    Replies: 5
    Last Post: 06-04-2011, 11:03 PM
  3. Can I change the border of a JPanel cell?
    By ryuzog in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 06:05 AM
  4. Change JPanel background after its been set once
    By mevets in forum AWT / Swing
    Replies: 4
    Last Post: 04-14-2010, 01:07 AM
  5. jtextfield backcolor
    By devstarter in forum New To Java
    Replies: 1
    Last Post: 03-01-2010, 06:16 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
  •