9

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!

Sandesh
  • 1,190
  • 3
  • 23
  • 41
dientmUET
  • 101
  • 1
  • 1
  • 14
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Dec 30 '13 at 12:07

3 Answers3

10

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

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
6

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
    }
});
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
5

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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433