Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot call method

 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to call "setI" from class1. Can somone help me and show me what is wrong?








Java is treating MyPanel exactly like a JPanel, and won't let me create any new methods for it
However I need to be able to call custom (and non-static) methods from the class. Is this possible?
If not, any alternatives?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that when you say "Does not work", you mean that there is a compiler error. MyPanel.setI(int i) expects an integer as an input parameter. Do you know how to do this when calling the method?
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




You need to pass an int variable to the setI(...) method.
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once I changed that, it worked... which is really confusing. I tried to replicate a problem from a larger program without posting the code.

The error I get is
The method setDrawValues(String, int, int, int, int) is undefined for the type JPanel

I think it is a very stupid mistake, but I can't find it. I'll post the necessary code here:
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moreover, when I type in graph. [CTRL + SPACE], "setDrawValues" is not even visible in the intellisense menu.
This is really confusing
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've declared graph as a JPanel. This means you can only call methods that are available in JPanel.

These two problems are not directly related to GUI programming so I'm moving this thread to Beginning Java.
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.

Anyway, after some trial and error I managed to solve this by like this:


 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whereas you created a MyGraphPanel, the variable graph is defined as a JPanel, which is fine because MyGraphPanel ISA JPanel. JPanel, however, does not have a setDrawValues() with the parameters that you specify.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic