Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By doWhile

Thread: Setting JButton's mousedown state

  1. #1
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Setting JButton's mousedown state

    Hello!

    I was just wondering if anyone knew how to remove a JButton's mousedown state. I'm creating a custom button with a triangular shape and when I click the button a white background appears behind the transparent parts of the image.

    Below you can find both an image of the issue as well as the source code for my custom button class:

    Setting JButton's mousedown state-buttonstates.png

    Java Code:
    import java.awt.Color;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
    public class MenuButton extends JButton{
    
    	private static final long serialVersionUID = 1L;
    	
    	public MenuButton(String path){
    		
    		super.setBackground(new Color(0, 0, 0, 0));
    		super.setBorder(null);
    		
    		ImageIcon ii = new ImageIcon(this.getClass().getResource(path));
    		super.setIcon(ii);
    		
    	}
    	
    }
    Last edited by 0026sd; 12-14-2011 at 02:40 PM. Reason: Spelling errors

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Setting JButton's mousedown state

    You can use the setPressedIcon method to set the icon of the button when it is pressed. This will give you control over how the pressed state is rendered.

  3. #3
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Setting JButton's mousedown state

    Quote Originally Posted by doWhile View Post
    You can use the setPressedIcon method to set the icon of the button when it is pressed. This will give you control over how the pressed state is rendered.
    Hmm. Doesn't seem to be working. I've tried both setting the setPressedIcon value to null and the same ImageIcon as the regular state. Still getting that white background though.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Setting JButton's mousedown state

    Don't use a background color that is transparent. Instead use:
    Java Code:
    super.setBackground( Color.BLACK ) );
    
    or
    
    super.setBackground(new Color(0, 0, 0));

  5. #5
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Setting JButton's mousedown state

    Quote Originally Posted by camickr View Post
    Don't use a background color that is transparent. Instead use:
    Java Code:
    super.setBackground( Color.BLACK ) );
    
    or
    
    super.setBackground(new Color(0, 0, 0));
    Hi camickr,

    Thanks for your reply however that's not quite what I'm looking for either. If I set the background to black then I get a black background where I want it to be transparent. It also doesn't fix the mousedown issue.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Setting JButton's mousedown state

    Try button.setContentAreaFilled(false). You won't have any change to the button color when pressed unless you change the setPressedIcon to a different image.
    0026sd and Fubarable like this.

  7. #7
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Setting JButton's mousedown state

    Quote Originally Posted by doWhile View Post
    Try button.setContentAreaFilled(false). You won't have any change to the button color when pressed unless you change the setPressedIcon to a different image.
    Perfect!! Worked like a charm

    Thanks so much for your help!

Similar Threads

  1. Replies: 7
    Last Post: 07-12-2011, 04:19 PM
  2. setting JButton setEnabled() outside of main class
    By danborgir in forum New To Java
    Replies: 11
    Last Post: 05-07-2011, 04:03 PM
  3. Replies: 2
    Last Post: 02-14-2011, 02:27 PM
  4. Replies: 3
    Last Post: 05-25-2009, 03:33 AM
  5. rewrite mousedown code
    By brahms666 in forum New To Java
    Replies: 0
    Last Post: 11-26-2008, 01:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •