How to change the background of a Jdatechooser?

Asked

Viewed 374 times

2

I have a JDateChooser and I’d like to change the background when he wins focu, but I’m not getting it.

I tried to do some ways:

dataChooser.getDateEditor().getUiComponent().setBackground(new Color(0, 0, 0));

or

dataChooser.setBackground(new Color(0, 0, 0));

and this I put into the event of focusGained

import com.toedter.calendar.JDateChooser;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;

public class BackGr extends JFrame {

    JDateChooser data = new JDateChooser();
    JPanel painel = new JPanel();

    public BackGr() {
        setSize(300, 150);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        painel.add(data);

        data.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                data.getDateEditor().getUiComponent().setBackground(new Color(0, 0, 0));
                //data.setBackground(new Color(0, 0, 0));
            }

            @Override
            public void focusLost(FocusEvent e) {
            }
        });

        add(painel);
    }

    public static void main(String[] args) {
        BackGr bg = new BackGr();
        bg.setVisible(true);
    }
}
  • Please add a [mcve] of your code so it is possible to test and see the problem running.

  • @Articuno put there, sorry !

  • What does that mean focu that you highlighted?

  • focu , in the sense of entering with the cursor in the field, I think I wrote wrong.

  • The application already opens with this field in focus?

  • in this case this, however I tested without starting directly with the focu, and the result is the same.

Show 1 more comment

1 answer

1


If the goal is to directly change the text field, you need to add the event in the JDateChooser. To do this, retrieve the editor component using the method getDateEditor():

JTextFieldDateEditor dateChooserEditor = ((JTextFieldDateEditor)data.getDateEditor());

Then add the "focus" event directly to it:

dateChooserEditor.addFocusListener(new FocusListener() {

    @Override
    public void focusGained(FocusEvent e) {

        dateChooserEditor.setBackground(new Color(0, 0, 0));
    }

    @Override
    public void focusLost(FocusEvent e) {

    }
});

However, this will permanently alter the background of the component. If the goal is only to change when you’re focused on it, an alternative is to store the default background color and restore it when you lose focus:

dateChooserEditor.addFocusListener(new FocusListener() {

    Color defaultBG = dateChooserEditor.getBackground();

    @Override
    public void focusGained(FocusEvent e) {

        dateChooserEditor.setBackground(Color.CYAN);
    }

    @Override
    public void focusLost(FocusEvent e) {

        dateChooserEditor.setBackground(defaultBG);
    }
});

See an example of the above change in practice:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.