Advertisement
RupeshAcharya60

JTextField with ActionLIstner

Mar 24th, 2023 (edited)
771
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | Source Code | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. class TextFieldExample implements ActionListener {
  7.     JFrame frame;
  8.     JTextField text1;
  9.     JLabel label1;
  10.  
  11.     TextFieldExample(){
  12.         frame = new JFrame();
  13.         text1 = new JTextField(20);
  14.         frame.setLayout(new FlowLayout());
  15.         label1 = new JLabel("working");
  16.  
  17. //register the actionListner to textField
  18.         text1.addActionListener(this);
  19.         frame.add(text1);
  20.         frame.add(label1);
  21.         frame.setSize(300,300);
  22.         frame.setVisible(true);
  23.     }
  24.  
  25.  
  26. //This method is called when user press "enter" or "return" keyword is textField
  27.     public void actionPerformed(ActionEvent e) {
  28.         System.out.println(text1.getText());
  29.         label1.setText("The typed text is:"+text1.getText());
  30.     }
  31. }
  32.  
  33. public class GUI{
  34.  
  35.         public static void main(String[] args){
  36.  
  37.             new TextFieldExample();
  38.  
  39.         }
  40.  
  41. }
  42.  
  43.  
Advertisement
Comments
  • RupeshAcharya60
    1 year (edited)
    # Java 0.12 KB | 0 0
    1. In this program actionPerformed(ActionEvent e) method is called when user press "enter" or "return" keyword in TextField.
Add Comment
Please, Sign In to add comment
Advertisement