Results 1 to 4 of 4
- 01-29-2013, 03:16 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Trying to change a JPanel's backcolor...
I'm using Eclipse.
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."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); } }
-
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.
- 01-29-2013, 05:06 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Re: Trying to change a JPanel's backcolor...
How do I get all panels on the contentPane?
Current code is something like this:
How do I check the type of the Component? I only want the MouseListener to be added to JPanels.Java Code:for(int i=0;i<=contentPane.getComponentCount()-1;i++) { contentPane.getComponent(i).addMouseListener(ma); }Last edited by b2271916; 01-29-2013 at 05:53 AM.
- 01-29-2013, 09:06 AM #4
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
-
Change JPanel text of Parent JPanel from JDialog
By bikashlama in forum AWT / SwingReplies: 7Last Post: 12-09-2011, 03:47 AM -
Change JPanel background color
By snaquetime in forum AWT / SwingReplies: 5Last Post: 06-04-2011, 11:03 PM -
Can I change the border of a JPanel cell?
By ryuzog in forum New To JavaReplies: 2Last Post: 10-10-2010, 06:05 AM -
Change JPanel background after its been set once
By mevets in forum AWT / SwingReplies: 4Last Post: 04-14-2010, 01:07 AM -
jtextfield backcolor
By devstarter in forum New To JavaReplies: 1Last Post: 03-01-2010, 06:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks