Reputation: 73
I had employ name Text Field in 1st panel in D module. when i click generate button the employ name automatically update in display panel Employe Name Textfield in E module. so in both the panels the value must be same. how can i get the value from D module and update in E module by using Java Swing.
Upvotes: 0
Views: 432
Reputation: 39495
Swing relies heavily on the Obeserver pattern. You can use this pattern to help your E module know when the generate button is clicked.
If your E module has a reference to your D module, you can add E as an ActionListener
to the generate button. You can then pull the text from the D module when the action is fired. A brute-force approach is mapped out below:
public class DModule {
private JButton genButton = new JButton("generate");
private JTextField empNameTF = new JTextField();
// ---more code ---
public void addGenButtonListener (ActionListener l) {
genButton.addActionListener(l);
}
public String getEmpName() {
return empNameTF.getText();
}
}
public class EModule implements ActionListener {
DModule d = null;
JTextField myEmpNameTF = new JTextField();
public EModule (DModule d) {
this.d = d;
d.addGenButtonListener(this);
}
// --- more code ---
public void actionPerformed(ActionEvent event) {
myEmpNameTF.setText(d.getEmpName());
}
}
Upvotes: 1