Java Code Examples for java.awt.Component#addKeyListener()

The following examples show how to use java.awt.Component#addKeyListener() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: arcusplatform   File: Oculus.java    License: Apache License 2.0 6 votes vote down vote up
public static void invokeOnEnter(Component c, Runnable action) {
   if (c instanceof JTextField) {
      ((JTextField)c).addActionListener((evt) -> action.run());
   } else if (c instanceof AbstractButton) {
      ((AbstractButton)c).addActionListener((evt) -> action.run());
   } else {
      c.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent e) {
            if(e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ENTER) {
               action.run();
            }
         }
      });
   }
}
 
Example 2
private void registerKeyAction(Component c) {
    if (c instanceof ReplaceDialog == false) {
        c.removeKeyListener(this);
        c.addKeyListener(this);
    }

    if (c instanceof Container) {
        Container cnt = (Container) c;
        cnt.removeContainerListener(this);
        cnt.addContainerListener(this);
        Component[] ch = cnt.getComponents();
        for (int i = 0; i < ch.length; i++) {
            registerKeyAction(ch[i]);
        }
    }
}
 
Example 3
Source Project: rest-client   File: EscapableDialog.java    License: Apache License 2.0 6 votes vote down vote up
private void registerKeyAction(Component c) {
    if (c instanceof EscapableDialog == false) {
        c.removeKeyListener(this);
        c.addKeyListener(this);
    }

    if (c instanceof Container) {
        Container cnt = (Container) c;
        cnt.removeContainerListener(this);
        cnt.addContainerListener(this);
        Component[] ch = cnt.getComponents();
        for (int i = 0; i < ch.length; i++) {
            registerKeyAction(ch[i]);
        }
    }
}
 
Example 4
Source Project: jace   File: EditableLabel.java    License: GNU General Public License v2.0 6 votes vote down vote up
public EditableLabel(JLabel label, Component edit, int width, int horizontalPadding, int verticalPadding, Object owner, String property) {
    ownerObject = owner;
    this.width = width;
    objectProperty = property;
    addMouseListener(this);
    edit.addFocusListener(this);
    addFocusListener(this);
    layout = new CardLayout(horizontalPadding, verticalPadding);
    setLayout(layout);
    add(label, cards.label.toString());
    add(edit, cards.edit.toString());
    labelComponent = label;
    editComponent = edit;
    deactivateEdit();
    setBackground(UserInterface.Theme.background.color);
    label.setForeground(UserInterface.Theme.foreground.color);
    label.setOpaque(false);
    edit.setBackground(UserInterface.Theme.backgroundEdit.color);
    edit.setForeground(UserInterface.Theme.foregroundEdit.color);
    edit.setFocusTraversalKeysEnabled(false);
    edit.addKeyListener(NAV_LISTENER);
    label.addKeyListener(NAV_LISTENER);
    this.addKeyListener(NAV_LISTENER);
}
 
Example 5
public ColorPickerPopup() {
    boolean includeOpacity = MultiThumbSliderUI
            .getProperty(GradientSlider.this, "GradientSlider.includeOpacity", "true").equals("true");

    mini = new ColorPicker(false, includeOpacity);
    mini.setMode(ColorPicker.HUE);
    mini.setPreferredSize(new Dimension(220, 200));
    PropertyChangeListener p = evt -> {
        ColorPicker p1 = (ColorPicker) evt.getSource();
        Color[] colors = getColors();
        colors[lastSelectedThumb] = p1.getColor();
        setValues(getThumbPositions(), colors);
    };
    mini.addPropertyChangeListener(ColorPicker.SELECTED_COLOR_PROPERTY, p);
    mini.addPropertyChangeListener(ColorPicker.OPACITY_PROPERTY, p);
    for (int a = 0; a < mini.getComponentCount(); a++) {
        Component c = mini.getComponent(a);
        c.addKeyListener(commitListener);
    }
    add(mini);
}
 
