Change TextField color when writing

1

I am in a task for the university and they ask me to change the background color of a textfield when the user enters a character , I know that to change the background is occupied by the .SetBackgroud (color red), but I have only been able to configure it that way by pressing a button so that it changes the color of the background. Does anyone know how to do it so that when you enter a character the background changes? thanks!

    
asked by Bastian 25.11.2017 в 21:19
source

2 answers

0

How about there you have a way to do it very similar to what happened to the previous user, be sure to position yourself on that class, right button and then "Run" - > "Ass Java Application" , once the swing window opens, enter some character in the JTextField.

Form number 1:

Directly from the JTextField we call a KeyListener, it avoids having to declare the text.addKeyListener (this); as well as we also managed to implement the interface in the class and the implementation of the rest of its methods, because we are directly calling it from the button.

The result is that in a very simple way, by touching any key we change its background.

Ideally, in a case like this, we would apply Lambda, but we can not , since the interface declares more than one method within its interface.

package com.soa.frames;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Panel {
    private JFrame frame;
    private JPanel panel;
    private JTextField text;

    public Panel() {
        frame = new JFrame();
        panel = new JPanel();
        text = new JTextField(10);

        BorderLayout borderLayout = new BorderLayout();
        frame.setTitle("Change JTextField Color");
        frame.add(panel);
        panel.add(text, borderLayout.NORTH);
        frame.setBounds(100, 100, 250, 150);
        frame.setVisible(true);

        /* Metemos la interfaz directamente dentro del boton, sin implementar nada */
        text.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                text.setBackground(Color.BLUE);
            }
        });

    }

    public static void main(String[] args) {
        /* Creamos el panel con el setVisible() en su constructor */
        Panel p = new Panel();

    }

}

Form number 2:

In the following one it is not more than a conditional, where we do implement the interface and it forces us to declare all the methods within it. If we want the background of the JTextField to change depending on the code of the letter that we enter then this code goes well, I do not know the codes of the letters but they are simply ascertained.

package com.soa.frames;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;  
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Panel implements KeyListener {
private JFrame frame;
private JPanel panel;
private JTextField text;

    public Panel() {
        frame = new JFrame();
        panel =new JPanel();
        text = new JTextField(10);

        BorderLayout borderLayout = new BorderLayout();
        frame.setTitle("Change JTextField Color");
        frame.add(panel);
        panel.add(text, borderLayout.NORTH);
        frame.setBounds(100, 100, 250, 150);
        frame.setVisible(true);

        text.addKeyListener( this);
    }

    public static void main(String[] args) {
        /* Creamos el panel con el setVisible() en su constructor */
        Panel p = new Panel();

    }

    /* Al ser una interface te obliga a definir todos los métodos, sin embargo sólo usamos uno */
    /* Mandé cualquier código de tecla, total ni sé cual es el 65 */
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getSource().equals(KeyEvent.getKeyText(0))) {
            text.setBackground(Color.BLUE);
        }
        else {
            text.setBackground(Color.PINK);
        }

    }

    /* Estos métodos estamos obligados a llamarlos por ser una interface pero no los vamos a definir, sólo declarar */
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}
    
answered by 26.11.2017 / 04:59
source
0

Try this:

jTextField.addKeyListener(new KeyAdapter() {
   @Override
   public void keyPressed(KeyEvent e) {
    ((JTextField) e.getSource()).setBackground(Color.red);
   }
  });
    
answered by 25.11.2017 в 22:28