Home > Back-end >  JTogelButton triggered ChangeListeter when the mouse enter and leave?
JTogelButton triggered ChangeListeter when the mouse enter and leave?

Time:03-04

Create code:
 
Private void addToggleButton (JToolBar JToolBar, Element tool) {
JToggleButton togBtn=new JToggleButton ();
TogBtn. Elegantly-named setName (tool. AttributeValue (Widgets. Key_tool_attr_name));
TogBtn. SetText (tool. AttributeValue (Widgets. Key_tool_attr_text));
TogBtn. SetActionCommand (tool. AttributeValue (Widgets. Key_tool_attr_cmd));
//togBtn. SetIcon (new ImageIcon (tool. AttributeValue (Widgets. Key_tool_attr_icon)));
Int w=Integer. ParseInt (tool. AttributeValue (Widgets. Key_tool_attr_width));
Int h=Integer. ParseInt (tool. AttributeValue (Widgets. Key_tool_attr_height));
Dimension size=new Dimension (w, h);
TogBtn. SetSize (size);
TogBtn. SetPreferredSize (size);
TogBtn. SetBackground (RdrmColor. ParseToColor (tool. AttributeValue (Widgets. Key_tool_attr_bcolor)));
TogBtn. SetForeground (RdrmColor. ParseToColor (tool. AttributeValue (Widgets. Key_tool_attr_fcolor)));
Boolean enabled=!" False ". The equals (tool. AttributeValue (Widgets. Key_tool_attr_enabled))? True, false;
TogBtn. SetEnabled (enabled);
TogBtn. SetBorder (null);
//what kind listener JToggleButton
TogBtn. AddChangeListener (changelistener);
Jtoolbar. Add (togBtn);
}



Listener code:
 
Public void createActionListener () {
Applistener=new ActionListener () {
@ Override
Public void actionPerformed (an ActionEvent e) {
RequestExecute (e);
}
};

Changelistener=new changelistener () {
@ Override
Public void stateChanged (ChangeEvent e) {
JToggleButton toggleBtn=(JToggleButton) um participant etSource ();
System. The out. Println (toggleBtn. GetText () + "if selected:" + toggleBtn. An isSelected ());
//setMute (JTB. An isSelected ());
//System. Out. Println (isMute);
}
};
ItemListener=new itemListener () {
@ Override
Public void itemStateChanged (ItemEvent e) {
If (um participant etStateChange ()==ItemEvent. SELECTED) {
//when JRadioButton is selected, change the value of the variable engine_selected
JRadioButton rBtn=(JRadioButton) um participant etSource ();
Engine_selected=rBtn. GetName (). The trim ();
The switch (engine_selected) {
Case engine_selected_checktext:
System. The out. Println (((JRadioButton) um participant etSource ()). The getActionCommand ());
break;
Case engine_selected_offline:
System. The out. Println (((JRadioButton) um participant etSource ()). The getActionCommand ());
break;
Case engine_selected_online:
System. The out. Println (((JRadioButton) um participant etSource ()). The getActionCommand ());
break;
Case engine_selected_websocket:
System. The out. Println (((JRadioButton) um participant etSource ()). The getActionCommand ());
break;
}
}
}
};
}



After execution, in the mouse, click, triggered when they leave 7 times:


I just want to in the state of the mouse is triggered when a, how to do?

CodePudding user response:

Actually, want to trigger an event when JTogelButton to select/deselect,
In most cases use ActionListener is enough, and whether the mouse to click or press the space change state of the button, will trigger
Only a handful of cases, such as: "where else would you call directly by code setSelected (true/false) change button state,
Only in that case button state changes, not trigger ActionListener
If you have such a demand, can use ItemListener rather than ChangeListeter
ChangeListeter is not really a very friendly event listeners, seldom go to with it

 
BTN. AddItemListener (new ItemListener () {
@ Override
Public void itemStateChanged (ItemEvent e) {
Int stateChange=um participant etStateChange ();
If (stateChange==ItemEvent. SELECTED) {//SELECTED
System. The out. Println (true);
} else if (stateChange==ItemEvent. DESELECTED) {//uncheck
System. The out. Println (false);
}
}
});

CodePudding user response:

reference 1st floor sunyiz response:
, in fact, to trigger an event when JTogelButton to select/deselect,
In most cases use ActionListener is enough, and whether the mouse to click or press the space change state of the button, will trigger
Only a handful of cases, such as: "where else would you call directly by code setSelected (true/false) change button state,
Only in that case button state changes, not trigger ActionListener
If you have such a demand, can use ItemListener rather than ChangeListeter
ChangeListeter is not really a very friendly event listeners, seldom go to with it

 
BTN. AddItemListener (new ItemListener () {
@ Override
Public void itemStateChanged (ItemEvent e) {
Int stateChange=um participant etStateChange ();
If (stateChange==ItemEvent. SELECTED) {//SELECTED
System. The out. Println (true);
} else if (stateChange==ItemEvent. DESELECTED) {//uncheck
System. The out. Println (false);
}
}
});

I also feel this is a problem of listeners, who has examined the source code, there is only one method, there is little that can judge through ChangeEvent e
  • Related