I have an assignment where I have two JPanels and I need to be able to push button1 in jpanel1 and get information to change on button2 of jpanel2. I've been working on the problem for a few days and no matter what I try I get stuck.
My idea is that I need a method to convert the button in jpanel2 to a button in jpanel that changes once the button in jpanel1 is pushed. Thing is that I can't figure out how to get a method to work with a button.
Here's what I have so far.
MyJPanel:
public class myJPanel extends JPanel {
public myJPanel(){
super();
JButton j2 = new JButton("..");
setBackground(Color.gray);
setLayout(new BorderLayout());
myJPanel1 p1 = new myJPanel1(JButton(j2)); // thought something
//like this would work to pass jpanel2 to jpanel1 using
//myjpanel as the common class
add(p1,"North");
myJPanel2 p2 = new myJPanel2(j2);
add(p2,"Center");
}
}
panel1
public class myJPanel1 extends JPanel implements ActionListener {
student st1 = new student("Fred","Fonseca",44);
JButton j = new JButton(st1.getInfo());
JButton b1 = new JButton("..");
public myJPanel1(JButton j2) {
super();
setBackground(Color.yellow);
// the whatsUp of this student has to shown in the other panel
j.addActionListener(this);
add(j);
add(b1);
}
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
//=====================================
if (obj == j){
j2.setText(st1.whatsUp()); // Output on JButton in JPanel2
b1.setText(st1.whatsUp());
}
}
}
jpanel2
public class myJPanel2 extends JPanel {
JButton j1 = new JButton("When the user clicks on the button in the UPPER panel" );
public void setButton(JButton j2){
j1 = j2; // shouldn't this pass convert the information between the classes?
}
public myJPanel2(JButton j1){
super();
setBackground(Color.pink);
setLayout(new GridLayout(3,1));
add(j1);
j1.setText("");
}
}