1

I have more textfields in Java.When I press CTRL + A I want all the textfields to get selected. I tried this way, but only the textfield which has the focus gets selected.

Can you help me, please?

class Main{

public JTextField[] textFields;

private TextFieldSelected(int byteCount) {

    textFields = new JTextField[byteCount];
    for (int i = 0; i < textFields.length; i++) {
        textFields[i] = new JTextField(3);

    }

private class CtrlAAdapter extends KeyAdapter {

    public void keyPressed(KeyEvent e) {
        //JTextField textField = (JTextField) e.getComponent();

        if(KeyEvent.VK_CONTROL == e.getKeyCode() && e.getModifiers() == 0)
            if(KeyEvent.VK_A == e.getKeyCode() && e.getModifiers() ==0)

                new SelectAllListener();}}


            //  for(int i=0; i < textFields.length; i++) textFields[i].selectAll();

         class SelectAllListener implements ActionListener {  

             public void actionPerformed(ActionEvent ev) {  
                 for(int i=0; i < textFields.length; i++){
                     //JTextField field = textFields[i];
                    // String text=textFields[i].getText();
                    // textArea.append(text);
                     textFields[i].selectAll();  
                 }



         }
//      reset();
        //  e.consume();
         }

}

I change it, but still doesn`t work :

for (int i = 0; i < textFields.length; i++) {
         JTextField textField = textFields[i];
        textField.getDocument().addDocumentListener(documentListener);
        KeyBindings keybindings = new KeyBindings(textField);


    } 

class KeyBindings {

    public KeyBindings(JTextField textField){
        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                for(int i=0;i < textFields.length; i++){
                    JTextField textField = textFields[i];


                    for(int j=0;j < textFields.length; j++){

                            textFields[j].selectAll();
                            textFields[j].transferFocus();
                            }
                }
            }
        };

        String keyStrokeAndKey = "control A";
        KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
        textField.getInputMap().put(keyStroke, keyStrokeAndKey);
        textField.getActionMap().put(keyStrokeAndKey, action);

    }
}

and how can I make the unfocused fields display the selection too?

  • The same question I asked before , Here is the answer click this link The same question I asked before , Here is the answer click [This Link][1] [1]: http://stackoverflow.com/questions/13979667/how-to-add-shortcut-keys-to-jtextfield – Azad Mar 15 '13 at 12:05
  • I don`t undrestand what your example does... –  Mar 15 '13 at 12:13

1 Answers1

1

KeyListener has a wonderful restriction. The component it is registered with must have focus and be focasable before it will receive key events

In order to receive this, you should use the key bindings API

Basically, register a KeyStroke with the container containing the text fields, be sure to use WHEN_IN_FOCUSED_WINDOW. Walk the component list, looking for JTextField and call selectAll on the fields

The other problem you may have, is the unfocused fields may not display the selection

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I tried in another way : for (int i = 0; i < textFields.length; i++) { JTextField textField = textFields[i]; textField.getDocument().addDocumentListener(documentListener); textField.getActionMap().put("foo", new AbstractAction() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < textFields.length; i++) {JTextField tf = textFields[i]; tf.selectAll(); }}});InputMap inputMap =textField.getInputMap(); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK), "foo";}} –  Mar 15 '13 at 14:33
  • and how can I make the unfocused fields display the selection too? –  Mar 15 '13 at 14:37
  • The only thing I can think of is, use something like a JTextPane set to a single row, but this is going to require a DocuemntFilter to strip out new line characters – MadProgrammer Mar 15 '13 at 21:11