mercredi 23 avril 2014

multiple keyboard listener possibility


Vote count:

0




I am trying to make use of more than one keyboard listener function in a JApplet program, want to know if this is possible. I have one that allows only numbers to be entered while the other allows some extra characters like plus minus multiply divide etc. I want to choose a particular one depending on the tab user is in. Tried it but the first one that was chosen is always in play even when i call the second... These are my two keylistening functions



private void keyboardSetting1()//disable keyboard1
`{//this deactivates the keyboard except for certain characters
Display.addKeyListener(new KeyAdapter(){
@Override
public void keyTyped(KeyEvent evt){
char c = evt.getKeyChar();
java.awt.event.ActionEvent e = null;//just to be able to call the button function
if (c=='*') {Multiply_BtnActionPerformed(e);evt.consume();} //override * to x
else if (c=='/') {Divide_BtnActionPerformed(e);evt.consume();}//override / to ÷
else if (c=='+'){Plus_BtnActionPerformed(e);evt.consume();}
else if(c=='-'){Minus_BtnActionPerformed(e);evt.consume();}//override +and - buttons also
else if((c>='0')&&(c<='9'));//allow digits
else if(c=='('){Opn_BracketActionPerformed(e);evt.consume();}
else if(c == ')'){Cls_BracketActionPerformed(e);evt.consume();}//override open and close brackets
else if (c=='^'){PowerBtnActionPerformed(e);evt.consume();}//override power button
else evt.consume();//destroy or else
}
});



private void keyboardSetting2()//disable keyboard1
{//this deactivates the keyboard except for certain numbers
Display.addKeyListener(new KeyAdapter(){
@Override
public void keyTyped(KeyEvent evt){
char c = evt.getKeyChar();
java.awt.event.ActionEvent e = null;//just to be able to call the button function

if((c>='0')&&(c<='9'));//allow digits
else evt.consume();//destroy or else
}
});


In activating them I tried this code to choose either :



private void Parent_TabStateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
//this function checks when a the tabbed pane has a new panel selected
Display.setText(null);//clear the Display
if (Parent_Tab.getSelectedComponent()==StandardTab) //for standard panel
{
Btn_DecPnt.setEnabled(true);//enable dec point
keyboardSetting1();//reset the keyboard for this tab specially
}
else if(Parent_Tab.getSelectedComponent()==Base_Converter)
{
keyboardSetting2();
Btn_DecPnt.setEnabled(false);//disable decimal point as not to be used
}


So my first tab is the standard tab so it activates setting1. When I then call on setting two when base converting tab is selected, it doesnt work but setting one still holds.. Is it impossible to use more than one keyboard listener?? thanks



asked 38 secs ago






Aucun commentaire:

Enregistrer un commentaire