Ver Mensaje Individual
  #2 (permalink)  
Antiguo 28/10/2014, 01:14
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 9 meses
Puntos: 182
Respuesta: JOptionPane cambiar color boton onFocus

Buenas,

Puedes cambiar el color de fondo del boton con el metodo setBackGround cuando se produzca el evento del foco.

Aqui te pongo un ejemplo de como cambiar el fondo que puedes ejecutar y probar:

Código Java:
Ver original
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3.  
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.SwingConstants;
  7. import javax.swing.SwingUtilities;
  8.  
  9. public class TestButton {
  10.  
  11.     class MyButton extends JButton {
  12.  
  13.         private Color hoverBackgroundColor;
  14.         private Color pressedBackgroundColor;
  15.  
  16.         public MyButton() {
  17.             this(null);
  18.         }
  19.  
  20.         public MyButton(String text) {
  21.             super(text);
  22.             super.setContentAreaFilled(false);
  23.         }
  24.  
  25.         @Override
  26.         protected void paintComponent(Graphics g) {
  27.             if (getModel().isPressed()) {
  28.                 g.setColor(pressedBackgroundColor);
  29.             } else if (getModel().isRollover()) {
  30.                 g.setColor(hoverBackgroundColor);
  31.             } else {
  32.                 g.setColor(getBackground());
  33.             }
  34.             g.fillRect(0, 0, getWidth(), getHeight());
  35.             super.paintComponent(g);
  36.         }
  37.  
  38.         @Override
  39.         public void setContentAreaFilled(boolean b) {
  40.         }
  41.  
  42.         public Color getHoverBackgroundColor() {
  43.             return hoverBackgroundColor;
  44.         }
  45.  
  46.         public void setHoverBackgroundColor(Color hoverBackgroundColor) {
  47.             this.hoverBackgroundColor = hoverBackgroundColor;
  48.         }
  49.  
  50.         public Color getPressedBackgroundColor() {
  51.             return pressedBackgroundColor;
  52.         }
  53.  
  54.         public void setPressedBackgroundColor(Color pressedBackgroundColor) {
  55.             this.pressedBackgroundColor = pressedBackgroundColor;
  56.         }
  57.     }
  58.  
  59.     protected void createAndShowGUI() {
  60.         JFrame frame = new JFrame("Test button");
  61.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62.         final MyButton btnSave = new MyButton("Save");
  63.         btnSave.setForeground(new Color(0, 135, 200).brighter());
  64.         btnSave.setHorizontalTextPosition(SwingConstants.CENTER);
  65.         btnSave.setBorder(null);
  66.         btnSave.setBackground(new Color(3, 59, 90));
  67.         btnSave.setHoverBackgroundColor(new Color(3, 59, 90).brighter());
  68.         btnSave.setPressedBackgroundColor(Color.PINK);
  69.         frame.add(btnSave);
  70.         frame.setSize(200, 200);
  71.         frame.setVisible(true);
  72.     }
  73.  
  74.     public static void main(String[] args) {
  75.         SwingUtilities.invokeLater(new Runnable() {
  76.             @Override
  77.             public void run() {
  78.                 new TestButton().createAndShowGUI();
  79.             }
  80.         });
  81.  
  82.     }
  83.  
  84. }


Fuente: http://stackoverflow.com/questions/1...-mouse-pressed

Un saludo
__________________
If to err is human, then programmers are the most human of us