Results 1 to 6 of 6
  1. #1
    km0r3 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Question Accessing GUI object methods

    Hello,

    I am writing a simple game with Java and Swing. For writing the GUI I use the NetBeans design tool.

    In my code I have a custom java.awt.Canvas object named GameCanvas, in a JPanel Form called GamePanel, and I want to call a custom method terminarEstadoInicialin that object. Though, when I try to call it from the Panel NetBeans tells me that it cannot finds that symbol.

    Here you see that I instantiated my custom Canvas:



    This is the method in the Canvas file I try to call from the Panel file:



    And here is the error I get from NetBeans:



    Why is that not possible? Is there a way to access my custom Canvas object?
    Last edited by km0r3; 10-05-2010 at 08:12 PM. Reason: Removed a superfluous BBCode tag

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

    Default

    Where do you declare the canvas1 variable? Are you sure that it's declared as a GamePanel object and not a JPanel object? Also, why are you mixing AWT and Swing components together? You're much better off sticking with all-Swing components.

    Most here will recommend that you ditch the NetBeans code-generation and code Swing by hand. Then you can access anything and everything and have total control over your GUI design.
    Last edited by Fubarable; 10-05-2010 at 09:26 PM.

  3. #3
    km0r3 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Fubarable View Post
    Where do you declare the canvas1 variable?
    NetBeans seems to declare the variables at the end of the class. This is what it created automatically in GamePanel:
    Java Code:
        // Variables declaration - do not modify
        [snip]
        
        private java.awt.Canvas canvas1;
        
        [snip]
        // End of variables declaration
    AHA! Shouldn't it be mypackage.myclass.GameCanvas instead of java.awt.Canvas?

    Are you sure that it's declared as a GamePanel object and not a JPanel object?
    GamePanel is a JPanel object.

    Also, why are you mixing AWT and Swing components together? You're much better off sticking with all-Swing components.
    I would really like to stick 100% to Swing, but I'm not aware of the existence of a JCanvas class or do you mean "How to make a Canvas in Swing"?

    Most here will recommend that you ditch the NetBeans code-generation and code Swing by hand. Then you can access anything and everything and have total control over your GUI design.
    Hmm, sounds like others already have gathered some (negative) experience with NetBeans' code-generation. Thanks for your well-intentioned advice, and I will do that when there's no other solution.

    Maybe the NetBeans Form-Design tool is just not (yet) made for not-so-straight-forward purposes. (?)

    I hope I could provide you with all information you need.

    EDIT:
    Also, if you feel the need to take a closer look at the code, you can browse it here. I don't mean to be rude by not posting the code here, but I don't want to litter this thread with too much irrelevant code.
    Last edited by km0r3; 10-05-2010 at 09:53 PM.

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

    Default

    Quote Originally Posted by km0r3 View Post
    NetBeans seems to declare the variables at the end of the class. This is what it created automatically in GamePanel:
    Java Code:
        // Variables declaration - do not modify
        [snip]
        
        private java.awt.Canvas canvas1;
        
        [snip]
        // End of variables declaration
    AHA! Shouldn't it be mypackage.myclass.GameCanvas instead of java.awt.Canvas?
    If you hand-coded this, then yes it would be a GameCanvas object, not a Canvas object.


    GamePanel is a JPanel object.
    I was mixing up GamePanel and GameCanvas.

    I would really like to stick 100% to Swing, but I'm not aware of the existence of a JCanvas class or do you mean "How to make a Canvas in Swing"?
    No. Use a JPanel. It can do all Canvas can do and more.

  5. #5
    km0r3 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Smile

    Quote Originally Posted by Fubarable View Post
    If you hand-coded this, then yes it would be a GameCanvas object, not a Canvas object.
    It turns out, that this was my issue, but I had to "ditch the NetBeans code-generation" and to create the GUI "manually".

    Thanks for your kind help, Fubarable. :)

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

Similar Threads

  1. Methods in Object class
    By Navatha in forum New To Java
    Replies: 1
    Last Post: 09-28-2010, 11:17 PM
  2. Replies: 3
    Last Post: 03-24-2010, 12:24 AM
  3. Error Accessing Methods in My JAR file
    By avu in forum Advanced Java
    Replies: 4
    Last Post: 03-23-2010, 12:35 AM
  4. Replies: 2
    Last Post: 03-08-2010, 10:29 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •