Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make 3 similar focusListeners into 1 ? 1

Status
Not open for further replies.

paulbradley

Programmer
Oct 9, 2002
158
0
0
GB
Hi,

I have 3 very similar FocusListeners and have been trying to find a way of having one and the labels and accesors & mutators accessed chaning depending on who triggered the listener.

There are 3 textFields (tN, tP and tK) and 3 mutators and accessors (getN(), getP() and getK())

The currect code is as follows, how can I have just one of the following and get these little bits of code to relate to where the listener came from, many thanks in advance.


tN.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent fe)
{
try{
if( (Integer.parseInt(tN.getText())>=0)
&& ( (thedata.getP()!=0) || (thedata.getK()!=0)) ){

thedata.setN(Integer.parseInt(tN.getText()));

}else{
throw new IllegalArgumentException();
}

}catch(IllegalArgumentException e){
tN.setText(""+thedata.getN());
}
}

public void focusGained(FocusEvent fe){}

});



tP.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent fe)
{
try{
if( (Integer.parseInt(tP.getText())>=0)
&& ( (thedata.getN()!=0) || (thedata.getK()!=0)) ){

thedata.setP(Integer.parseInt(tP.getText()));

}else{
throw new IllegalArgumentException();
}

}catch(IllegalArgumentException e){
tP.setText(""+thedata.getP());
}
}

public void focusGained(FocusEvent fe){}

});

tK.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent fe)
{
try{
if( (Integer.parseInt(tK.getText())>=0)
&& ( (thedata.getP()!=0) || (thedata.getN()!=0)) ){

thedata.setK(Integer.parseInt(tK.getText()));

}else{
throw new IllegalArgumentException();
}

}catch(IllegalArgumentException e){
tK.setText(""+thedata.getK());
}
}

public void focusGained(FocusEvent fe){}

});
 
Code:
FocusListener f = new FocusListener(){
  ... your code to do something
};
tN.setFocusListener(f);
tPsetFocusListener(f);
tKsetFocusListener(f);


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
That won't work i'm afraid as the code is slightly different in each listener.

Any other ideas, thanks.
 
Yes, but you can interrogate the FocusEvent, find which component generated the event, and perform different logic depending. All within the one bit of code.

Code:
FocusListener f = new FocusListener(){
  public void focusGainded(FocusEvent fe){
    Component c = fe.getComponent();
    //check which component we've got in c and do
    //different things depending.
  }
};
tN.setFocusListener(f);
tPsetFocusListener(f);
tKsetFocusListener(f);

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Create you own FocusListener, something likes:
Code:
public class MyFocusListener extends FocusListener(){
   private JTextField f1, f2, f3;
   public MyFocusListener(JTextField f1, JTextField f2, JTextField f3,){
      this.f1 = f1;
      this.f2 = f2;
      this.f3 = f3;
   }

   publicvoid focusLost{FocusEvent fe}{
      try{
       if( (Integer.parseInt(f1.getText())>=0)
          && ( (thedata.f2()!=0) ||   (thedata.f3()!=0)) ){
            thedata.setK(Integer.parseInt(f1.getText()));
        }else{
           throw new IllegalArgumentException();
        }
       }catch(IllegalArgumentException e){
              f1.setText(""+thedata.f1.getF1());
       }

   }

When you call it,
tk.setFocusListener(New MyfocusListener(tk, tP, tn));
tn.setFocusListener(New MyfocusListener(tn, tk, tp));
tp.setFocusListener(New MyfocusListener(tp, tn, tk));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top