Example 6
protected void disableEnterKey() {
    Component[] components = this.getComponents();
    listener = new EnterKeyListener();
    for (int i = 0; i < components.length; i++) {
        Component component = components[i];
        if (component.isFocusable() && !(component instanceof JLabel)) {
            KeyListener kl = (KeyListener) WeakListeners.create(KeyListener.class, listener,
                    component);
            component.addKeyListener(kl);
        }
    }
}
 
Example 7
private static void createAndShowGUI() {
    f = new JFrame();

    final Component component = new JTextField();
    component.addKeyListener(new MyKeyListener());

    f.add(component);
    f.setSize(300, 300);
    f.setLocationRelativeTo(null);
    f.setAlwaysOnTop(true);
    f.setVisible(true);
}
 
Example 8
Source Project: pumpernickel   File: ModifierTracker.java    License: MIT License 5 votes vote down vote up
private static void track(Component c) {
	c.addKeyListener(keyListener);

	if (c instanceof Container) {
		Container container = (Container) c;
		for (int a = 0; a < container.getComponentCount(); a++) {
			track(container.getComponent(a));
		}
	}
}
 
Example 9
private void set_VK_ESCAPE_KeyListener() {
  // Set VK_ESCAPE KeyEvent listeners
  List<Component> comps =
      BaselineCorrectorSetupDialog.getAllComponents(BaselineCorrectorSetupDialog.this);
  for (Component c : comps) {
    c.addKeyListener(BaselineCorrectorSetupDialog.this.keyListener);
  }
}
 
Example 10
Source Project: ontopia   File: KeyInputManager.java    License: Apache License 2.0 5 votes vote down vote up
private void listenTo(Component component) {
  component.addKeyListener(this);
  if (component instanceof Container) {
    Container container = (Container)component;
    container.addContainerListener(this);
    
    Component components[] = container.getComponents();
    for (int i = 0; i < components.length; i++) {
      Component currentComponent = components[i];
      listenTo(currentComponent);
    }
  }
}
 
Example 11
Source Project: ontopia   File: HighlightNode.java    License: Apache License 2.0 5 votes vote down vote up
private void listenTo(Component component) {
  component.addKeyListener(this);
  if (component instanceof Container) {
    Container container = (Container)component;
    container.addContainerListener(this);
    
    Component components[] = container.getComponents();
    for (int i = 0; i < components.length; i++) {
      Component currentComponent = components[i];
      listenTo(currentComponent);
    }
  }
}
 
Example 12
public void addKeyAndContainerListenerRecursively(final Component c) {
	c.addKeyListener(this);
	if (c instanceof Container) {
		final Container container = (Container) c;
		container.addContainerListener(this);
		final Component[] children = container.getComponents();
		for (int i = 0; i < children.length; i++) {
			addKeyAndContainerListenerRecursively(children[i]);
		}
	}
}
 
Example 13
/**
 * This method will remove the existing keylisteners from the component 'c',
 * tells 'firstKeyListener' to call those key listeners if it has not dealt
 * with the key, and then sets 'firstKeyListener' as the key listener for
 * 'c'
 */
public static void setAsFirstKeyListener(final Component c, final QueueJumpingKeyListener firstKeyListener) {
	final KeyListener[] oldKeyListeners = c.getKeyListeners();
	for (final KeyListener kl : oldKeyListeners) {
		c.removeKeyListener(kl);
	}
	firstKeyListener.addOtherKeyListeners(oldKeyListeners);
	c.addKeyListener(firstKeyListener);
}
 
Example 14
Source Project: settlers-remake   File: GOSwingEventConverter.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a new event converter, that converts swing events to go events.
 * 
 * @param component
 *            The component.
 * @param provider
 *            THe provider to which to send the events.
 */
