Search code examples
javajpanelfocusactionjtextfield

Focused JTextField prevents access to ActionMap of JPanel


The code below can trigger two actions:

- the "Paste" action triggered by the "Paste" JButton or "Ctrl V" KeyStroke.
- the "Close" action triggered by the "Close" JButton or "Esc" KeyStroke.

Each action will work if :

- you do not select a JTextField and you click the JButton.
- you do not select a JTextField and you use the Keystroke.
- you select a JTextField and you click the JButton.

If the content of two cells of a spreasheet is copied, the "Paste" action will paste the content of each cell in the corresponding JTextField.
The "Close" action will close the JFrame.

However, it will not work if :

- you select a JTextField and you use the Keystroke.

If the content of two cells of a spreasheet is copied, the "Paste" action will paste the content of all cells in the selected JTextField.
The "Close" action will not close the JFrame.
How can I solve this ?

I tried many things, given for instance at How to UnFocus a JTextField.
I am missing something since none of them worked.

Thanks in advance for your answers.

Here is the code :

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

public class Test extends JFrame implements ActionListener {

    private static JTextField[] A;
    private static JButton[] B;

    public static void main(String[] args) {
        Test F = new Test();
        JPanel H = new JPanel();
        F.setContentPane(H);
        A = new JTextField[2];
        B = new JButton[2];
        B[0] = new JButton("Paste");
        H.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK),"0");
        H.getActionMap().put("0",new AbstractAction() {
            public void actionPerformed(ActionEvent J) {
                B[0].doClick();
            }
        });
        B[1] = new JButton("Close");
        H.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0),"1");
        H.getActionMap().put("1",new AbstractAction() {
            public void actionPerformed(ActionEvent J) {
                B[1].doClick();
            }
        });
        GridBagLayout I = new GridBagLayout();
        H.setLayout(I);
        for (int i=0;i<A.length;i++) {
            A[i] = new JTextField();
            A[i].setHorizontalAlignment(SwingConstants.CENTER);
            A[i].setPreferredSize(new Dimension((int)B[i].getPreferredSize().getWidth(),(int)A[i].getPreferredSize().getHeight()));
            I.setConstraints(A[i],new GridBagConstraints(0,i,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
            H.add(A[i]);
        }
        for (int i=0;i<B.length;i++) {
            B[i].addActionListener(F);
            B[i].setActionCommand(String.valueOf(i));
            I.setConstraints(B[i],new GridBagConstraints(0,A.length+i,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
            H.add(B[i]);
        }
        F.pack();
        F.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        F.setLocationRelativeTo(null);
        F.setTitle("Parameters");
        F.setVisible(true);
    }

    public void actionPerformed(ActionEvent J) {
        if(J.getActionCommand().equals("0")) {
            try {
                if (Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null)!=null && Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor) && A[0].isEditable() && A[1].isEditable()) {
                    Scanner D = new Scanner((String)Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor));
                    A[0].setText(String.valueOf(D.next()));
                    A[1].setText(String.valueOf(D.next()));
                    D.close();
                }
            }
            catch (Throwable K) {
                System.out.println("Paste failed");
            }
        }
        if (J.getActionCommand().equals("1")) {
            this.dispose();
        }
    }

}

Solution

  • The code above doesn't even enter the ActionMap of the JPanel when a JTextField is selected.
    Thus a solution I found is to add the ActionMap of the JPanel to each JTextField by adding:

            A[i].getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK),"0");
            A[i].getActionMap().put(String.valueOf("0"),new AbstractAction() {
                public void actionPerformed(ActionEvent J) {
                    B[0].doClick();
                }
            });
            A[i].getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0),"1");
            A[i].getActionMap().put(String.valueOf("1"),new AbstractAction() {
                public void actionPerformed(ActionEvent J) {
                    B[1].doClick();
                }
            });
    

    after:

    A[i] = new JTextField();