jcom
jcom

Reputation: 109

(Java) Difficulty in classes

enter image description here

Based on the diagram above; I would like Frame1 to show Frame2 using New(Jbutton) and Frame3 show using Show(JButton). Frame3 has this default "Hello World"(JtextField) and I would like to make this empty using the Yes(JButton) from Frame2.

The problem is that I don't know the code for Frame2 and how to empty the textfield from frame3.

Here is my code so far:

Frame1.java

public class Frame1 extends JFrame implements ActionListener{

JButton b1 = new JButton("New");
JButton b2 = new JButton("Show");

Frame2 f2 = new Frame2();
Frame3 f3 = new Frame3();

public Frame1(){
    setLayout(new FlowLayout());
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,300);
    add(b1);
    add(b2);
    b1.addActionListener(this);
    b2.addActionListener(this);
}

public static void main(String args[]){
    new Frame1();
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b1){
        f2.setVisible(true);
    }
    else{
        f3.setVisible(true);
    }
}
}

Frame2.java

public class Frame2 extends JFrame implements ActionListener{

JButton b1 = new JButton("Yes");
JButton b2 = new JButton("No");

public Frame2(){
    setLayout(new FlowLayout());
    setVisible(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,100);

    add(b1);
    add(b2);
}
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b1){

    }else{

    }
}
}

Frame3.java

public class Frame3 extends JFrame{

JTextField t1 = new JTextField("Hello WOrld");

public Frame3(){
    setLayout(new FlowLayout());
    setVisible(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200,200);

    add(t1);

}

}

Upvotes: 1

Views: 198

Answers (2)

talnicolas
talnicolas

Reputation: 14053

You could just just pass a reference to the frame3 in your frame2 constructor, and then when you click on the Yes button, clear the JTextField of your frame3.

EDIT

Well when you declare your frames, you could create your frame3 before and pass it to the frame2 constructor:

Frame3 f3 = new Frame3();
Frame2 f2 = new Frame2(f3);

and in your frame2

Frame refToFrame3;
...
public Frame2(Frame f){
   ...
   refToFrame3 = f;
   ...
...

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b1){
        refToFrame3.clearText()
        ...

and then in your frame3 you create a clearText method that will clear the text in your JTextField.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Some recommendations:

  • Frame2 shouldn't be a JFrame but rather a modal JDialog as it is dependent on Frame1 and should prevent user interaction with Frame1 until Frame2 has been dealt with. A JOptionPane would work great here.
  • Frame1 should get the result from Frame2, and then Frame1 will use this to decide whether to create a new Frame3 (which also should probably be a dialog).

Upvotes: 1

Related Questions