Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Swing Code Examples

Adding KeyListener to gain focus on a componet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Adding KeyListener to gain focus on a componet import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; public class FocusTest extends JFrame implements KeyListener { JTextField jtf1,jtf2,jtf3; JPanel p; public FocusTest() { jtf1 = new JTextField(10); jtf2 = new JTextField(10); jtf3 = new JTextField(10); jtf1.addKeyListener(this); p = new JPanel(); p.add(jtf1); p.add(jtf2); p.add(jtf3); getContentPane().add(p); } public static void main(String dd[]) { FocusTest ft = new FocusTest(); ft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ft.setSize(200,200); ft.setVisible(true); } public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_ENTER) { System.out.println("ll"); jtf3.requestFocus(); } } public void keyReleased(KeyEvent ke) {} public void keyTyped(KeyEvent ke) {} }