Manish Kumar Sahu
Manish Kumar Sahu

Reputation: 45

JTextField setText()

I was making a GUI to reverse an input string, I am taking input from the first textfield, then reversing the string using my function strRev() then trying to produce the result on the second textfield. But am not getting any output on 2nd textfield.

package com.awt;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;

public class Buttons{

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Buttons window = new Buttons();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public Buttons() {
        initialize();
    }
    
    public String strRev(String s) {
        char a;
        String ans="";
        for(int i=0;i<s.length();i++) {
            a=s.charAt(i);
            ans=a+ans;
        }
        return ans;
    }

    private void initialize() {
        frame = new JFrame("Label Demo");
        frame.setBounds(150, 200, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        
        textField = new JTextField();
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        
        JButton okButton = new JButton("Reverse");
        frame.getContentPane().add(okButton);
        
        textField_1 = new JTextField();
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        
        String str=textField.getText();
        final String ans=strRev(str);
        
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField_1.setText(ans);
            }
        });
        
    }

}

The GUI looks like this:- enter image description here

Please let me know my mistake, Thanks

Upvotes: 1

Views: 1374

Answers (1)

whiplash
whiplash

Reputation: 725

You need to read the contents of textField only when the user clicks on the Reverse button. At the moment, you read the contents when the GUI loads at which time there is nothing in the textField. So calling a reverse on empty string is going to give you an empty string and that is what you see in your textField_1. Move the code where you read the textField into your addActionListener.

private void initialize() {
    frame = new JFrame("Label Demo");
    frame.setBounds(150, 200, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    
    textField = new JTextField();
    frame.getContentPane().add(textField);
    textField.setColumns(10);
    
    JButton okButton = new JButton("Reverse");
    frame.getContentPane().add(okButton);
    
    textField_1 = new JTextField();
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);
    
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // THIS BLOCK IS RUN EVERYTIME THE BUTTON IS CLICKED!
            // Read the contents of textField
            String str=textField.getText();
            // Reverse the string you just read
            final String ans=strRev(str);
            // Set the answer on the other textField.
            textField_1.setText(ans);
        }
    });   
}

Upvotes: 3

Related Questions