Java Forum / General / December 2005
Eclipse - access by method name requires a receiver
S - 06 Dec 2005 12:18 GMT <a href="http://home.online.no/~phersta/siggen/pictures/java-error.JPG">This picture shows my problem</a>
Have tried to google and find information about this, and I`m sorry if this is a noob-question. I am using eclipse (ver 3.1.1.) with java 1.5.0.06.
I am trying to make a GUI using VE in eclipse. And so far I have managed to make make some JTabbedPane, JPanes and JButtons. It seemed to work - with no errors. But when I opened the file again today - all I got was this message - and I have no idea on how to solve it.
2. question: I have a calculation.class who does all calculations. And I want to use methods from this inside my jAppletRobotArm.java to calculate things. But I can not seem to find the right place to add (calculations calculation = new calculations()) - parameter. Is there something special to deal with since this is an applet? How to make it easiest?
Thanks in advantage for the swift replies.
Hendrik Maryns - 06 Dec 2005 12:30 GMT S schreef:
> <a > href="http://home.online.no/~phersta/siggen/pictures/java-error.JPG">This > picture shows my problem</a> Seems like this is caused by a plugin. I don´t know where you get the upper part of the editor window from.
BUT more important: start all your Java classes with a capital letter (Eclipse warns about this!)
And you might want to use the Java perspective instead of resource.
> 2. question: I have a calculation.class who does all calculations. And > I want to use methods from this inside my jAppletRobotArm.java to > calculate things. But I can not seem to find the right place to add > (calculations calculation = new calculations()) - parameter. Is there > something special to deal with since this is an applet? How to make it > easiest? Have you imported it? Once again, start class names with a capital letter.
H.
 Signature Hendrik Maryns
================== www.lieverleven.be http://aouw.org
S - 06 Dec 2005 12:58 GMT Thanks for a very swift reply. Have changed all class-names and now it works. However I still have one problem. I have these lines (removed lines not necessary for this example)
public class JAppletRobotArm extends JApplet { private Calculations calculations = new Calculations(); }
When I run debug mode - this will run, and start the constructor of Calcuations, and do everything I have asked for in the constructor.
However I can not for instance write: this.calculations.doSomething() later on, when for instance a button is pressed.
I have also tried to write
public class JAppletRobotArm extends JApplet { private Calculations calculations = null; }
and ad the creation of the class here:
public JAppletRobotArm() { super(); }
or here:
public void init() { this.setSize(500, 400); this.setContentPane(getJContentPane()); this.calculations = new Calculations(); }
withouth any luck. I can not seem to use this.calculations.someMethod() anywhere in the code.
Is there a major points I have missed here? I should be able to call methods from other classes when pushing a button in one class.
Thanks again for a swift reply.
Hendrik Maryns - 06 Dec 2005 13:25 GMT S schreef:
> Thanks for a very swift reply. Have changed all class-names and now it > works. Good, though I don´t see how the two could be related (Java does work with non-conventional class names, there are even a lot of them defined in the API, it is just not recommended, definitely not if you hope for help on this forum).
However I still have one problem.
> I have these lines (removed lines not necessary for this example) > [quoted text clipped - 19 lines] > super(); > } I don´t know what you mean by this. I don´t suppose calculations is initialised in JApplet.
> or here: > [quoted text clipped - 3 lines] > this.calculations = new Calculations(); > } Should work _if you invoke this method_. But it seems like a very useless method to me. You might consider to make it private.
> withouth any luck. I can not seem to use this.calculations.someMethod() > anywhere in the code. Strange.
> Is there a major points I have missed here? I should be able to call > methods from other classes when pushing a button in one class. Why don´t you post some real code that exhibits the problem. See http://mindprod.com/jgloss/sscce.html
Cheers, H.
 Signature Hendrik Maryns
================== www.lieverleven.be http://aouw.org
S - 06 Dec 2005 13:50 GMT The calculations should be initialized in the JAppletRobotArm.java - because this is the main applet. Do I need a main() method in an Applet?
So what I want to do is to initialize Calculations calculation, and use methods from this class for instance in: private JButton getJButton() {
Thanks once again for the swift reply.
This is the code (removed comments) as it is in JAppletRobotArm.java =================================================== import javax.swing.JPanel; import javax.swing.JApplet; import javax.swing.JTabbedPane; import javax.swing.JButton; public class JAppletRobotArm extends JApplet { private static final long serialVersionUID = 2621050794148530944L; private JPanel jContentPane = null; private JTabbedPane jTabbedPane = null; private JPanel jPanelOptions = null; private JPanel jPanelGraphs = null; private JPanel jPanelStatistics = null; private JButton jButton = null; private JButton jButtonNextGen = null; private JPanel jPanelRobotArm1 = null; private JPanel jPanelRobotArm2 = null;
public JAppletRobotArm() { super(); Calculations calculation = new Calculations(); }
private JTabbedPane getJTabbedPane() { if (jTabbedPane == null) { jTabbedPane = new JTabbedPane(); jTabbedPane.setBounds(new java.awt.Rectangle(1,5,492,342)); jTabbedPane.addTab("Graphs", null, getJPanelGraphs(), null); jTabbedPane.addTab("Options", null, getJPanelOptions(), null); jTabbedPane.addTab("Statistics", null, getJPanelStatistics(), null); } return jTabbedPane; }
private JPanel getJPanelOptions() { if (jPanelOptions == null) { jPanelOptions = new JPanel(); jPanelOptions.setLayout(null); } return jPanelOptions; }
private JPanel getJPanelGraphs() { if (jPanelGraphs == null) { jPanelGraphs = new JPanel(); jPanelGraphs.setLayout(null); jPanelGraphs.add(getJButton(), null); jPanelGraphs.add(getJButtonNextGen(), null); jPanelGraphs.add(getJPanelRobotArm1(), null); jPanelGraphs.add(getJPanelRobotArm2(), null); } return jPanelGraphs; }
private JPanel getJPanelStatistics() { if (jPanelStatistics == null) { jPanelStatistics = new JPanel(); } return jPanelStatistics; }
private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new java.awt.Rectangle(142,6,104,25)); jButton.setText("Start/pause"); jButton.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent e) { if ((e.getPropertyName().equals("enabled"))) { System.out.println("propertyChange(enabled)"); // TODO Auto-generated property Event stub "enabled" } } }); } return jButton; }
private JButton getJButtonNextGen() { if (jButtonNextGen == null) { jButtonNextGen = new JButton(); jButtonNextGen.setBounds(new java.awt.Rectangle(8,5,130,27)); jButtonNextGen.setText("Next Generation"); } return jButtonNextGen; }
private JPanel getJPanelRobotArm1() { if (jPanelRobotArm1 == null) { jPanelRobotArm1 = new JPanel(); jPanelRobotArm1.setPreferredSize(new java.awt.Dimension(200,200)); jPanelRobotArm1.setSize(new java.awt.Dimension(200,200)); jPanelRobotArm1.setLocation(new java.awt.Point(7,33)); } return jPanelRobotArm1; } private JPanel getJPanelRobotArm2() { if (jPanelRobotArm2 == null) { jPanelRobotArm2 = new JPanel(); jPanelRobotArm2.setBounds(new java.awt.Rectangle(208,33,200,200)); jPanelRobotArm2.setPreferredSize(new java.awt.Dimension(200,200)); } return jPanelRobotArm2; }
public void init() { this.setSize(500, 400); this.setContentPane(getJContentPane()); //this.calculation = new Calculations(); }
private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJTabbedPane(), null); } return jContentPane; }
} // @jve:decl-index=0:visual-constraint="0,7"
Hendrik Maryns - 06 Dec 2005 14:25 GMT S schreef:
> The calculations should be initialized in the JAppletRobotArm.java - > because this is the main applet. Do I need a main() method in an > Applet? I don´t know, never wrote an applet. See http://mindprod.com/jgloss/applet.html
> So what I want to do is to initialize Calculations calculation, and use > methods from this class for instance in: private JButton getJButton() { [quoted text clipped - 8 lines] > Calculations calculation = new Calculations(); > } And what does Java do now? Forget about calculation, of course! It is a local variable. Define it outside the constructor, just like all those swing thingies above.
HTH, H.
 Signature Hendrik Maryns
================== www.lieverleven.be http://aouw.org
S - 06 Dec 2005 15:17 GMT Hehe, well - got to admit I don`t realy understand what was wrong, but now it works. Moved the define outside the constructor as you said, and tried it... And now it works. Guess it was just a result of to mutch work and no fun.
Hope I did not spend to mutch of your time, and thanks for the help.
Roedy Green - 06 Dec 2005 20:37 GMT >Do I need a main() method in an >Applet? Yes, then you can debug or run it as an application. See http://mindprod.com/jgloss/japplet.html#HYBRID for how to write one.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 06 Dec 2005 20:39 GMT >private JPanel jPanelOptions = null; all the = null don't do anything. instance referenced by default are initialised to null.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
S - 08 Dec 2005 13:40 GMT Yes, I know. But I use the inbuilt Visual Editor in Eclipse, so it automatically generates these and other methods. I know it is better to write the GUI "by hand", but I really don`t like it - and just want an easy way to make a GUI that works. Any other recommendations than using the VE would happily be accepted.
Roedy Green - 06 Dec 2005 20:40 GMT > Calculations calculation = new Calculations(); that should have been an instance variable.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 06 Dec 2005 20:43 GMT > private JTabbedPane getJTabbedPane() { > if (jTabbedPane == null) { [quoted text clipped - 5 lines] > } > return jTabbedPane; It is much less wicked to have a long method so long as it contains no conditional logic. Just pile the initialisation code into your init method or if you want to split it up into bites, use methods that just init and return nothing and do no if. init is called only once. You can trust it to be the place to create all your widgets.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 06 Dec 2005 20:44 GMT >jButton.setBounds(new java.awt.Rectangle(142,6,104,25)); This is C think. Use layouts instead. See http://mindprod.com/jgloss/layout.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 06 Dec 2005 20:45 GMT >jButton.addPropertyChangeListener(new This is likely an error. In a newbie program almost certainly you mean a simple ActionListener.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 06 Dec 2005 20:36 GMT On Tue, 06 Dec 2005 14:25:51 +0100, Hendrik Maryns <hendrik_maryns@despammed.com> wrote, quoted or indirectly quoted someone who said :
>> public void init() { >> this.setSize(500, 400); [quoted text clipped - 4 lines] >Should work _if you invoke this method_. But it seems like a very >useless method to me. You might consider to make it private. did he not say he was writing an JApplet? You need a public init.
Applets don't decide their size. HTML does.
>> this.setContentPane(getJContentPane()); this should not be necessary unless you are doing something very peculiar.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Hendrik Maryns - 07 Dec 2005 11:29 GMT Roedy Green schreef:
> On Tue, 06 Dec 2005 14:25:51 +0100, Hendrik Maryns > <hendrik_maryns@despammed.com> wrote, quoted or indirectly quoted [quoted text clipped - 10 lines] > > did he not say he was writing an JApplet? You need a public init. As I said, I´ve never writen an applet before, sorry.
> Applets don't decide their size. HTML does. ?
H.
 Signature Hendrik Maryns
================== www.lieverleven.be http://aouw.org
Roedy Green - 06 Dec 2005 20:30 GMT >2. question: I have a calculation.class who does all calculations. And >I want to use methods from this inside my jAppletRobotArm.java to >calculate things. Your Calculate class could be a top level default scope class in the same package as your Applet. Making it a nested class just adds complications you don't want to deal with unless you have to.
It is highly irritating and confusing when someone fail to start class names with Upper case, packages and methods with lower case. It is as disorienting as someone who named the male characters in his novel Beverly, Leslie, and Carroll and the female characters Pat, Chris and Jamie.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
S - 08 Dec 2005 12:35 GMT Thanks for all the advices, will try not to name Bob karen or Karen bob anymore.
Chris Smith - 08 Dec 2005 15:37 GMT > It is highly irritating and confusing when someone fail to start class > names with Upper case, packages and methods with lower case. It is as > disorienting as someone who named the male characters in his novel > Beverly, Leslie, and Carroll and the female characters Pat, Chris and I agree that it's irritating... but I'm confused about something. Pat, Chris, and Jamie are all fairly common female names.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Oliver Wong - 08 Dec 2005 20:46 GMT >> It is highly irritating and confusing when someone fail to start class >> names with Upper case, packages and methods with lower case. It is as [quoted text clipped - 3 lines] > I agree that it's irritating... but I'm confused about something. Pat, > Chris, and Jamie are all fairly common female names. I had assumed that was intentional (because the odds seem low that that was coincidental/accidental), though it does break the symmetry because the male characters have "obviously female" names, as opposed to "typically female, but possibly male" names.
- Oliver
Roedy Green - 09 Dec 2005 00:23 GMT >I agree that it's irritating... but I'm confused about something. Pat, >Chris, and Jamie are all fairly common female names. Mothers give their daughters these names in hopes will help them avoid discrimination. If you chose such names in a novel, it would be because you wanted to deliberately confuse the reader as to genders.
In programming, you want to choose names that are maximally clear and unambiguous.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Chris Smith - 09 Dec 2005 00:52 GMT > Mothers give their daughters these names in hopes will help them > avoid discrimination. If you chose such names in a novel, it would be > because you wanted to deliberately confuse the reader as to genders. When I hear someone referred to as "Jamie", I would be shocked to find that they are male. Obviously, I feel differently about "Chris". ;)
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Roedy Green - 09 Dec 2005 02:02 GMT >When I hear someone referred to as "Jamie", I would be shocked to find >that they are male. I am a big Jamie Lee Curtis fan. Perhaps they should retire names, just like hockey jersey numbers.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|