Oliver Bak
Oliver Bak

Reputation: 161

Need help to link classes

As the title suggests, I need help to link classes. I got one main class, and 4 classes which are going to pass to each other!

My code:

Mainclass: run all the 4 classes

public class main {
    public static void main(String[] args){ 
        class4 c4 = new class4();
        class3 c3 = new class3(c4);
        class2 c2 = new class2(c3);

        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);
    }
}

class1: pass a String to a method in class2.

public class class1 extends JFrame{
    private JButton jb;

    private class4 c4;
    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);
    }

    public void recieveHi(String x){
        System.out.println(x);
    }

}

class2: Output String from class1, and pass a String to class3

public class class2 extends JFrame{
    private JTextField jt;

    private class3 c3;

    public class2(class3 c3){
        this();
        this.c3 = c3;
    }

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

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

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

    public void sayHi(){
        c3.recieveHi("Hey class3");
    }
}

class3: Output String from class2, and pass a String to class4

public class class3 {
    Scanner scan = new Scanner(System.in);

    private class4 c4;
    public class3(class4 c4){
        //this();
        this.c4 = c4;
        }

    public void recieveHi(String x){
        System.out.println(x);
        sayHi();

        //sayHi();
    }
    public void sayHi(){
        System.out.println();
        c4.recieveHi("Hey class4");
    }
}

class4: Output String from class3, and are supposed to send a String back to class1. But I can't make it work.

A problem is that I can't link them in mainclass, because this: class4 c4 = new class4(c1); doesn't work, c1 isn't simply made when class4 needs it, so that's not an option.

    public class class4 {
    private class1 c1;

    //this are supposed to work, but doesnt...
    public void setClass1Object(class1 pC1) {
        this.c1 = pC1;
    }

    public void recieveHi(String x){
        System.out.println(x);
        killMessenger();
        setClass1Object(c1);
    }

    public void killMessenger(){
        String s = "back to class 1";
        c1.recieveHi(s);
    }
}

To simple that, all I need is to being able to send a String back to class1. I need it for a bigger project, and all the classes have to get opened from the mainclass.

Upvotes: 0

Views: 165

Answers (1)

stefan.itu
stefan.itu

Reputation: 41

I think if you should first initialize all the classes and afterwards to set them visible. If you do like this probably it will work:

public static void main(String[] args){ 
  class4 c4 = new class4();
  class3 c3 = new class3(c4);
  class2 c2 = new class2(c3);
  class1 c1 = new class1(c2);
  c4.setClass1Object(c1);
  // here make all the setters like setSize, setVisible etc
}

Upvotes: 0

Related Questions