[RESOLVIDO] Mudança de cor ao entrar/sair de JTextField

Olá pessoal,

Estou tentando implementar uma lógica para que quando um JTextField receber o foco ele fique com uma cor de fundo/fonte, e quando ele perder o foco deve mudar novamente.

O código que estou utilizando é este:

       tfField.setForeground(new Color(cfgInstance.getValueInteger("FIELD_FG_COLOR_R"),
                                       cfgInstance.getValueInteger("FIELD_FG_COLOR_G"),
                                       cfgInstance.getValueInteger("FIELD_FG_COLOR_B")));
       tfField.setBackground(new Color(cfgInstance.getValueInteger("FIELD_BG_COLOR_R"),
                                       cfgInstance.getValueInteger("FIELD_BG_COLOR_G"),
                                       cfgInstance.getValueInteger("FIELD_BG_COLOR_B")));

       tfField.addFocusListener(new FocusListener() {
         public void focusGained(FocusEvent evt) {
                            CFGManager cfgInstance = CFGManager.getInstance();
                            setForeground(new Color(cfgInstance.getValueInteger("FIELD_ATIVE_FOCUSED_FG_COLOR_R"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_FOCUSED_FG_COLOR_G"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_FOCUSED_FG_COLOR_B")));
                            setBackground(new Color(cfgInstance.getValueInteger("FIELD_ATIVE_FOCUSED_BG_COLOR_R"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_FOCUSED_BG_COLOR_G"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_FOCUSED_BG_COLOR_B")));
      }
         public void focusLost(FocusEvent evt) {
                            CFGManager cfgInstance = CFGManager.getInstance();
                            setForeground(new Color(cfgInstance.getValueInteger("FIELD_ATIVE_NOFOCUSED_FG_COLOR_R"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_NOFOCUSED_FG_COLOR_G"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_NOFOCUSED_FG_COLOR_B")));
                            setBackground(new Color(cfgInstance.getValueInteger("FIELD_ATIVE_NOFOCUSED_BG_COLOR_R"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_NOFOCUSED_BG_COLOR_G"),
                                                    cfgInstance.getValueInteger("FIELD_ATIVE_NOFOCUSED_BG_COLOR_B")));
      }
      });

Porém se eu entrar ou sair do campo ele não muda as cores …

Já conferí os valores que o método “getValueInteger” está retornando e está diferente nos dois casos (com/sem foco)…

Eu tenho um método que eu chamo em todo lugar para fazer a “configuração” dos botões.

Quando eu crio um botão, faço assim:

JButton btIncluir = null;
btIncluir = configuraJButton(btIncluir,"Incluir");

Isto faz com que o botão criado fique com a configuração desejada.

O código para tratar a entrada e saída do campo está dentro deste método que configura o botão.

Toda a configuração que eu preciso está sendo feita corretamente, só não está funcionando a troca de cores (da fonte e do fundo) quando eu entro e saio do campo.

Se eu colocar os mesmos comandos “setForeground” e “setBackground” fora do FocusListener, ele assume as cores normalmente. Só não funciona quando ocorre o evento.

Eu já tentei colocar o “tfField” antes da chamada dos métodos “setForeground” e “setBackground”, mas dá erro.

Alguém sabe como fazer como que ele altere as cores ao entrar/sair do campo?

Criei esse ouvinte para vc

[code]
import java.awt.;
import java.awt.event.
;
import javax.swing.*;

/**
*

  • @author David Buzatto
    */
    public class FieldListener implements FocusListener {

    public void focusGained( FocusEvent e ) {

     Object o = e.getSource();
    
     if ( o instanceof JTextField ) {
         
         JTextField f = ( JTextField ) o;
         f.setBackground( new Color( 255, 0, 0 ) );
                     
     }
    

    }

    public void focusLost( FocusEvent e ) {

     Object o = e.getSource();
     
     if ( o instanceof JTextField ) {
         
         JTextField f = ( JTextField ) o;
         f.setBackground( new Color( 255, 255, 255 ) );
         
     }
    

    }

}[/code]

Cria um arquivo chamado FieldListener.java, cola esse código

Nos seus fields vc faz assim

[code]
// cria um novo ouvinte
FieldListener fl = new FieldListener();

// registra o ouvinte nos fiels
field1.addFocusListener( fl );
field2.addFocusListener( fl );
field3.addFocusListener( fl );
field4.addFocusListener( fl );[/code]

Falow!

Valeu pessoal, consegui resolver!