Comment récupérer la valeur de JTextField dans Java Swing?

Comment récupérer la valeur d'un champ de texte et actionPerformed()? J'ai besoin que la valeur soit convertie en String pour la suite du traitement. J'ai créé un champ Texte en cliquant sur un bouton j'ai besoin de stocker la valeur entrée dans un String pouvez-vous fournir un extrait de code?

27
demandé sur e-sushi 2011-04-22 08:33:27

6 réponses

testField.getText()

voir le java doc pour component swing jtextfield

exemple de code peut être:

button.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent ae){
      String textFieldValue = testField.getText();
      // .... do some operation on value ...
   }
})
48
répondu Harry Joy 2013-07-19 04:00:16
* First we declare JTextField like this

 JTextField  testField = new JTextField(10);

* We can get textfield value in String like this on any button click event.

button.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent ae){
      String getValue = testField.getText()

   }
})
8
répondu Chetan 2015-01-30 08:53:50

Comment récupérer une valeur dans un champ de texte?

mytestField.getText();

ActionListner exemple:

mytextField.addActionListener(this);

public void actionPerformed(ActionEvent evt) {
    String text = textField.getText();
    textArea.append(text + newline);
    textField.selectAll();
}
5
répondu Nirmal- thInk beYond 2011-04-22 06:35:09
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Swingtest extends JFrame implements ActionListener
{
    JTextField txtdata;
    JButton calbtn = new JButton("Calculate");

    public Swingtest()
    {
        JPanel myPanel = new JPanel();
        add(myPanel);
        myPanel.setLayout(new GridLayout(3, 2));
        myPanel.add(calbtn);
        calbtn.addActionListener(this);
        txtdata = new JTextField();
        myPanel.add(txtdata);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == calbtn) {
            String data = txtdata.getText(); //perform your operation
            System.out.println(data);
        }
    }

    public static void main(String args[])
    {
        Swingtest g = new Swingtest();
        g.setLocation(10, 10);
        g.setSize(300, 300);
        g.setVisible(true);
    }
}

maintenant son travail

3
répondu jayesh 2015-03-20 14:17:42

il suffit d'utiliser event.getSource() frim dans les actionPerformed

jette-le au composant

par exemple, si vous avez besoin de combobox

JComboBox comboBox = (JComboBox) event.getSource();
JTextField txtField = (JTextField) event.getSource();

utiliser l'api appropriée pour obtenir la valeur,

pour les anciens.

Object selected = comboBox.getSelectedItem();  etc.
2
répondu Anuj Singh 2014-12-17 07:58:36

Ce que j'ai trouvé utile est cette condition qui est ci-dessous.

String tempEmail = "";
JTextField tf1 = new JTextField();

tf1.addKeyListener(new KeyAdapter(){
    public void keyTyped(KeyEvent evt){
         tempEmail = ((JTextField)evt.getSource()).getText() + String.valueOf(evt.getKeyChar());
    }
});
2
répondu ArifMustafa 2015-05-03 02:27:27