Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-03-2007, 03:48 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Calling a method in another class
This must be a classic 'if I had a dollar' kind of question, but I'm confused about calling methods in other classes. The problem snippet is:

Code:
Card card = new Card(); card.Card();
- which to the best of my knowledge should work, but doesn't. What I want to do, for the purpose of experimenting with CardLayout, is initiate the Card Layout tutorial (in the class Card) from another class by calling the method Card();. Here's the whole thing:

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Menu { public static void main(String[] args) { JFrame frame = new JFrame("Menu"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize(1200,800); frame.setVisible(true); } Card card = new Card(); card.main(); } class Card extends JPanel { CardLayout cards = new CardLayout(); public Card() { setLayout(cards); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { cards.next(Card.this); } }; JButton button; button = new JButton("one"); button.addActionListener(listener); add(button, "one"); button = new JButton("two"); button.addActionListener(listener); add(button, "two"); button = new JButton("three"); button.addActionListener(listener); add(button, "three"); } public static void main(String[] args) { JFrame frame = new JFrame("Card"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize(200, 200); frame.setLocation(200, 200); frame.setContentPane(new Card()); frame.setVisible(true); } }

Last edited by levent : 08-03-2007 at 04:16 PM. Reason: Code placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-03-2007, 04:14 PM
Member
 
Join Date: Jul 2007
Posts: 41
gabriel is on a distinguished road
1) are both classes in the same package?
2) you have to write
Code:
Card card = new Card(); card.main();
in the main of the menu class
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-03-2007, 04:19 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Hi uncopywritable,

Please use [code] tag to wrap your codes. It makes the code much more readable and it is very easy to use!

And i did not understand what you really want. As far as i see your card class does not have a Card() method.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-03-2007, 05:33 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Re: Calling a method in another class
First off, gabriel - I tried your suggestion and it works if I declare the main method of Card:

Code:
public static void main() { }
instead of:

Code:
public static void main(String[] args) { }
writing

Code:
Card card = new Card(); card.main(String[] args);
in the main of Menu doesn't work! It would be great if I could activate methods regardless of the argument they take. Is there a way to do this?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-03-2007, 05:38 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Sorry levent, I just didn't realise about the tags, my browser seems to find the code and tag it anyway, which is odd.

The method Card() is in the class Card and goes like this:

Code:
public Card() { setLayout(cards); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { cards.next(Card.this); } }; JButton button; button = new JButton("one"); button.addActionListener(listener); add(button, "one"); button = new JButton("two"); button.addActionListener(listener); add(button, "two"); button = new JButton("three"); button.addActionListener(listener); add(button, "three"); }
All my attempts to call this method have failed.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-03-2007, 05:53 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Quote:
All my attempts to call this method have failed.
But it is constructor, not a method. You should at least add void keyword in front of the method name to make it a method.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-03-2007, 06:01 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Ah. Right. I was confused between the class declaration and the constructor.

Thanks for pointing that out!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-14-2008, 01:10 PM
Member
 
Join Date: Jan 2008
Posts: 1
anujraj92 has a little shameless behaviour in the past
Hello
My self anuj , I will see how I am on this forum
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-14-2008, 05:37 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Welcome anuj. Please do not revive old threads unless you have something substantial to add, notifying everyone that you're on this forum by posting to an old thread disqualifies as a substantial attribute. Can I have my minute and fifteen seconds back please?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic method calling Java Tip Java Tips 0 02-15-2008 09:46 AM
method calling? frejon26 New To Java 4 01-25-2008 04:38 AM
Calling method from another class asahli New To Java 1 12-15-2007 07:24 PM
Help with Calling a method Albert New To Java 3 07-10-2007 04:27 PM


All times are GMT +3. The time now is 12:39 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org