I need help figuring out how to change Button b2 in MyJPanel2. I cannot make any changes to MyJPanel2.
- p1 (the variable representing myJPanel1) needs to manipulate a button located in p2 (the variable representing myJPanel2).
- In order to do this, p1 (the variable representing myJPanel1) needs to receive p2 (the variable representing myJPanel2).
- Which object/class can have access to both myJPanel1 and myJPanel2?
- This will be the best place to send an object (information) to another is using the new statement and sending parameters in.
I have everything working except I do not know how to reach b2 within MyJPanel2 from MyJPanel1.
public class MyJFrame extends JFrame
{
public MyJFrame ()
{
super ("Name - Assignment 05");
MyJPanel mjp = new MyJPanel();
getContentPane().add(mjp,"Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (640, 480);
setVisible(true);
}
}
--------------
public class MyJPanel extends JPanel
{;
public MyJPanel()
{
super();
setBackground(Color.gray);
setLayout(new GridLayout(2, 1));
MyJPanel2 p2 = new MyJPanel2();
MyJPanel1 p1 = new MyJPanel1(p2);
add(p1,"North");
add(p2,"Center");
}
}
----------------
public class MyJPanel1 extends JPanel implements ActionListener
{
JButton jl1, j2, b2;
Student st1;
public MyJPanel1()
{
super();
setBackground(Color.yellow);
st1 = new Student("Bill","Gates",56);
// the whatsUp of this student has to shown in the other panel
jl1 = new JButton(st1.getInfo());
jl1.addActionListener(this);
add(jl1);
b2 = new JButton(st1.whatsUp());
add(b2);
}
//=====================================
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
//=====================================
if (obj == jl1) {
String b = st1.whatsUp();
b2.setText(" " + b + " ");
}
}
}
-----------------
public class MyJPanel2 extends JPanel
{
//==================================================
//no changes allowed in myJPanel2 for assignment 05
//==================================================
JButton b1,b2,b3,b4;
public MyJPanel2()
{
super();
setBackground(Color.pink);
//setLayout(new GridLayout(5,1));
b1 = new JButton("When the user clicks on the button in the UPPER panel" );
add(b1);
b2 = new JButton("Display here whatsUp from the student in UPPER Panel" );
add(b2);
b3 = new JButton("===>>>>You CANNOT create a student here <======" );
add(b3);
b4 = new JButton("It has to be the student from the UPPER Panel" );
add(b4);
}
}