Reputation: 31
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);
}
}
Upvotes: 0
Views: 1093
Reputation: 110
This stops the popup menu from grabbing the focus away from the text field: menu.setFocusable(false);
Upvotes: 0