Forum Stats

  • 3,873,394 Users
  • 2,266,552 Discussions
  • 7,911,524 Comments

Discussions

How to change the JButton Pressed Color

843789
843789 Member Posts: 46,655 Green Ribbon
Hi,

I'm trying to change the Button pressed color and I haven't found any property to change it.

How do I do to change this color?, is it necessary to change the color with a listener? do I need to create my own button to make it possible? or I need to create a mono-color Icon and then use it?

Thanks,

Ana

Comments

  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    There's a setPressedIcon() method you could use if you're using Icons on your buttons, and there's a setBackground() method you can use to change the background color of the button if not. I'm assuming you're using swing components.
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Hi,

    Thanks for the answer!

    I don't want to create an Icon to change the color, because I want to be able to change it on the run, I changed the background color but it changes the color when the button is not pressed I want to change the color when its pressed.

    Is there an other way?
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    A JButton only has one color at a time, it doesn't understand changing it's color when it's pressed, so you have to do it manually. Add a listener to it, (probably a MouseListener, I can't remember if there's a better one off the top of my head) and when the mousePressed event fires, change the color. When the mouseReleased event fires, change it back. You might need to use mouseExited too if you want it to change back if the user takes the mouse off the button while it's pressed.

    http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    So the better way its to create a new button class that extends the j button and add the listener to it so I can reuse it?
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Do you want to click the button have have the color remain changed when you release the mouse button, and then if you click again, have it change back? You just need an actionListener for that. Here is some simple code
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    class Test extends JPanel {
    
    	JButton btn;
    	Color btnDefault;
    	Color btnSecond = new Color(234,56,120);
    	
    	public Test()
    	{
    		super();
    		btn = new JButton("OK");
    	
    		btnDefault = btn.getBackground();
    		btn.addActionListener(new ActionListener(){
    						public void actionPerformed(java.awt.event.ActionEvent e) 
    						{
    							if(btn.getBackground().equals(btnDefault))
    								btn.setBackground(btnSecond);
    							else
    								btn.setBackground(btnDefault);
    						}
    						});
    		
    		this.add(btn, BorderLayout.NORTH);
    		
    	}
    	
    	public static void main(String[] args) {
    		JFrame f = new JFrame("Test");
    		Test pnl = new Test();
    		f.add(pnl,BorderLayout.NORTH);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setBounds(100,100,500,500);
    		f.setVisible(true);
    		
    		
    		
    	}
    
    }
    ~Tim
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    edited Jan 12, 2007 2:24PM
    Replying to your last message, if you want the same behavior for all your buttons, you could do that, extend the JButton that is, and in its constructor add a listener that will change the color when clicked. Or you can extend ActionListener and add that custom listener to all of you buttons.

    ~Tim

    Edit: fixed many typos
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    I think I preffer the first way Im going to try it, I'll add my own pressed color property and then it will be easy to change it on the run.

    thanks to both of you!
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Hey guys,

    It didn't work it only works for the enter and exit but not for the pressed and released states.

    Here its my code:
    package componentes;
    
    import javax.swing.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.*;
    
    /**
     * JButton con una nueva propiedad de Color presionado
     * @author Ana Mar�a Franco
     * @version 1.0
     */
    public class PressedColorButton extends JButton
    {
        // Atributos ************************************************************************
    
        private Color unpressedButtonColor;
        private Color pressedButtonColor;
    
        // Constructores ********************************************************************
        public PressedColorButton()
        {
            super();
            this.addMouseListener(new PressedColorButton_this_mouseAdapter(this));
        }
    
        // M�todos **************************************************************************
    
        /**
         * Obtiene el color sin presionar
         * @return Color
         */
        public Color getUnpressedButtonColor()
        {
            return this.unpressedButtonColor;
        }
    
        /**
         * Define el color sin presionar
         * @param nuevoColor Color
         */
        public void setUnpressedButtonColor(Color nuevoColor)
        {
            this.unpressedButtonColor = nuevoColor;
        }
    
        /**
         * Obtiene el color presionado
         * @return Color
         */
        public Color getPressedButtonColor()
        {
            return this.pressedButtonColor;
        }
    
        /**
         * Define el color presionado
         * @param nuevoColor Color
         */
        public void setPressedButtonColor(Color nuevoColor)
        {
            this.pressedButtonColor = nuevoColor;
        }
    
        public void this_mouseExited(MouseEvent e)
        {
            this.setBackground(unpressedButtonColor);
        }
    
        public void this_mousePressed(MouseEvent e)
        {
            this.setBackground(pressedButtonColor);
        }
    
        public void this_mouseReleased(MouseEvent e)
        {
            this.setBackground(unpressedButtonColor);
        }
    
        public void this_mouseEntered(MouseEvent e)
        {
            this.setBackground(pressedButtonColor);
        }
    
    }
    
    // Escuchadores *************************************************************************
    
    class PressedColorButton_this_mouseAdapter extends MouseAdapter
    {
        private PressedColorButton adaptee;
        PressedColorButton_this_mouseAdapter(PressedColorButton adaptee)
        {
            this.adaptee = adaptee;
        }
    
        public void mouseExited(MouseEvent e)
        {
            adaptee.this_mouseExited(e);
        }
    
        public void mousePressed(MouseEvent e)
        {
            adaptee.this_mousePressed(e);
        }
        
        public void mouseReleased(MouseEvent e)
        {
            adaptee.this_mouseReleased(e);
        }
    
        public void mouseEntered(MouseEvent e)
        {
            adaptee.this_mouseEntered(e);
        }
    }
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Good Morning. I don't know if you solved your problem, but the JButton doesn't seem to repaint the background color until you release the mouse button. You can create a JLabel and add it to the button, then play around with the button and label borders to create what you want, because the JLabel will repaint its background color immediately.

    Good Luck

    ~Tim
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Hi,

    I posted it on an other forum and I got that I needed to change this properties on the button for it to work:
            this.setContentAreaFilled(false);
            this.setOpaque(true);
    and now works!

    Thanks!,

    Ana
This discussion has been closed.