Results 1 to 13 of 13
Thread: JPanel cannot be resolved?
- 10-19-2013, 06:25 PM #1
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
JPanel cannot be resolved?
Hi guys,
I've hit a problem with an unusual error:
Java Code:addCustPanel Cannot be resolved.
The file is so long, some 316 lines, I've pasted it to pastebin:
custInfoGUI.java
The error is on line 306.
Any ideas on what is wrong with Eclipse and how I could get about fixing it?
Thanks.
- 10-19-2013, 11:23 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: JPanel cannot be resolved?
There is nothing wrong with Eclipse. You declared your variables in a method so their scope is limited to that method. Other methods can't see them.
If this worked in NetBeans then the problem is with NetBeans.
And BTW, it is bad technique to extend JFrame. Just create an instance. If you need to do some drawing or layout, extend JPanel and add to JFrame.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-21-2013, 08:06 PM #3
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
Re: JPanel cannot be resolved?
I opened up the same file in Eclipse again ( From netbeans ) and the red squiggle didn't appear?
Also how would I be able to create the new instance of JFrame and implement it into the code I posted?
- 10-21-2013, 08:56 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: JPanel cannot be resolved?
Your program should not compile as written.
You have the following in one method.
Java Code:private void initComponents() { GridBagConstraints gridBagConstraints; // Define new Components // The buttons: JButton addCustButton = new JButton ( "Add Customer" ); JButton findCustButton = new JButton ( "Find Customer" ); JButton backButton = new JButton ( "Back" ); JButton submitButton = new JButton ( "Submit" ); // The panels: JPanel addCustPanel = new JPanel(); ... }
Java Code:private void addCustButtonActionPerformed ( ActionEvent e ) { addCustPanel.setVisible ( true ); }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-21-2013, 09:03 PM #5
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
Re: JPanel cannot be resolved?
So what would be the best solution because I want to be able to have that panel appear when addCustButton is pressed?
- 10-21-2013, 09:14 PM #6
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
Re: JPanel cannot be resolved?
OK, I feel a little stupid as I've just realised and seen what Netbeans has done, and that's that it added variable prototypes at the very bottom of the main class ( which should be global ( if Java is as similar to C++ as I know ) ) allowing me to use it anywhere?
- 10-21-2013, 09:32 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: JPanel cannot be resolved?
You need to declare your instance fields in the top level portion of the class, not in any method. That way, they will be visible to all other methods of the enclosing class. Any variables declared within a method are of local scope to that method only.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-23-2013, 01:16 AM #8
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
Re: JPanel cannot be resolved?
Ah, I get it now... Another problem has occurred with the JFrame ( or atleast I think it is... )
I put the following into the main function:
Java Code:EventQueue.invokeLater ( new Runnable() { public void run() { new MainGUI().setVisible ( true ); } } );
Java Code:The Method setVisible(boolean) is undefined for the type MainGUI
NB. This is from a different project, but is structured EXACTLY the same as the one stated in the OP :L but the OP works perfectly fine... :S
- 10-23-2013, 01:28 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: JPanel cannot be resolved?
If the MainGUI class doesn't declare a setVisible method or inherit a class that does, you would get that message.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-23-2013, 10:42 PM #10
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
Re: JPanel cannot be resolved?
How do I go about doing that then? :L
- 10-24-2013, 12:09 AM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: JPanel cannot be resolved?
What does your MainGUI class look like? It makes no sense to create a setVisible method unless it can be somehow applied (like self painting).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-26-2013, 08:20 PM #12
Member
- Join Date
- Oct 2013
- Posts
- 10
- Rep Power
- 0
Re: JPanel cannot be resolved?
see the pastebin link below, it's easier if I paste the main.java file so you can see it for yourself...
main.java
- 10-26-2013, 08:43 PM #13
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: JPanel cannot be resolved?
You can't invoke setVisible from an instance of Main because Main doesn't extend anything or implement that method. However, in your program you
create an instance of JFrame called mainframe. After you invoke mainframe.pack(), then do mainframe.setVisible(true).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
the import x cannot be resolved
By Hazza in forum New To JavaReplies: 1Last Post: 11-12-2012, 12:44 PM -
cannot be resolved or is not a field
By Stevemagegod in forum EclipseReplies: 3Last Post: 04-10-2012, 01:14 AM -
Array cannot be resolved
By Lorelai in forum New To JavaReplies: 3Last Post: 04-06-2012, 07:53 AM -
Cannot be resolved to a type
By Naxix in forum AWT / SwingReplies: 1Last Post: 03-18-2012, 01:16 PM -
Array cannot be resolved
By aqeel2010 in forum New To JavaReplies: 4Last Post: 10-07-2011, 07:38 PM
Bookmarks