Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

key listener java

package com.javapointers.javase;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class KeyListenerTest implements KeyListener, ActionListener {
 
    JFrame frame;
    JTextField tf;
    JLabel lbl;
    JButton btn;
 
    public KeyListenerTest() {
        frame = new JFrame();
        lbl = new JLabel();
        tf = new JTextField(15);
        tf.addKeyListener(this);
        btn = new JButton("Clear");
        btn.addActionListener(this);
        JPanel panel = new JPanel();
        panel.add(tf);
        panel.add(btn);
 
        frame.setLayout(new BorderLayout());
        frame.add(lbl, BorderLayout.NORTH);
        frame.add(panel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }
 
    @Override
    public void keyTyped(KeyEvent ke) {
        lbl.setText("You have typed "+ke.getKeyChar());
    }
 
    @Override
    public void keyPressed(KeyEvent ke) {
        lbl.setText("You have pressed "+ke.getKeyChar());
    }
 
    @Override
    public void keyReleased(KeyEvent ke) {
        lbl.setText("You have released "+ke.getKeyChar());
    }
 
    @Override
    public void actionPerformed(ActionEvent ae) {
        tf.setText("");
    }
 
    public static void main(String args[]) {
        new KeyListenerTest();
    } 
}
Comment

PREVIOUS NEXT
Code Example
Java :: java random word from list 
Java :: jsonarray to list java 
Java :: java regex replace all characters before 
Java :: java encrypt string 
Java :: change button text onclick java 
Java :: randomize 2d array java 
Java :: java docstring javadoc 
Java :: java example 
Java :: initialize arraylist in 1 line in java 
Java :: parse string to int java 
Java :: spigot send player action bar message 1.8 
Java :: how to convert int into int array of digits in java 
Java :: kapt plugin 
Java :: javafx resizable window 
Java :: java restart program 
Java :: how to get int from string java 
Java :: spring boot docker 
Java :: printing prime numbers in java 
Java :: unprocessed continuation reference(s) remaining name 
Java :: for loop java 
Java :: java list distinct by object attribute 
Java :: getcurrencyinstance java example 
Java :: java insert char in string at index 
Java :: How to generate all possible IP addresses based on a given set of digits, in Java? 
Java :: java download image file 
Java :: java secretkey 
Java :: radio button java swing 
Java :: clone array in java 
Java :: viewpager2 dependency 
Java :: open website from android activity 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =