Search code examples
javaawtgettextsettext

I want to swap text TextFields


I tried to swap text of TextField But Only one TextField was changed other one stayed same :/,

My code is

import java.awt.*;
import java.awt.event.*;

        class mywindowclosing extends WindowAdapter {
                public void windowClosing(WindowEvent we) {
                        System.exit(0);
                }       
        }
        class Toggle extends Frame implements ActionListener {
                Label lb1, lb2;
                TextField txt1, txt2;
                Button bt1, bt2, bt3, bt4,bt5;
                public Toggle(){
                    super(".::Assignment::.");
                    setBounds(200,200,400,210);
                    setBackground(new Color(75,0,130));
                    setVisible(true);
                    setLayout(new FlowLayout(FlowLayout.LEFT));
                    //First Row
                    lb1=new Label("Label 1 ");
                    add(lb1);
                    txt1=new TextField(30);
                    add(txt1);
                    bt1=new Button("Button 1");
                    add(bt1);
                    bt1.addActionListener(this);
                    //2nd Row
                    lb2=new Label("Label 2 ");
                    add(lb2);
                    txt2=new TextField(30);
                    add(txt2);
                    bt2=new Button("Button 2");
                    add(bt2);
                    bt2.addActionListener(this);
                    //3rd Row
                    setLayout(new FlowLayout(FlowLayout.CENTER));
                    bt3=new Button("Clear");
                    add(bt3);
                    bt3.addActionListener(this);
                    bt4=new Button("Toggle");
                    add(bt4);
                    bt4.addActionListener(this);
                    bt5=new Button("Exit");
                    add(bt5);
                    bt5.addActionListener(this);
                    addWindowListener(new mywindowclosing());
                }

                public void actionPerformed(ActionEvent ae) {
                        if(ae.getSource()==bt1) {
                        txt1.setText("Button 1 pressed");
                        }
                        if(ae.getSource()==bt2) {
                        txt2.setText("Button 2 pressed");
                        }
                        if(ae.getSource()==bt3) {
                        txt1.setText("");
                        txt2.setText("");
                        }
                        if(ae.getSource()==bt4) {
                        txt1.setText(txt2.getText());
                        txt2.setText(txt1.getText());
                        }
                        if(ae.getSource()==bt5) {
                        System.exit(0);
                        }
                }       
        }       
        public class Toggler {
            public static void main(String args[]) {
                new Toggle();
            }
        }

Solution

  • For your code below:

      if(ae.getSource()==bt4) {
                    txt1.setText(txt2.getText());
                    txt2.setText(txt1.getText());
                    }
    

    After doing this, the text in txt1 (TextField ) and txt2 (TextField ) will be the same.

    If you would like to swap the text between them, you need to use a temporary variable.

    Have a try with this:

        if (ae.getSource() == bt4) {
    
            String saveText1 = txt1.getText();
            txt1.setText(txt2.getText());
            txt2.setText(saveText1);
        }