tags:

views:

45

answers:

1

Hi. 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.

+1  A: 

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

}
akf
thanks a lot buddy
charan
@charan - if this solved your problem, consider marking this answer as accepted
Gnoupi