jtextfield - Arrow Keys, TextField focus in java? -
I have Now, I want to use my arrow key to focus from this jtextfield to second. How can I do this? Code (Extracted from the reply to OP) You can create an array on all your JTextFields and a KeyAdapter Panel If the entered key is the right key, set the focus on the next JTextField in the array. If this is the left key, set the focus on the previous JTextField. Consider the difference between preincrement and postininkment. JPanel 16
in jtextfield (4x4), I I am using them for loop.
Private JTextField [] characters; Public Zero Drawing () {pnlWords.removeAll (); pnlWords.setLayout (new grid layout (4, 4)); Character = new JTextField [4 * 4]; For (int i = 0; i <4 * 4; i ++) {character [i] = new JTextField (); Character [i] .setHorizontalAlignment (JLabel.CENTER); Character [i] .setBackground (Color.LIGHT_GRAY); Character [i] .setFont (font); Characters [i] .addKeyListener (new Java.Awt.event.KeyAdapter () {Public Zero Key was typed (java.awt.event.KeyEvent evt) {lettersKeyTyped (evt);}}); PnlWords.add (letter [i]); }} Public Zero Character Insect (java.awt.event.KeyEvent evt) {int key = evt.getKeyCode (); For (int i = 0; i <4 * 4; i ++) {switch (key) {case of event VK_LEFT: Characters [i - 1] .requestFocus (); break; CaseyWell VK_RiGHT: Characters [i + 1] .requestFocus (); break; Case event. VK_UP: Characters [i-4] .requestFocus (); break; Case KeyEvent.VK_DOWN: Characters [i + 4] .requestFocus (); break; }}}}
Comments
Post a Comment