Show typed text in a text field after clicking button

Asked

Viewed 762 times

1

The code below I got from the Internet is working for me. In the main, it executes a form with the name to type and the button ok.

I wanted to run, no main, shortly after the new MeuPrograma(); one System.out.println("##") where it displayed the name that the guy typed there in the field of swing How do I do that?

public class MeuPrograma extends JFrame  {



public static JTextField text = new JTextField(10);

private static final long serialVersionUID = 1L;
    private JLabel labelNome;
    private JTextField textFieldNome;
    private JButton buttonOk;

    public MeuPrograma() {
        setTitle("Programa Swing1");
        setLayout(new FlowLayout());
        labelNome = new JLabel("Nome: ");
        textFieldNome = new JTextField(15);
        buttonOk = new JButton("OK");
        // --> adiciona os componentes a janela
        add(labelNome);
        add(textFieldNome);
        add(buttonOk);
        // --> ajusta o tamanho, a posicao e a acao ao fechar
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        // --> mostra a janela
        setVisible(true);
    }



public static void main(String[] args)  {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // --> cria um novo objeto do tipo Swing1
    // por causa da execucao multithreading da
    // API swing,isso deve ser feito dessa forma:
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MeuPrograma();
        }
    });
  • i want to display the system.out.println after q the guy click ok

  • 2

    If it is desktop, System.out.println will not display anything, because it only displays on the console, unless the user starts the application via command line. Explain better where you want to show off and why.

  • I actually have a code that generates a pdf using itext, I want to take this typed value and play inside a variable.. so I want to know how to get the value that was typed when the guy click ok

1 answer

3


To capture the value typed in a JTextFiled after the button is pressed, add a Listener in the JButton, through the method actionPerformed, something like that:

buttonOk.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
  String strTexto = textFieldNome.getText();
  //adicione o que quer fazer com o texto
 }
});

As it is an invocation of anonymous class, to pass the value to Listener of JButton, you can or create external method that makes this capture, and call it within the action, transform strTexto in a class parameter MeuPrograma or make it final.

Example:

private String strTexto;

...

buttonOk.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {

  strTexto = textFieldNome.getText();

 }
});

...

Further clarification can be obtained in official documentation.

References for reading:

How to Use Buttons, Check Boxes, and Radio Buttons

How to Write an Action Listener

Browser other questions tagged

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