[英]How can I disable the mouse click on the button action event?
Is there a way I can disable mouse click ?有没有办法可以禁用鼠标单击? In the panel there are different components and for some of the Button Click events, I want to disable the mouse click.
在面板中有不同的组件,对于某些按钮单击事件,我想禁用鼠标单击。 I mean the click of the mouse doesn't have any effect on the components.
我的意思是单击鼠标对组件没有任何影响。 I can disable using the
setEnabled()
function but I don't want to do that way.我可以禁用使用
setEnabled()
函数,但我不想这样做。
Is there any way I can disable the mouse click ?有什么办法可以禁用鼠标单击吗?
Situation :情况 :
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//..disable the mouse click on each component present inside the panel
}
You can add an extended ActionListener to all the buttons like this:您可以向所有按钮添加扩展的 ActionListener,如下所示:
public abstract class ExtendedActionListener implements ActionListener{
private static boolean disabled = false;
public static void setDisabled(boolean disabled){
ExtendedActionListener.disabled = disabled;
}
@Override
public final void actionPerformed(ActionEvent e){
if(disabled)
return;
doSomething;
}
}
And now just disable all the ActionListeners by calling the method setDisabled(false)
.现在只需通过调用方法
setDisabled(false)
禁用所有 ActionListeners 。 The Button visual behavior doesn't change at all, but nothing happens, when you click on it. Button 视觉行为根本没有改变,但当您单击它时,什么也没有发生。
If the visual click behaviour doesn't matter, then you can just remove the MouseListeners.如果视觉点击行为无关紧要,那么您可以删除 MouseListeners。
You can create a button group like this:您可以像这样创建一个按钮组:
public class SingleSelectionButtonGroup {
private final List<JButton> buttons;
public static SingleSelectionButtonGroup group(List<JButton> buttons) {
return new SingleSelectionButtonGroup(buttons);
}
public static SingleSelectionButtonGroup group(JButton...buttons) {
return new SingleSelectionButtonGroup(Arrays.asList(buttons));
}
private SingleSelectionButtonGroup(List<JButton> buttons) {
this.buttons = new ArrayList<JButton>(buttons);
setupListener();
}
private void setupListener() {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
SingleSelectionButtonGroup.this.disableAllExcept((JButton) e.getSource());
}
};
for (JButton button : buttons) {
button.addActionListener(listener);
}
}
private void disableAllExcept(JButton clickedButton) {
for (JButton button : buttons) {
if (!clickedButton.equals(button)) {
button.setEnabled(false);
}
}
}
}
And then uses it with a collection of buttons that you want to group:然后将它与要分组的按钮集合一起使用:
public class Application {
public void run() {
final JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 300));
final JPanel pane = new JPanel();
List<JButton> buttons = new ArrayList<JButton>();
String[] texts = {"A", "B", "C"};
for (String text : texts) {
JButton button = new JButton(text);
buttons.add(button);
pane.add(button);
}
SingleSelectionButtonGroup.group(buttons);
frame.getContentPane().add(pane);
frame.setVisible(true);
}
public static void main(String[] args) {
new Application().run();
}
}
you should use one common class of your listener and have static method for turn listener turn on and off你应该使用你的监听器的一个公共类,并有静态方法来打开和关闭监听器
public abstract class BaseMouseListener implements ActionListener{
private static boolean active = true;
public static void setActive(boolean active){
BaseMouseListener.active = active;
}
protected abstract void doPerformAction(ActionEvent e);
@Override
public final void actionPerformed(ActionEvent e){
if(active){
doPerformAction(e);
}
}
}
your listeners would have to implements doPerformedAction()
你的听众必须实现
doPerformedAction()
Add empty mouse listener.添加空鼠标侦听器。 This will "disable" the click because it will not have any effect.
这将“禁用”点击,因为它不会产生任何效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.