简体   繁体   English

将MouseListener添加到JCheckBox

[英]Add MouseListener to JCheckBox

I wrote this code for alert message shown to user when they uncheck the checkbox. 当我们取消选中复选框时,我为用户显示了此代码。 It only woks when I mouse key is realized with in the checkbox. 当我在复选框中实现鼠标键时,它才会响起。 If user click the checkbox and release out of the checkbox it allow user to uncheck the checkbox and doesn't shows alert message. 如果用户单击复选框并从复选框中释放,则允许用户取消选中该复选框,并且不显示警报消息。 How can I solve this bug? 我该如何解决这个bug?

public void mouseClicked(MouseEvent e) {        
    Vector matNoVect = new Vector();
    if (e.getClickCount() == 1) {
        Utools.setMouseBusy(sstEndProductMaterials.table);
        try {   
            Vector v = new Vector();
            v = Inter.LoadContents(str);
            System.out.println(v);
            if (v.size() > 0) {
                if (Integer.parseInt(v.get(0).toString()) > 0) {
                    JOptionPane.showMessageDialog(null,"Material "+matNo+" is used in some Item");
                    sstEndProductMaterials.table.setValueAt(new Boolean(true),index, 7);
                    Utools.setMouseNormal(sstEndProductMaterials.table);
                    return;
                }
            }    
        } catch (Exception e11) {
            e11.printStackTrace();
            Utools.setMouseNormal(sstEndProductMaterials.table);
        }
    }
}

Use an ItemListener instead of a MouseListener 使用ItemListener而不是MouseListener

http://download.oracle.com/javase/tutorial/uiswing/components/button.html#checkbox http://download.oracle.com/javase/tutorial/uiswing/components/button.html#checkbox

You can then do this inside the ItemListener: 然后,您可以在ItemListener中执行此操作:

public void itemStateChanged(ItemEvent event) {
    if (event.getStateChange() == ItemEvent.DESELECTED) {
        //Code to show alert etc.
    }
}
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

/** Some users are NOT addicted to the mouse!  Which is why
it would be better to add an ActionListener to a JCheckBox. */
class CheckBoxTest {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JPanel p = new JPanel(new GridLayout(0,1,20,20));

                JCheckBox cb1 = new JCheckBox("Broken on keyboard");
                cb1.addMouseListener( new MouseAdapter(){
                    @Override
                    public void mouseClicked(MouseEvent me) {
                        System.out.println("Mouse click");
                    }
                } );
                p.add( cb1 );

                JCheckBox cb2 = new JCheckBox("Works for keyboard or mouse!");
                cb2.addActionListener( new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        System.out.println("Event detected!");
                    }
                } );
                p.add( cb2 );

                JOptionPane.showMessageDialog(null, p);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM