0

I have a problem on a JTextField the thing is that whenever I type something on it I want the input to be analyzed and in the case that the input is "ADMIN" it needs to trigger a password text box.

The problem resides on the txtUsuario.addActionListener

I have the following code for now

package FP.Graphics;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.BoxLayout;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import javax.swing.JLabel;

public class MainWindow extends JFrame {

    protected static final Window lblPassword = null;
    private JPanel contentPane;
    private JTextField txtUsuario;
    private JTextField textField;
    private JTextField TextBox;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWindow frame = new MainWindow();
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        txtUsuario = new JTextField();
        txtUsuario.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Update();
            }
            public void Update() {
                if (txtUsuario.getText().toUpperCase().equals("ADMIN"))//Si el texto digitado pasado a mayuscula es "ADMIN" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().toUpperCase().equals("GESTOR"))//Si el texto digitado pasado a mayuscula es "GESTOR" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().toUpperCase().equals("USUARIO"))//Si el texto digitado pasado a mayuscula es "USUARIO" se ejecutara el siguiente bloque de codigo
                {

                }
                else
                {

                    lblPassword.setVisible(false);
                    passwordField.setVisible(false);//Se ocultara el textbox de password
                }
            }
        });

        txtUsuario.setBounds(300, 196, 86, 20);
        contentPane.add(txtUsuario);
        txtUsuario.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setEnabled(false);
        passwordField.setBounds(300, 225, 86, 20);
        contentPane.add(passwordField);
        passwordField.setVisible(false);

        JLabel lblUsuario = new JLabel("Usuario");
        lblUsuario.setBounds(209, 199, 46, 14);
        contentPane.add(lblUsuario);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(209, 228, 66, 14);
        contentPane.add(lblPassword);
        lblPassword.setVisible(false);

        JButton btnLogin = new JButton("Login");
        btnLogin.setBounds(426, 195, 79, 23);
        contentPane.add(btnLogin);
    }
}

ive also tried this without success

txtUsuario.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent arg0) {
                if (txtUsuario.getText().equals("ADMIN"))//Si el texto digitado pasado a mayuscula es "ADMIN" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().equals("GESTOR"))//Si el texto digitado pasado a mayuscula es "GESTOR" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().equals("USUARIO"))//Si el texto digitado pasado a mayuscula es "USUARIO" se ejecutara el siguiente bloque de codigo
                {

                }
                else
                {
                    lblPassword.setVisible(false);
                    passwordField.setVisible(false);//Se ocultara el textbox de password
                }

            }
            @Override
            public void insertUpdate(DocumentEvent arg0) {
                if (txtUsuario.getText().equals("ADMIN"))//Si el texto digitado pasado a mayuscula es "ADMIN" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().equals("GESTOR"))//Si el texto digitado pasado a mayuscula es "GESTOR" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().equals("USUARIO"))//Si el texto digitado pasado a mayuscula es "USUARIO" se ejecutara el siguiente bloque de codigo
                {

                }
                else
                {
                    lblPassword.setVisible(false);
                    passwordField.setVisible(false);//Se ocultara el textbox de password
                }

            }
            @Override
            public void removeUpdate(DocumentEvent arg0) {
                if (txtUsuario.getText().equals("ADMIN"))//Si el texto digitado pasado a mayuscula es "ADMIN" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().equals("GESTOR"))//Si el texto digitado pasado a mayuscula es "GESTOR" se ejecutara el siguiente bloque de codigo
                {
                    lblPassword.setVisible(true);
                    passwordField.setVisible(true);//Se hara visible un textbox de password
                }
                else if (TextBox.getText().equals("USUARIO") )//Si el texto digitado pasado a mayuscula es "USUARIO" se ejecutara el siguiente bloque de codigo
                {

                }
                else
                {
                    lblPassword.setVisible(false);
                    passwordField.setVisible(false);//Se ocultara el textbox de password
                }

            }

        });
  • Add a DocumentListener to txtUsuario.getDocument() to respond to input changes. And please also study the Java Naming Convention. – SurfMan Feb 01 '18 at 12:01
  • ive attempted to even do that and also resulted in a failure – Raul Pereyra Feb 01 '18 at 12:05
  • 1) Please don't use the space for answers to instead post what should be an [edit] to the question. 2) Tip: Add @SurfMan (or whoever, the `@` is important) to *notify* the person of a new comment. Noting the first sentence of the text below, be aware this only works when making a comment, and only when in the same 'comment thread' as the person who is being notified. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or .. – Andrew Thompson Feb 01 '18 at 12:15
  • .. [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 01 '18 at 12:16
  • Thanks for the pointers! – Raul Pereyra Feb 01 '18 at 12:22
  • I dont understand the info from the combinations of them, im just starting with swing – Raul Pereyra Feb 01 '18 at 12:26
  • can someone help? – Raul Pereyra Feb 01 '18 at 12:56
  • *"Thanks for the pointers!"* Did you miss point 2? Who are you replying to? – Andrew Thompson Feb 01 '18 at 22:42

0 Answers0