This is a bit of a hack, and I'd think twice before using this in a serious 
project, but as an exercise, this was quite amusing... Here you go...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel
{
    public static void main( String[] args )
    {
       JFrame f = new JFrame("test");
       f.setContentPane(new test());
       f.pack();
       f.show();
    }

    public test()
    {
       JToggleButton tb = new SmartToggleButton("Test");
       setLayout( new BorderLayout() );
       add( tb, BorderLayout.CENTER );
    }

    static class SmartToggleButton extends JToggleButton
    {
       public SmartToggleButton( String text )
       {
          super(text);
          setModel( new SmartToggleButtonModel() );
          addMouseListener( new MouseAdapter() {
                public void mouseClicked( MouseEvent ev )
                {
                   if( ev.getClickCount() == 2 )
                      ((SmartToggleButtonModel)getModel()).toggleSelection();
                }

             });
       }
    }

    static class SmartToggleButtonModel extends JToggleButton.ToggleButtonModel
    {
     public void setPressed(boolean b) {
         //
         // copied from DefaultButtonModel.java
         //
         if((isPressed() == b) || !isEnabled()) {
             return;
         }

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

         if(!isPressed() && isArmed()) {
             fireActionPerformed(
                 new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                                 getActionCommand())
                 );
         }

         fireStateChanged();
     }

     public void toggleSelection()
     {
       if (isArmed()) {
           setSelected(!this.isSelected());
       }
     }

    }
}


At 01:02 PM 2/15/2001, Perico wrote:
>I want to implement a JButton that behaves like a JToggleButton when there
>is a double click above it.
>In other words, I'd like the JButton to mantain its pressed state when it
>receives a double click (like a JToggleButton), and not to mantain it (like
>a JButton) when is single clicked.
>
>I don't mind if I get this behaviour using a JToggleButton with some extra
>code, or using a JButton with some extra code.
>
>However, I only can capture ActionEvent's produced above a JButton or
>JToggleButton.  I need to detect if the ActionEvent was due to a single
>click or a double click.
>
>Do you know how can I solve my problem?.
>
>Please, if you have some idea about this issue or you know where can I get
>related information, tell me.

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to