Java Code Examples for javax.swing.JTextField#getKeymap()

The following examples show how to use javax.swing.JTextField#getKeymap() . 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 File: GUIMain.java    From PacketProxy with Apache License 2.0 4 votes vote down vote up
/**
 * JTextPane上でCommand+Cとかでコピペをできるようにする
 */
private void addShortcutForMac() {
	if (!PacketProxyUtility.getInstance().isMac()) {
		return;
	}
	JPanel p = (JPanel) getContentPane();
	InputMap im = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
	ActionMap am = p.getActionMap();
	int hotkey = (KeyEvent.CTRL_MASK | KeyEvent.META_MASK);
	registerTabShortcut(KeyEvent.VK_H, hotkey, im, am, Panes.HISTORY.ordinal());
	registerTabShortcut(KeyEvent.VK_I, hotkey, im, am, Panes.INTERCEPT.ordinal());
	registerTabShortcut(KeyEvent.VK_R, hotkey, im, am, Panes.REPEATER.ordinal());
	registerTabShortcut(KeyEvent.VK_B, hotkey, im, am, Panes.BULKSENDER.ordinal());
	registerTabShortcut(KeyEvent.VK_O, hotkey, im, am, Panes.OPTIONS.ordinal());
	registerTabShortcut(KeyEvent.VK_L, hotkey, im, am, Panes.LOG.ordinal());

	JTextComponent.KeyBinding[] bindings1 = {
		new JTextComponent.KeyBinding(
				KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
				DefaultEditorKit.copyAction),
		new JTextComponent.KeyBinding(
				KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
				DefaultEditorKit.pasteAction),
		new JTextComponent.KeyBinding(
				KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
				DefaultEditorKit.cutAction),
		new JTextComponent.KeyBinding(
				KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
				DefaultEditorKit.selectAllAction),
	};

	JTextPane component_tp = new JTextPane();
	Keymap keymap_tp = component_tp.getKeymap();
	JTextComponent.loadKeymap(keymap_tp, bindings1, component_tp.getActions());

	JTextField component_tf = new JTextField();
	Keymap keymap_tf = component_tf.getKeymap();
	JTextComponent.loadKeymap(keymap_tf, bindings1, component_tf.getActions());

	JTextArea component_ta = new JTextArea();
	Keymap keymap_ta = component_ta.getKeymap();
	JTextComponent.loadKeymap(keymap_ta, bindings1, component_ta.getActions());
}
 
Example 2
Source File: NotifyDescriptor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Make a component representing the input line.
* @param text a label for the input line
* @return the component
*/
protected Component createDesign(final String text) {
    JPanel panel = new JPanel();
    panel.setOpaque (false);

    JLabel textLabel = new JLabel();
    Mnemonics.setLocalizedText(textLabel, text);

    boolean longText = text.length () > 80;
    textField = new JTextField(25);
    textLabel.setLabelFor(textField);
    
    textField.requestFocus();
    
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);
    if (longText) {
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(32, 32, 32))
                    .addComponent(textField))
                .addContainerGap())
        );
    } else {
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textField, GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                .addContainerGap())
        );
    }
    if (longText) {
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    } else {
        layout.setVerticalGroup(
                    layout.createParallelGroup(Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(textLabel)
                            .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
    }

    javax.swing.KeyStroke enter = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0);
    javax.swing.text.Keymap map = textField.getKeymap();

    map.removeKeyStrokeBinding(enter);

    /*

          textField.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
    System.out.println("action: " + evt);
                InputLine.this.setValue (OK_OPTION);
              }
            }
          );
    */
    panel.getAccessibleContext().setAccessibleDescription(
        NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputPanel")
    );
    textField.getAccessibleContext().setAccessibleDescription(
        NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputField")
    );
    
    return panel;
}