Swapping JTextField contents

1.1k views Asked by At

I have one Jframe which includes two TextFields and I want to exchange two integer numbers after clicking on exchange button.

2

There are 2 answers

0
Uma Kanth On BEST ANSWER

Get the value from one text box and store it in the other.

.getText() is used to get the value from the textbox.

.setText() is used to set the value of a textbox.

private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String temp = jTextField1.getText();
    jtextField1.setText(jTextField2.getText());
    jtextField2.setText(temp);
    
}                                        
// Variables declaration - do not modify                     
private javax.swing.JButton jButton;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
0
dly On

Since JTextFields use Strings this works pretty much everywhere.

String temp = text1.getText();
text1.setText(text2.getText());
text2.setText(temp);