Oliver Bak
Oliver Bak

Reputation: 151

Need help to run method from running class

I need help to pass a String from a running class, to another running class. I made a little example, which should be able to explain my problem a little further.

Main class; runs class 1 and 2.

public class main {
    public static void main(String[] args){
        class2 c2 = new class2();
        c2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c2.setSize(200,100);
        c2.setLocationRelativeTo(null);
        c2.setVisible(true);
        class1 c1 = new class1(c2);
        c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c1.setSize(200,100);
        c1.setLocationRelativeTo(null);
        c1.setVisible(true);
        ;

    }
}

Class 1; wanna say Hi to class 2.

public class class1 extends JFrame{
    private JButton jb;

    private class2 c2;
    public class1(class2 c2){
        this();
        this.c2 = c2;
        }


    public class1(){
        super("");
        setLayout(new FlowLayout());

        jb = new JButton("click click");
        add(jb);

        jb.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        sayHi("Hi buddy");
                    }});
    }
    public void sayHi(String x){
        c2.recieveHi(x);
    }

}

Class 2: Wants to recieve a Hi.. But never got any...

public class class2 extends JFrame{

    private JTextField jt;

    public class2(){
        super("Yeds");
        setLayout(new FlowLayout());

        jt = new JTextField();
        add(jt);
        //recieveHi("hey");
    }

    public void recieveHi(String x){
        String j = x;
        jt.setText(j);
    }
}

I would appreciate your help. I just need to run a method from an already running class. I need it for a bigger program.

Upvotes: 0

Views: 52

Answers (2)

java seeker
java seeker

Reputation: 1266

you are not passing object of C2 class to constructor of C1 class.

as you are not passing object of c2 class you should get a NullPointerException when you call sayHi(...)

please look at rearranged code block

public static void main(String[] args){

    class2 c2 = new class2();
    c2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c2.setSize(200,100);
    c2.setLocationRelativeTo(null);
    c2.setVisible(true);

    class1 c1 = new class1(c2);
    c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c1.setSize(200,100);
    c1.setLocationRelativeTo(null);
    c1.setVisible(true);


}

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

You have a constructor that hava as parameter a class2 object, so why don't you use it? In the main() method:

class2 c2 = new class2();
class1 c1 = new class1(c2); // Use constructor with 'class2' parameter
...

You could also create a method to set the 'class2 c2' instance in class1:

public void setClass2Object(class2 pC2) {
    this.c2 = pC2;
}

Edit:

As @vandale commented, you may want to call this() in the constructor with 1 parameter, so it get initialized correctly:

public class1(class2 c2) {
    this();
    this.c2 = c2;
}

Upvotes: 3

Related Questions