Change label color according to screen status

2

I'm having trouble changing the color of the content of a label, according to the state of my screen (enabled / disabled).

I would like to know how I can to make it change the color.

I made a very simple example (without worrying about the best practices of organization and etc.)

Simplified code example:

package cor;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MudaCor extends JFrame implements ActionListener
{
    public final int DESABILITADA = 0;
    public final int HABILITADA = 1;
    public int estadoTela = DESABILITADA;

    public JPanel jpBotoes = new JPanel();
    JTextField tx1 = new JTextField();
    JTextField tx2 = new JTextField();

    private JButton botao01 = new JButton("Habilita");
    private JButton botao02 = new JButton("Desabilita");

    private String conteudo = "Teste de cor";

    public MudaCor()
    {
        setTitle("Tela de teste");
        setSize(400, 300);      
        add(posicaoComponentes());        
        habilitaComp(false);        
        estilo(conteudo);//metodo estilo        
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }    

    public String  estilo(String estilo)
    {
        String x = "<html><font color=#4a2d56> <b> Teste de cor </b> </font></html>"; //roxo
        String y = "<html><font color=#225218> <b> Teste de cor </b> </font></html>";//verde

        if(estadoTela != DESABILITADA)
        {
            return conteudo = x;
        }

        else
        {
            return conteudo = y;
        }        
    }       

    public JComponent posicaoComponentes()
    {
        JPanel painel = new JPanel();
        painel.setLayout(null);
        JLabel label = new JLabel(estilo(conteudo));        
        painel.add(label);
        label.setBounds(170, 150, 100, 25);        
        painel.add(tx1);
        tx1.setBounds(130, 30, 150, 22);                            
        painel.add(tx2);       
        tx2.setBounds(130, 70, 150, 22);

        getContentPane().add("South", jpBotoes);        
        jpBotoes.setLayout(new GridLayout(1, 2));      
        adicionaBotao(botao01);
        adicionaBotao(botao02);

        return painel;        
    }

    private void adicionaBotao(JButton botao) 
    {
        jpBotoes.add(botao);
        botao.addActionListener(this);
    }

    public void habilitaComp(boolean status)
    {
        if(estadoTela == DESABILITADA)
        {
            tx1.setEnabled(status);
            tx2.setEnabled(status);
        }

        else
        {
            tx1.setEnabled(status);
            tx2.setEnabled(status);
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {

        if (ae.getSource() == botao01) 
        {            
            habilitaComp(true);
        } 

        else if (ae.getSource() == botao02) 
        {
            habilitaComp(false);
        } 
    }

    public static void main(String[] args) 
    {
        MudaCor cor = new MudaCor();
        cor.setVisible(true);
    }     
}
    
asked by anonymous 21.04.2017 / 19:56

1 answer

2

First and foremost I want to leave two alerts about your code:

  

Always start the screen within the Event-Dispatch-Thread because the swing API is not Thread-Safe , and the entire interface needs to start within this single thread. In this answer better explains the reason for this and possible problems that may occur. This other answer shows you some ways to start the application within this Thread.

     

Avoid using absolute layout , the swing API comes in many Layouts Managers to make it easier for the programmer to create screens, and to make the screen flexible to different monitor sizes and resolutions, without having to deal with it directly in the code. Absolute Layout will break the look of your application, depending on the monitor where the application is run.

To change the font color of a JLabel , simply use the setForeground() ", but even adding this to your code, it never changes the estadoTela variable and the status parameter of the habilitaComp method, which is what you is using to change the status of the component, it is never used.

I made some changes to your code so that the color changes according to the boolean value passed in the habilitaComp method, there is no need to create constants:

public void habilitaComp(boolean status)
{

    tx1.setEnabled(status);
    tx2.setEnabled(status);
    label.setForeground(status ? new Color(74, 45, 86) : Color.GREEN);

}

With this change, the estilo method and the constants you created to control the state are no longer needed, as it creates unnecessary complexity for something you can do using ternary operation.

    
21.04.2017 / 20:33