Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Mimicking a JButton Click

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mimicking a JButton Click

    Hi,
    I have calss JPButton that extends JButton. It basically is a graphical button that shifts 1 pixel to the right and down when mousePressed(), and shifts back when mouseReleased(). Both of those functions are Overriden obviously. The question I have is that I'm trying to have a keypress mimick the Mouse action. I attempted to do this by Overriding doClick(). Doesn't seem to work. Is there a preferred way to do this?

    public void doButtonDown()
    {
    	setBounds(getBounds().x+1,getBounds().y+1,getWidth(), getHeight());
    	repaint();
    }
     
    public void doButtonUp()
    {
    	setBounds(getBounds().x-1,getBounds().y-1,getWidth(), getHeight());
    	repaint();
    }
     
    @Override
    public void doClick()
    {
    	if (isEnabled())
    	{
    		doButtonDown();
    		try
    		{
    			Thread.sleep(500L);
    		}
    		catch (InterruptedException e)
    		{
    			e.printStackTrace();
    		}
    		doButtonUp();
    	}
    }
     
    @Override
    public void mousePressed(MouseEvent e)
    {
    	if (isEnabled())
    	{
    		doButtonDown();
    	}
    }
     
    @Override
    public void mouseReleased(MouseEvent e)
    {
    	if (isEnabled())
    	{
    		doButtonUp();
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mimicking a JButton Click

    Got it to work by doing the following:

        @Override  
        public void doClick()   
        {   
            if (isEnabled())   
            {   
                setBounds(getBounds().x+1,getBounds().y+1,getWidth(), getHeight());   
                paintImmediately(0,0,getWidth(), getHeight());   
                try {   
                    Thread.currentThread();   
                    Thread.sleep(100);   
                } catch(InterruptedException ie) {   
                }   
                setBounds(getBounds().x-1,getBounds().y-1,getWidth(), getHeight());   
                paintImmediately(0,0,getWidth(), getHeight());   
            }   
        }   
     
        @Override  
        public void mousePressed(MouseEvent e)   
        {   
            if (isEnabled())   
            {   
                setBounds(getBounds().x+1,getBounds().y+1,getWidth(), getHeight());   
                repaint();   
            }   
        }   
     
        @Override  
        public void mouseReleased(MouseEvent e)   
        {   
            if (isEnabled())   
            {   
                setBounds(getBounds().x-1,getBounds().y-1,getWidth(), getHeight());   
                repaint();   
            }   
        }  
    	@Override
    	public void doClick()
    	{
    		if (isEnabled())
    		{
    			setBounds(getBounds().x+1,getBounds().y+1,getWidth(), getHeight());
    	        paintImmediately(0,0,getWidth(), getHeight());
    	        try {
    	            Thread.currentThread();
    				Thread.sleep(100);
    	        } catch(InterruptedException ie) {
    	        }
    			setBounds(getBounds().x-1,getBounds().y-1,getWidth(), getHeight());
    	        paintImmediately(0,0,getWidth(), getHeight());
    		}
    	}
     
    	@Override
    	public void mousePressed(MouseEvent e)
    	{
    		if (isEnabled())
    		{
    			setBounds(getBounds().x+1,getBounds().y+1,getWidth(), getHeight());
    			repaint();
    		}
    	}
     
    	@Override
    	public void mouseReleased(MouseEvent e)
    	{
    		if (isEnabled())
    		{
    			setBounds(getBounds().x-1,getBounds().y-1,getWidth(), getHeight());
    			repaint();
    		}
    	}

Similar Threads

  1. Mimicking a browser sigining into a website
    By JohnBoy in forum Java Networking
    Replies: 0
    Last Post: November 15th, 2012, 09:06 AM
  2. How to close a frame on a button click ??
    By rk0887 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 22nd, 2012, 09:58 AM
  3. Click and increase size
    By rohit_nik in forum AWT / Java Swing
    Replies: 1
    Last Post: April 4th, 2011, 08:38 AM
  4. Waiting for a Second JButton Click
    By Firebert in forum Java Theory & Questions
    Replies: 0
    Last Post: March 16th, 2011, 09:15 PM