Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default 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:

    Java 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:

    Java 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.

  2. #2
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    1) are both classes in the same package?
    2) you have to write
    Java Code:
    Card card = new Card();
    card.main();
    in the main of the menu class

  3. #3
    levent Guest

    Default

    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.

  4. #4
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default 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:

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

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

    Java 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?

  5. #5
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default

    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:

    Java 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.

  6. #6
    levent Guest

    Default

    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.

  7. #7
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default

    Ah. Right. I was confused between the class declaration and the constructor.

    Thanks for pointing that out! :)

  8. #8
    anujraj92 is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default Hello

    My self anuj , I will see how I am on this forum

  9. #9
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  10. #10
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Calling a method in another class

    Thanks for your answer

    I'm trying to instance but i have an error too

    Java Code:
    public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;
            Aluno t= new Aluno(); // found no arguments ---
            t.getID();
            for(int i=0; i<alunos.size(); i++)
            {
          
           
           }
           alunos.add(aluno);
           return true;
        }
    After instance i think expect something, iv'e tried to put (aluno, id) and i have another error saying can't find symbol "id"

    I'm lost...

Similar Threads

  1. Dynamic method calling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:46 AM
  2. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 04:38 AM
  3. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 07:24 PM
  4. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 04:27 PM

Posting Permissions

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