Recover the value of a Jtextfield via an Actionlistener

Asked

Viewed 1,313 times

2

I’m a little new to java and I was trying to understand the listeners. So excuse me if the mistake is simple.

I have a program that creates a simple graphical interface and I want to get the value of a JTextField through a ActionListener. Follows the code:

public abstract class Teste {

    public static void main(String[] args) {

        JFrame tela = new JFrame("Teste");
        JPanel panel = new JPanel();
        JLabel texto = new JLabel("Nome: ");
        JTextField nomeField = new JTextField(60);

        panel.add(texto);
        panel.add(nomeField);

        Clique text = new Clique();
        nomeField.addActionListener(text);


        panel.setLayout(new FlowLayout());
        tela.setContentPane(panel);
        tela.pack();
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tela.setVisible(true);
   }

}


class Clique implements ActionListener{

    public void actionPerformed(ActionEvent e){

        System.out.println(); //Quero imprimir o texto do campo aqui

    }
}

The point is that I don’t know how to reference the text field object to get the value through a getText().

1 answer

2


You can pass the required field in the builder:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class Teste {

    public static void main(String[] args) {

        JFrame tela = new JFrame("Teste");
        JPanel panel = new JPanel();
        JLabel texto = new JLabel("Nome: ");
        JTextField nomeField = new JTextField(60);

        panel.add(texto);
        panel.add(nomeField);

        Clique text = new Clique(nomeField);
        nomeField.addActionListener(text);

        panel.setLayout(new FlowLayout());
        tela.setContentPane(panel);
        tela.pack();
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tela.setVisible(true);
   }

}

class Clique implements ActionListener {

    private final JTextField field;

    public Clique(JTextField field) {
        this.field = field;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(field.getText());
    }
}

Another alternative is to use a lambda (Java 8 or higher):

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class Teste {

    public static void main(String[] args) {

        JFrame tela = new JFrame("Teste");
        JPanel panel = new JPanel();
        JLabel texto = new JLabel("Nome: ");
        JTextField nomeField = new JTextField(60);

        panel.add(texto);
        panel.add(nomeField);

        nomeField.addActionListener(event -> System.out.println(nomeField.getText()));

        panel.setLayout(new FlowLayout());
        tela.setContentPane(panel);
        tela.pack();
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tela.setVisible(true);
   }

}

A third way, if you are in Java 7 or below, is to use an anonymous class:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class Teste {

    public static void main(String[] args) {

        JFrame tela = new JFrame("Teste");
        JPanel panel = new JPanel();
        JLabel texto = new JLabel("Nome: ");
        JTextField nomeField = new JTextField(60);

        panel.add(texto);
        panel.add(nomeField);

        nomeField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.out.println(nomeField.getText());
            }
        });

        panel.setLayout(new FlowLayout());
        tela.setContentPane(panel);
        tela.pack();
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tela.setVisible(true);
   }

}
  • very good answer, about the second method has some link that can indicate with some further explanation?

  • 1

    @leandro17br See if this link helps you: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html - If it doesn’t help, just search Google for "java lambda-Expressions" and you should find plenty of things.

Browser other questions tagged

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