-1

I am new to Java and have just tried out Java's swing, I tried making a log in form that would print the content of a JTextField to the console, but the console doesn't show anything when I tried it.

Here's my code:


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

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

public class JavaTextField {

    private JFrame frame;
    private JTextField text1;
    private JTextField text2;

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

    /**
     * Create the application.
     */
    public JavaTextField() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        text1 = new JTextField();
        text1.setBounds(114, 38, 122, 40);
        frame.getContentPane().add(text1);
        text1.setColumns(10);
        String majorText = text1.getText();
        
        
        text2 = new JTextField();
        text2.setBounds(114, 117, 86, 20);
        frame.getContentPane().add(text2);
        text2.setColumns(10);
        String minorText = text2.getText();
        
        JButton btnButton = new JButton("Button");
        btnButton.setBounds(132, 192, 159, 40);
        frame.getContentPane().add(btnButton);
        btnButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(majorText);
                System.out.println(minorText);
            }
            }
    );
}
    }

I'm glad if anyone could point me in the right direction, because I haven't seen the solution to this problem on the internet yet.

FireChair
  • 3
  • 1

2 Answers2

5

The issue here is, that you retrieve the content from the JTextFields at the wrong time. Currently you call getText() right when the components are being initialized. Of course, the content returned from getText() will be an empty String.

So to fix the issue in your logic, you should actually retrieve the majorText and minorText from the JTextFields only once your JButton has been pressed. Because at that point in time, when your button is pressed, the content of your text fields should be correct. To do that, move the retrieval of the text to the ActionListener.

The updated ActionListener should look as follows:

btnButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String majorText = text1.getText();
        String minorText = text2.getText();
        System.out.println(majorText);
        System.out.println(minorText);
    }
}

or simply:

btnButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println(text1.getText());
        System.out.println(text2.getText());
    }
}

Sidenote (as also mentioned by the other answer):

Using null layout is highly discouraged, as it is a frequent source for unnecessary errors. Have a look at the different LayoutManagers available and how and when to use them.

maloomeister
  • 2,461
  • 1
  • 12
  • 21
2

There are some improvements to do in your code:

  1. Avoid the use of null-layout and setBounds(...), see why should you avoid it and a graphic example along with a suggestion to fix it by using layout managers

  2. Your majorText is getting the text BEFORE you click on the button, and by that time it's empty and never update it, you need to get the text on button click, so move this line:

    String majorText = text1.getText();
    

    Inside your actionListener, same thing for minorText

And if you tagged your question with Java 8, then you could rewrite your listener using Java 8 lambdas

btnButton.addActionListener(e -> {
    String majorText = text1.getText();
    String minorText = text1.getText();
    System.out.println(majorText);
    System.out.println(minorText);
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89