Search code examples
javaeclipseswingjlabeljtextfield

JTextField not editing JTextInput


I'm trying to make a simple program where there is a text input field and a label, and when you input something to the field it stores the input as "testInput" and changes the label to that text. I'm using Eclipse's Window Builder plugin for this, and it has worked fine before. What happens now is it will take the text in, but wont send it back out to the jLabel. Here is my code:

package interest;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TestWin {

String testInput;

private JFrame frame;
private JTextField txtTest;

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

/**
 * Create the application.
 */
public TestWin() {
    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);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    frame.getContentPane().setLayout(gridBagLayout);

    txtTest = new JTextField(); 
    //Change label when enter is pressed
    txtTest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            testInput = txtTest.getText();
            txtTest.setText(testInput);
        }
    });
    txtTest.setText("test");
    GridBagConstraints gbc_txtTest = new GridBagConstraints();
    gbc_txtTest.insets = new Insets(0, 0, 5, 0);
    gbc_txtTest.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtTest.gridx = 5;
    gbc_txtTest.gridy = 1;
    frame.getContentPane().add(txtTest, gbc_txtTest);
    txtTest.setColumns(10);

    JLabel lblTest = new JLabel("test");
    GridBagConstraints gbc_lblTest = new GridBagConstraints();
    gbc_lblTest.gridx = 5;
    gbc_lblTest.gridy = 4;
    frame.getContentPane().add(lblTest, gbc_lblTest);
}

}

I'm sure I'm making a really simple mistake, so any help is appreciated!


Solution

  • Put

    JLabel lblTest = new JLabel("test");
    

    at the begining of initialize() (or anywhere above txtTest.addActionListener...), and then replace

    txtTest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            testInput = txtTest.getText();
            txtTest.setText(testInput);
        }
    });
    

    for

    txtTest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            testInput = txtTest.getText();
            lblTest.setText(testInput);    //This line was changed.
        }
    });