Results 1 to 9 of 9
Thread: Calling an Object
- 08-09-2010, 09:54 PM #1
Member
- Join Date
- Mar 2010
- Location
- Hilton Head, SC
- Posts
- 34
- Rep Power
- 0
- 08-09-2010, 09:58 PM #2
Be more specific.
Does A have methods? Do you want to call a constructor?
Something like that?Java Code:public class B { public B() { A a = new A(); // calls constructor a.someMethod(); // calls method "someMethod" } }
-
Do you mean "class" A and B, not object? If so, then
Java Code:class A { public void someMethodOfA() { System.out.println("in someMethodOfA"); } } class B { private A aObject = new A(); public void someMethod() { aObject.someMethodOfA(); } }
If this is not what you meant, then you will need to clarify your question a lot.
- 08-09-2010, 10:53 PM #4
Member
- Join Date
- Mar 2010
- Location
- Hilton Head, SC
- Posts
- 34
- Rep Power
- 0
Let me give a more detailed exmaple:
The code gives me the error:Java Code:public class RSS { public static void main(String[] args) { Frame frame = new Frame(); frame.setDeafultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); } } class Frame extends JFrame { public Frame() { setBounds(/* location, size */); setTitle("Some Text"); Container contentPane = getContentPane(); JPanel panel = new Panel(); contentPane.add(panel); panel.setLayout(null); } } class Panel extends JPanel { public Panel() { Menu menu = new Menu(); add(menu); text = new JTextField(); text.setBounds(/* Size it, position it */); add(text); } public String getText() { return text.getText(); } private JTextField text; } class Menu extends JMenuBar { public Menu() { setBounds(/* locate, size it */); JMenu file = new JMenu("File"); JMenuItem item = new JMenuItem("Do Stuff"); file.add(item); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String str = panel.getText(); } }); } }
RSS.java:53: cannot find symbol
symbol: variable panel
String str = panel.getText();
^
1 error
I know I can do it statically by calling Panel.getText(); and making text a static JTextField, but is there a way to do it they way I have above?
-
Pass a reference of the Panel object into Menu via a constructor parameter, and call the method off of that.
e.g.,
Java Code:class Panel extends JPanel { public Panel() { Menu menu = new Menu(this); //!! *** note change **** add(menu); // .... code deleted } //... } class Menu extends JMenuBar { Panel myPanel; public Menu(Panel p) { myPanel = p; // get your reference and assign it. // .... item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // !! String str = panel.getText(); String str = myPanel.getText(); } }); } }Last edited by Fubarable; 08-09-2010 at 11:05 PM.
- 08-09-2010, 11:50 PM #6
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
make myPanel static. Then it should work
-
To the original poster, please do not follow this route as it will destroy any object-oriented abilities of your program.
To crikey: Please stop giving out bad advice (as here secondary-jframe). This is worse than giving no advice at all. And don't get me wrong, I'm not saying I'm perfect, far from it, but at least I state when I don't know what the heck I'm talking about or when I'm guessing.Last edited by Fubarable; 08-10-2010 at 12:16 AM.
- 08-10-2010, 01:38 AM #8
Member
- Join Date
- Mar 2010
- Location
- Hilton Head, SC
- Posts
- 34
- Rep Power
- 0
Passing references! I do that all the time with event delegation in jQuery yet I never realize it is the same concept. Thank you so much!
- 08-10-2010, 01:49 AM #9
Similar Threads
-
same object variable t and same methods is calling
By javastuden in forum New To JavaReplies: 1Last Post: 11-24-2009, 04:10 AM -
Calling Object.method
By Fedor in forum New To JavaReplies: 1Last Post: 04-11-2009, 01:44 PM -
'Class' Object and calling Static Methods?
By mikeiz404 in forum Advanced JavaReplies: 3Last Post: 01-24-2009, 12:58 PM -
Calling a method for all instances of an object
By rattle in forum New To JavaReplies: 4Last Post: 04-30-2008, 02:10 PM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks