Java Code Examples for java.awt.event.FocusListener

The following examples show how to use java.awt.event.FocusListener. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: attic-polygene-java   Source File: BoundAssociation.java    License: Apache License 2.0 6 votes vote down vote up
public void use( Association<T> actualAssociation )
{
    actual = actualAssociation;
    T value = null;
    if( actualAssociation != null )
    {
        value = actualAssociation.get();
    }
    stateModel.use( value );
    for( JComponent component : components )
    {
        SwingAdapter adapter = adapters.get( component.getClass() );
        adapter.fromAssociationToSwing( component, actualAssociation );
        for( FocusListener listener : component.getFocusListeners() )
        {
            if( AssociationFocusLostListener.class.isInstance( listener ) )
            {
                ( (AssociationFocusLostListener) listener ).use( adapter, actual );
            }
        }
    }
}
 
Example 2
Source Project: ghidra   Source File: GhidraFileChooserTest.java    License: Apache License 2.0 6 votes vote down vote up
private void removeFocusIssuesInBatchMode() {

		// install our own custom visibility listener for debugging
		DropDownSelectionTextField<?> field = getFilenameTextField();
		setInstanceField("windowVisibilityListener", field, spy);

		if (!BATCH_MODE) {
			// Batch mode has focus issue when running in parallel.  In this case, update
			// the drop-down field to disable closing the popup window during focus changes.  By
			// only doing this in batch mode, the test can still be run by a developer with
			// the normal behavior.		
			return;
		}

		FocusListener[] focusListeners = field.getFocusListeners();
		for (FocusListener l : focusListeners) {
			field.removeFocusListener(l);
		}
	}
 
Example 3
Source Project: hottub   Source File: bug4624207.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 4
Source Project: jdk8u60   Source File: bug4624207.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 5
Source Project: openjdk-jdk8u   Source File: bug4624207.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 6
Source Project: openjdk-8   Source File: bug4624207.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 7
Source Project: netbeans   Source File: FixDuplicateImportStmts.java    License: Apache License 2.0 6 votes vote down vote up
private JComboBox createComboBox(DataItem item, Font font, FocusListener listener) {
    List<ItemVariant> variants = item.getVariants();
    JComboBox combo = new JComboBox(variants.toArray());
    combo.setSelectedItem(item.getDefaultVariant());
    combo.getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_Combo_ACSD")); //NOI18N
    combo.getAccessibleContext().setAccessibleName(getBundleString("FixDupImportStmts_Combo_Name_ACSD")); //NOI18N
    combo.setOpaque(false);
    combo.setFont(font);
    combo.addFocusListener(listener);
    combo.setEnabled(variants.size() > 1);
    combo.setRenderer(new DelegatingRenderer(combo.getRenderer(), variants, item.getVariantIcons()));
    InputMap inputMap = combo.getInputMap(JComboBox.WHEN_FOCUSED);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "showPopup"); //NOI18N
    combo.getActionMap().put("showPopup", new TogglePopupAction()); //NOI18N
    return combo;
}
 
Example 8
Source Project: netbeans   Source File: A11YUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get universal Focus listener suitable for decsription JTextFields only.
 * It provides screen reades support for read only enabled descriptions.
 */
public static synchronized FocusListener getA11YJTextFieldSupport() {

    if (flis == null) {
        flis = new java.awt.event.FocusListener() {
            public void focusGained(java.awt.event.FocusEvent e) {
                if (e.getComponent() instanceof javax.swing.JTextField) {
                    ((javax.swing.JTextField)e.getComponent()).selectAll();
                }
            }
            public void focusLost(java.awt.event.FocusEvent e) {
                if (e.getComponent() instanceof javax.swing.JTextField) {
                    ((javax.swing.JTextField)e.getComponent()).select(1,1);
                }
            }
        };
    }
    return flis;
}
 
Example 9
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 10
Source Project: openjdk-jdk9   Source File: bug4624207.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 11
Source Project: jdk8u-jdk   Source File: bug4624207.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createAndShowGUI() {
    tab = new JTabbedPane();
    tab.add("Tab1", new JButton("Button1"));
    tab.add("Tab2", new JButton("Button2"));
    tab.setMnemonicAt(0, KeyEvent.VK_T);
    tab.setMnemonicAt(1, KeyEvent.VK_B);

    JFrame frame = new JFrame();
    frame.getContentPane().add(tab, BorderLayout.CENTER);
    txtField = new JTextField();
    frame.getContentPane().add(txtField, BorderLayout.NORTH);
    listener = new bug4624207();
    txtField.addFocusListener((FocusListener) listener);
    frame.pack();
    frame.setVisible(true);
}
 
Example 12
private static Runnable createOnFocusGained(final JTextComponent textComponent, final Runnable onFocusGained) {
  final FocusListener focusListener = new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent arg0) {
      super.focusGained(arg0);
      SwingUtilities.invokeLater(onFocusGained);
      textComponent.removeFocusListener(this);
    }
  };
  textComponent.addFocusListener(focusListener);
  return new Runnable() {
    @Override
    public void run() {
      textComponent.requestFocus();
    }
  };
}
 
Example 13
Source Project: ghidra   Source File: AbstractDockingTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Simulates a focus event on the given component
 *
 * @param component the component upon which to trigger focus
 */
private static void triggerFocusGained(Component component) {
	FocusListener[] listeners = component.getFocusListeners();
	FocusEvent e = new FocusEvent(component, (int) System.currentTimeMillis());
	runSwing(() -> {
		for (FocusListener l : listeners) {
			l.focusGained(e);
		}
	});
}
 
Example 14
Source Project: ghidra   Source File: AbstractEditorTest.java    License: Apache License 2.0 5 votes vote down vote up
private void removeFocusListener(Object editor) {

		if (editor == null) {
			return;
		}

		Component c = null;
		if (editor instanceof Component) {
			c = (Component) editor;
		}

		if (editor instanceof DefaultCellEditor) {
			c = ((DefaultCellEditor) editor).getComponent();
		}

		if (c == null) {
			return;
		}

		FocusListener[] focusListeners = c.getFocusListeners();
		for (FocusListener focusListener : focusListeners) {
			Class<? extends FocusListener> clazz = focusListener.getClass();
			String name = clazz.getName();
			if (name.contains("ghidra")) {
				c.removeFocusListener(focusListener);
			}

		}
	}
 
Example 15
@Override
public synchronized void addFocusListener(FocusListener l) {
	// workaround for java bug #6433257
	for (Component c : getComponents()) {
		c.addFocusListener(l);
	}
}
 
Example 16
Source Project: netbeans   Source File: FixDuplicateImportStmts.java    License: Apache License 2.0 5 votes vote down vote up
private JComboBox createComboBox(CandidateDescription[] choices, CandidateDescription defaultValue, Font font, FocusListener listener ) {
    JComboBox combo = new JComboBox(choices);
    combo.setSelectedItem(defaultValue);
    combo.getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_Combo_ACSD")); //NOI18N
    combo.getAccessibleContext().setAccessibleName(getBundleString("FixDupImportStmts_Combo_Name_ACSD")); //NOI18N
    combo.setOpaque(false);
    combo.setFont( font );
    combo.addFocusListener( listener );
    combo.setEnabled( choices.length > 1 );
    combo.setRenderer( new DelegatingRenderer(combo.getRenderer()));
    InputMap inputMap = combo.getInputMap( JComboBox.WHEN_FOCUSED );
    inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_SPACE, 0), "showPopup" ); //NOI18N
    combo.getActionMap().put( "showPopup", new TogglePopupAction() ); //NOI18N
    return combo;
}
 
Example 17
Source Project: netbeans   Source File: IconPanel.java    License: Apache License 2.0 5 votes vote down vote up
public void addFocusListener(FocusListener fl) {
    if (comp != null) {
        comp.addFocusListener(fl);
    } else {
        super.addFocusListener(fl);
    }
}
 
Example 18
Source Project: netbeans   Source File: IconPanel.java    License: Apache License 2.0 5 votes vote down vote up
public void removeFocusListener(FocusListener fl) {
    if (comp != null) {
        comp.removeFocusListener(fl);
    } else {
        super.removeFocusListener(fl);
    }
}
 
Example 19
Source Project: netbeans   Source File: ButtonPanel.java    License: Apache License 2.0 5 votes vote down vote up
/** Overridden to proxy adds to the custom editor button and the
 * installed component */
public void addFocusListener(FocusListener l) {
    if (comp != null) {
        button.addFocusListener(l);
        comp.addFocusListener(l);
    }
}
 
Example 20
Source Project: netbeans   Source File: ButtonPanel.java    License: Apache License 2.0 5 votes vote down vote up
/** Overridden to proxy removes to the custom editor button and the
 * installed component */
public void removeFocusListener(FocusListener l) {
    if (comp != null) {
        button.removeFocusListener(l);
        comp.removeFocusListener(l);
    }
}
 
Example 21
Source Project: ios-image-util   Source File: SplitterFrame.java    License: MIT License 5 votes vote down vote up
/**
 * Initialize text field to size box.
 *
 * @param tooltipText tooltip text
 * @param focusListener
 * @param documentListener
 * @return
 */
private JTextField createSizeText(String tooltipText, FocusListener focusListener, DocumentListener documentListener) {
	JTextField textField = new JTextField("", 5);
	Insets insets = new Insets(2, 2, 2, 4);
	textField.setToolTipText(tooltipText);
	textField.setHorizontalAlignment(JTextField.RIGHT);
	textField.setMargin(insets);
	textField.addFocusListener(focusListener);
	textField.getDocument().addDocumentListener(documentListener);
	return textField;
}
 
Example 22
Source Project: netbeans   Source File: WeakListenerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** Delegates to the original listener.
*/
@Override public void focusGained(FocusEvent ev) {
    FocusListener l = (FocusListener) super.get(ev);

    if (l != null) {
        l.focusGained(ev);
    }
}
 
Example 23
Source Project: netbeans   Source File: WeakListenerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** Delegates to the original listener.
*/
@Override public void focusLost(FocusEvent ev) {
    FocusListener l = (FocusListener) super.get(ev);

    if (l != null) {
        l.focusLost(ev);
    }
}
 
Example 24
Source Project: netbeans   Source File: ImportChooserInnerPanel.java    License: Apache License 2.0 5 votes vote down vote up
private JComboBox createComboBox( String[] choices, String defaultValue, Icon[] icons, Font font, FocusListener listener ) {
    JComboBox combo = new JComboBox(choices);
    combo.setSelectedItem(defaultValue);
    combo.getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_Combo_ACSD")); //NOI18N
    combo.getAccessibleContext().setAccessibleName(getBundleString("FixDupImportStmts_Combo_Name_ACSD")); //NOI18N
    combo.setOpaque(false);
    combo.setFont( font );
    combo.addFocusListener( listener );
    combo.setEnabled( choices.length > 1 );
    combo.setRenderer( new DelegatingRenderer(combo.getRenderer(), choices, icons ) );
    InputMap inputMap = combo.getInputMap( JComboBox.WHEN_FOCUSED );
    inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_SPACE, 0), "showPopup" ); //NOI18N
    combo.getActionMap().put( "showPopup", new TogglePopupAction() ); //NOI18N
    return combo;
}
 
Example 25
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column) {
  TableCellRenderer renderer = table.getCellRenderer(row, column);
  Component c = renderer.getTableCellRendererComponent(table, value, isSelected, true, row, column);
  if (c != null) {
    myEditor.getJComponent().setOpaque(true);
    myEditor.getJComponent().setBackground(c.getBackground());
    if (c instanceof JComponent) {
      myEditor.getJComponent().setBorder(((JComponent) c).getBorder());
    }
  } else {
    myEditor.getJComponent().setOpaque(false);
  }
  myOption.setValue((Color)value, this);
  final FocusListener onStartEditing = new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      myEditor.getJComponent().removeFocusListener(this);
      myEditor.openChooser();
    }
  };
  myEditor.getJComponent().addFocusListener(onStartEditing);
  myEditor.setOnCancelCallback(new Runnable() {
    @Override
    public void run() {
      cancelCellEditing();
      myEditor.getJComponent().removeFocusListener(onStartEditing);
      moveFocusToTable(row);
    }
  });
  myEditor.setOnOkCallback(new Runnable() {
    @Override
    public void run() {
      myEditor.getJComponent().removeFocusListener(onStartEditing);
      moveFocusToTable(row);
    }
  });
  return myEditor.getJComponent();
}
 
Example 26
Source Project: netbeans   Source File: BaseSectionNode.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Hack: I need to disable the focus listener for the title button, but cannot
 * do that via SectionNodePanel constructor because the parameter is blocked.
 */
protected void disableTitleButtonFocusListener(SectionNodePanel nodePanel) {
    JButton titleButton = getTitleButton(nodePanel);
    if(titleButton != null) {
        FocusListener [] listeners = titleButton.getFocusListeners();
        if(listeners != null && listeners.length == 2) {
            titleButton.removeFocusListener(listeners[1]);
        }
    }
}
 
Example 27
Source Project: netbeans   Source File: CollapsibleSectionPanel.java    License: Apache License 2.0 5 votes vote down vote up
public ActionsBuilder (JPanel panel, FocusListener listener) {
    this.focusListener = listener;
    panel.removeAll();
    GroupLayout layout = (GroupLayout) panel.getLayout();
    horizontalSeqGroup = layout.createSequentialGroup();
    layout.setHorizontalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(horizontalSeqGroup)
    );
    verticalParallelGroup = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(verticalParallelGroup)
    );
}
 
Example 28
Source Project: netbeans   Source File: UIUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void keepFocusedComponentVisible(Component component, JComponent container) {
    FocusListener listener;
    if(component instanceof JComponent ) {
        listener = getNotShowingFieldsFocusListener(container);
    } else {
        listener = getScrollingFocusListener(); // legacy fallback
    }
    keepFocusedComponentVisible(component, listener);
}
 
Example 29
Source Project: netbeans   Source File: UIUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static FocusListener getNotShowingFieldsFocusListener(JComponent container) {
    String key = "notShowingFieldFocusListener";                            // NOI18N
    Object l = container.getClientProperty(key);
    if (l == null) {
        l = new NotShowingFieldsFocusListener(container);
        container.putClientProperty(key, l);
    }
    return (FocusListener) l;
}
 
Example 30
Source Project: netbeans   Source File: SectionInnerPanel.java    License: Apache License 2.0 5 votes vote down vote up
public synchronized void addFocusListener(FocusListener l) {
    super.addFocusListener(l);
    if (!localFocusListenerInitialized) {
        localFocusListenerInitialized = true;
        Container container = this;
        FocusListener focusListener = localFocusListener;
        addFocusListenerRecursively(container, focusListener);
    }
}