public GOSwingEventConverter(Component component, GOEventHandlerProvider provider) {
	super(provider);

	component.setFocusTraversalKeysEnabled(false);

	component.addKeyListener(this);
	component.addMouseListener(this);
	component.addMouseMotionListener(this);
	component.addMouseWheelListener(this);
	component.addHierarchyListener(this);

	addReplaceRule(new EventReplacementRule(ReplacableEvent.DRAW, Replacement.COMMAND_SELECT, MOUSE_TIME_TRSHOLD, MOUSE_MOVE_TRESHOLD));
	addReplaceRule(new EventReplacementRule(ReplacableEvent.PAN, Replacement.COMMAND_ACTION, MOUSE_TIME_TRSHOLD, MOUSE_MOVE_TRESHOLD));
}
 
Example 15
/**
 * This method registers a component for the mapped actions.
 *
 * @param component component
 */
public void registerComponent(Component component) {
    component.addKeyListener(this);
    if (component instanceof Container) {
        Component[] comps = ((Container) component).getComponents();
        for (Component comp : comps) {
            registerComponent((comp));
        }
    }
}
 
Example 16
public FindAndReplaceDialog(final AnimationEditor editor) {
	super(editor);
	textEditor = editor;

	final Container root = getContentPane();
	root.setLayout(new GridBagLayout());

	final JPanel text = new JPanel(new GridBagLayout());
	final GridBagConstraints c = new GridBagConstraints();

	c.gridx = c.gridy = 0;
	c.gridwidth = c.gridheight = 1;
	c.weightx = c.weighty = 1;
	c.ipadx = c.ipady = 1;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.anchor = GridBagConstraints.LINE_START;
	searchField = createField("Find Next", text, c, null);
	replaceField = createField("Replace with", text, c, this);

	c.gridwidth = 4;
	c.gridheight = c.gridy;
	c.gridx = c.gridy = 0;
	c.weightx = c.weighty = 1;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.anchor = GridBagConstraints.LINE_START;
	root.add(text, c);

	c.gridy = c.gridheight;
	c.gridwidth = 1;
	c.gridheight = 1;
	c.weightx = 0.001;
	matchCase = createCheckBox("Match Case", root, c);
	regex = createCheckBox("Regex", root, c);
	forward = createCheckBox("Search forward", root, c);
	forward.setSelected(true);
	c.gridx = 0;
	c.gridy++;
	markAll = createCheckBox("Mark All", root, c);
	wholeWord = createCheckBox("Whole Word", root, c);

	c.gridx = 4;
	c.gridy = 0;
	findNext = createButton("Find Next", root, c);
	replace = createButton("Replace", root, c);
	replaceAll = createButton("Replace All", root, c);
	cancel = createButton("Cancel", root, c);
	setResizable(true);
	pack();

	getRootPane().setDefaultButton(findNext);

	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

	final KeyAdapter listener = new KeyAdapter() {

		@Override
		public void keyPressed(final KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ESCAPE) dispose();
		}
	};
	for (final Component component : getContentPane().getComponents())
		component.addKeyListener(listener);
	searchField.addKeyListener(listener);
	replaceField.addKeyListener(listener);
}
 
Example 17
public InputHandler(Component inputSource) {
	this();
	inputSource.addKeyListener(this); //add key listener to game
	//inputSource.addMouseListener(this); //add mouse listener to game (though it's never used)
}
 
Example 18
public void appendListenersTo(Component control){
	control.addKeyListener(this.listener);
}
 
Example 19
@Override
public void bind(Component component) {
    super.bind(component);
    component.addKeyListener(this);
}
 
Example 20
Source Project: DroidUIBuilder   File: HardLayoutPane.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 给除文本组件外的其它加入本面板的组件添加“回车转移焦点到下一组件”的监听器.<br>
 * 注:文本组件的相似监听器已经利用其全局action map在LNF UI中调用实现了.
 * 
 * @param willBeAddTo 要给其添加“回车转移焦点到下一组件”的监听器的组件
 * @see LaunchRoot#spesificTextField()
 * @see TextFocusAction#installTextActionMap()
 */
private void addEnterKeyTransferFocusImplExceptText(Component willBeAddTo)
{
	//文本组件不需要添加了
	if(!(willBeAddTo instanceof javax.swing.text.JTextComponent))
		willBeAddTo.addKeyListener(TransferFocusImpl.SINGLETON);
}