JButton:模拟 JButton 按下而不触发动作 [英] JButton: Simulate JButton press without the action firing

查看:43
本文介绍了JButton:模拟 JButton 按下而不触发动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是我有一些 JButton 可以向 ActionListener 触发操作.我还使用击键向 AcionListener 发出一些相同的操作命令.当击键快捷方式触发也由其中一个按钮完成的操作时,我希望该按钮看起来好像被按下但不触发事件.

My use case is I have some JButtons that fire actions to an ActionListener. I also use keystrokes to fire some of the same action commands to the AcionListener. When the keystroke shortcut fires an action that is also done by one of the buttons I want the button to look as though it was pressed but not to fire the event.

所以我研究了 AbstractButton API 并尝试了其中的一些方法,例如 setSelected,但没有达到预期的效果.最后,我查看了 doCLick 方法,看看我是否可以使用删除动作触发部分,但这也不起作用

So I have looked into the AbstractButton API and I tried some of the methods there like setSelected but it didn't have the desired effect. Finally I have looked at the method doCLick to see if I could use remove the action firing part but this also doesn't work

 367       public void doClick(int pressTime) {
 368           Dimension size = getSize();
 369           model.setArmed(true);
 370           model.setPressed(true);
 371           paintImmediately(new Rectangle(0,0, size.width, size.height));
 372           try {
 373               Thread.currentThread().sleep(pressTime);
 374           } catch(InterruptedException ie) {
 375           }
 376           model.setPressed(false);
 377           model.setArmed(false);
 378       }

我曾想过删除所有听众.运行 doClick 然后再次添加它们,但我认为应该有更优雅的东西.

I had thought of removing all the listeners. Running the doClick and then adding them again but I thought something more elegant should be available.

SSCE 将是

public class Test {

public static void main(String[] args) throws InterruptedException{
    JFrame jf = new JFrame();
    jf.setLocationRelativeTo(null);
    JButton jb = new JButton("Test Button");
    jb.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("I don't want this to fire");
        }
    });
    jf.getContentPane().add(jb);
    jf.pack();
    jf.setVisible(true);

    Thread.sleep(1000);
    clickWithoutFiringAction(jb);
}

public static void clickWithoutFiringAction(JButton button){
    Dimension size = button.getSize();
    ButtonModel model = button.getModel();
            //I tried changing these combinations but I could not get the desired effect
    model.setArmed(true);
    model.setPressed(true);
    button.paintImmediately(new Rectangle(0,0, size.width, size.height));
    try {
        Thread.sleep(68);
    } catch(InterruptedException ie) {
    }
    model.setPressed(false);
    model.setArmed(false);
 }
}

推荐答案

JButton 使用具有 setPressed(boolean) 函数生成的 DefaultButtonModel使用 fireActionPerformed(ActionEvent e) 函数执行的动作事件.您将需要扩展此模型并为 setPressed(boolean b) 函数提供自定义实现,以避免触发动作事件.详情请参考该模型类的源代码.

JButton uses DefaultButtonModel which has setPressed(boolean) function for generating action performed event using fireActionPerformed(ActionEvent e) function. You will need to extend this model and provide custom implementation for setPressed(boolean b) function for avoiding action event firing. Please refer to the source code of this model class for more details.

class CustomModel extends DefaultButtonModel
{


    @Override
    public void setPressed(boolean b){
        if((isPressed() == b) || !isEnabled()) {
            return;
        }

        if (b) {
            stateMask |= PRESSED;
        } else {
            stateMask &= ~PRESSED;
        }

        fireStateChanged();
    }

}

现在,您可以设置模型:jButton.setModel(new CustomModel());

now, you can set the model: jButton.setModel(new CustomModel());

这篇关于JButton:模拟 JButton 按下而不触发动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