1

I'm doing a form in java.

The idea is:

When an user insert his name and second name he have to press the "submit" button. Now I would like to listen to this action with the submit button but I don't know how to get what the user put in the text fields: "name" and "second name".

I would like to see this data just with the submit button, but i don't know, if it's possible.

Or I would like to know in which way I can see all this data when the user clicks on "submit".

thanks, this is the code.

public class appNegozio extends JFrame {
     private JPanel contentPane;
     private JTextField textField;
     private JTextField textField_1;

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

/**
 * Create the frame.
 */
    public appNegozio() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 450, 300);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       SpringLayout sl_contentPane = new SpringLayout();
       contentPane.setLayout(sl_contentPane);

       textField = new JTextField();
       sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 10,    
                   SpringLayout.NORTH, contentPane);
       sl_contentPane.putConstraint(SpringLayout.WEST, textField, 62, 
                   SpringLayout.WEST, contentPane);
       contentPane.add(textField);
       textField.setColumns(10);

       textField_1 = new JTextField();
       sl_contentPane.putConstraint(SpringLayout.NORTH, textField_1, 16, 
                  SpringLayout.SOUTH, textField);
       sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0, 
                  SpringLayout.WEST, textField);
       contentPane.add(textField_1);
       textField_1.setColumns(10);

      JLabel lblNome = new JLabel("Nome");
      sl_contentPane.putConstraint(SpringLayout.NORTH, lblNome, 10,  
                  SpringLayout.NORTH, contentPane);
      sl_contentPane.putConstraint(SpringLayout.EAST, lblNome, -7, 
                  SpringLayout.WEST, textField);
      contentPane.add(lblNome);

      JLabel lblCognome = new JLabel("Cognome");
      sl_contentPane.putConstraint(SpringLayout.NORTH, lblCognome, 0, 
                  SpringLayout.NORTH, textField_1);
      sl_contentPane.putConstraint(SpringLayout.EAST, lblCognome, -6, 
                  SpringLayout.WEST, textField_1);
      contentPane.add(lblCognome);

    JButton btnSubmit = new JButton("Submit");
    sl_contentPane.putConstraint(SpringLayout.NORTH, btnSubmit, 24, 
                  SpringLayout.SOUTH, textField_1);
    sl_contentPane.putConstraint(SpringLayout.WEST, btnSubmit, 10, 
                  SpringLayout.WEST, contentPane);
    contentPane.add(btnSubmit);
   }
 }

3 Answers 3

2

An easy approach would be to use a final local variable for each JTextField. You can access them from within an ActionListener added to your JButton:

final JTextField textField = new JTextField();

btnSubmit.addActionListener(new ActionListener() {

    public void actionPerformed(final ActionEvent e) {
        System.out.println(textField.getText());
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Don't extend a JFrame, you are not adding any new functionality to the frame.

Read the section from the Swing tutorial on How to Use Text Fields for a working example that will show you a better way to structure your program so that you have access to the data in the text fields from your ActionListener.

The Swing tutorial has examples for all Swing components.

1 Comment

@GiovanniFar, don't you think you should read the tutorial first (before decideing which answer you want to use) to understand the suggestion. It is not a good idea to make variables final just so your program compiles. You should design the code properly from the beginning.
0

use JTextField object.getText() and store it locally in a String in Event function of the button.Use that local String for reference.

Example

String name=jtextfield.getText();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.