Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get value on TextField Java Swing

I have a simple Java Swing form with a JTextField, I get value on JTextField by getText() method but I can't use it to main program. Can you help me What problem and how to fix? This is my code :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Login {
private String name;

public Login() {
    JFrame main = new JFrame("LOGIN");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setResizable(false);
    main.setLayout(null);
    main.setPreferredSize(new Dimension(200, 300));
    main.setLocation(400, 200);

    // Heading: LOGIN
    JLabel heading = new JLabel("LOGIN");
    heading.setBounds(80, 20, 50, 20);
    main.add(heading);

    // Label Username
    JLabel username_label = new JLabel("username: ");
    username_label.setBounds(5, 70, 80, 20);
    main.add(username_label);
    // Textfield Username
    final JTextField username_field = new JTextField();
    username_field.setBounds(70, 70, 120, 20);
    main.add(username_field);
    this.name = username_field.getText();

    // Button Login
    JButton loginBtn = new JButton("LOGIN");
    loginBtn.setBounds(40, 150, 120, 25);
    main.add(loginBtn);
    main.pack();
    main.setVisible(true);

    loginBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            name = username_field.getText();
            // System.out.println(name); //IT WORKS
        }
    });
}

public static void main(String[] args) {
    Login me = new Login();
    me.print();//I EXPECT IT WILL PRINT @name BUT NO, WHY? 
}

public void print() {
    System.out.println(name);
}
}

Thanks very much!

like image 368
dientmUET Avatar asked Dec 30 '13 12:12

dientmUET


3 Answers

You shouldn't print the value before user clicks the button

public static void main(String[] args) {
    HelloWorldSwing me = new HelloWorldSwing();
    //me.print();//DON't PRINT HERE
}

Instead, you can do this in actionPerformed() method,

 loginBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            name = username_field.getText();
            HelloWorldSwing.this.print();
        }
    });

This will print the value, you enter in your text field

like image 52
Keerthivasan Avatar answered Nov 19 '22 06:11

Keerthivasan


You are trying to print the name value immediately after showing your GUI:

Login me = new Login();
me.print(); // This executes immediately after the statement above

But this value is not set until the user presses the login button:

loginBtn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        name = username_field.getText();
        // System.out.println(name); //IT WORKS
    }
});
like image 31
Duncan Jones Avatar answered Nov 19 '22 05:11

Duncan Jones


Adding a text field to a GUI class does not magically add a getText() method to that class. I mean think about it, if there were two text fields, which one should be used for getText()? The field should be declared as an attribute of the class, and a method defined that will getText() of that field.

BTW:

  1. don't use a JTextField when the GUI actually requires a JPasswordField.
  2. Instead of JFrame this should probably be a modal JDialog. See How to Use Modality in Dialogs for details.
like image 5
Andrew Thompson Avatar answered Nov 19 '22 06:11

Andrew Thompson