Results 1 to 13 of 13
  1. #1
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default JPanel cannot be resolved?

    Hi guys,

    I've hit a problem with an unusual error:

    Java Code:
    addCustPanel Cannot be resolved.
    The really annoying thing is that I designed this in netbeans first and it worked perfectly fine, I also opened the netbeans java file with "addCustPanel" in it in Eclipse and there are no resolve error in that, so what is going on?

    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.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default 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?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default 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();
             ...
    }
    And the following in another.


    Java Code:
    private void addCustButtonActionPerformed ( ActionEvent e ) {
            addCustPanel.setVisible ( true );
    }
    The scope of addCustPanel is declared in (and hence local to) the initComponents method. It is not visible to the addCustButtonActionPerformed method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default 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?

  6. #6
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default 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?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default 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 );
        }
    } );
    And it has the following error:
    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

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: JPanel cannot be resolved?

    How do I go about doing that then? :L

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default 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

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. the import x cannot be resolved
    By Hazza in forum New To Java
    Replies: 1
    Last Post: 11-12-2012, 11:44 AM
  2. cannot be resolved or is not a field
    By Stevemagegod in forum Eclipse
    Replies: 3
    Last Post: 04-10-2012, 12:14 AM
  3. Array cannot be resolved
    By Lorelai in forum New To Java
    Replies: 3
    Last Post: 04-06-2012, 06:53 AM
  4. Cannot be resolved to a type
    By Naxix in forum AWT / Swing
    Replies: 1
    Last Post: 03-18-2012, 12:16 PM
  5. Array cannot be resolved
    By aqeel2010 in forum New To Java
    Replies: 4
    Last Post: 10-07-2011, 06:38 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
  •