0

Thanks guys for your reply. I'll use codes below(imports ignored) to show my problem. (for short, call the above text field as "field1", call the below text field as "field2") In dilaog, when i input in field1, and click field2, field2 will show text in field1. but if i input in field1, and show its popup menu to copy/paste, I do NOT want to show the text of field1 to field2.

public class MyDialog extends JDialog {
    public MyDialog() {
        final JTextField name = new JTextField(20);
        name.setEditable(true);
        final JTextField clone = new JTextField(20);
        clone.setEditable(true);
        getContentPane().setLayout(new GridLayout(2, 1));
        getContentPane().add(name);
        getContentPane().add(clone);

        name.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                clone.setText(name.getText());
            }
        });
        addPopupMenu(name);
    }

    private void addPopupMenu(final JTextField name) {
        JPopupMenu menu = new JPopupMenu();
        JMenuItem copyItem = menu.add(name.getActionMap().get(DefaultEditorKit.copyAction));
        copyItem.setText("copy");
        JMenuItem pasteItem = menu.add(name.getActionMap().get(DefaultEditorKit.pasteAction));
        pasteItem.setText("paste");
        name.setComponentPopupMenu(menu);
    }

    public static void main(String args[]){
        MyDialog dlg = new MyDialog();
        dlg.setSize(500, 200);
        dlg.setVisible(true);
    }
}
zhang jy
  • 31
  • 1
  • 5
  • for better help sooner edit your question with a [SSCCE](http://sscce.org/) – mKorbel Apr 26 '12 at 15:36
  • I dont think you need to meddle with the focussing of your components, please show us what your problem is and provide a small example of what isnt working for you – Maarten Blokker Apr 26 '12 at 15:45
  • Try [`setComponentPopupMenu()`](http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#setComponentPopupMenu%28javax.swing.JPopupMenu%29). – trashgod Apr 26 '12 at 23:48

1 Answers1

0

This stops the popup menu from grabbing the focus away from the text field: menu.setFocusable(false);

kjohnson
  • 110
  • 1
  • 4