java.awt.event.FocusListener Java Examples

The following examples show how to use java.awt.event.FocusListener. 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: TreeTableCellEditorImpl.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
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 #2
Source File: bug4624207.java    From openjdk-8 with 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 #3
Source File: bug4624207.java    From openjdk-jdk8u with 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 File: bug4624207.java    From openjdk-jdk8u-backup with 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 File: bug4624207.java    From openjdk-jdk9 with 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 File: bug4624207.java    From jdk8u60 with 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 File: bug4624207.java    From hottub with 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 #8
Source File: A11YUtil.java    From netbeans with 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
Source File: GhidraFileChooserTest.java    From ghidra with 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 #10
Source File: FixDuplicateImportStmts.java    From netbeans with 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 #11
Source File: bug4624207.java    From jdk8u-jdk with 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
Source File: BoundAssociation.java    From attic-polygene-java with 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 #13
Source File: ResolveDeclarationsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initComponentsMore() {
    contentPanel.setLayout(new GridBagLayout());
    contentPanel.setBackground(UIManager.getColor("Table.background")); //NOI18N
    
    int row = 0;
    combos = new ArrayList<>(items.size());

    Font monoSpaced = new Font("Monospaced", Font.PLAIN, new JLabel().getFont().getSize()); //NOI18N
    FocusListener focusListener = new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            Component c = e.getComponent();
            Rectangle r = c.getBounds();
            contentPanel.scrollRectToVisible(r);
        }

    };
    for (int i = 0; i < items.size(); i++) {
        ResolveDeclarationItem item = items.get(i);
        JComboBox jComboBox = createComboBox(item, monoSpaced, focusListener);
        combos.add(jComboBox);

        JLabel lblSimpleName = new JLabel(item.getName());
        lblSimpleName.setOpaque(false);
        lblSimpleName.setFont(monoSpaced);
        lblSimpleName.setLabelFor(jComboBox);

        contentPanel.add(lblSimpleName, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 5, 2, 5), 0, 0));
        contentPanel.add(jComboBox, new GridBagConstraints(1, row++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 2, 5), 0, 0));
    }

    contentPanel.add(new JLabel(), new GridBagConstraints(2, row, 2, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

    Dimension d = contentPanel.getPreferredSize();
    d.height = getRowHeight() * Math.min(combos.size(), 6);

}
 
Example #14
Source File: AbstractEditorTest.java    From ghidra with 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
Source File: ActivityForm.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public void unselectOthers() {
    Component[] components = getParent().getComponents();
    for (Component component : components) {
        if ((component instanceof FocusListener) && !Objects.equals(component, this)) {
            ((FocusListener) component).focusLost(new FocusEvent(this, FocusEvent.FOCUS_GAINED));
        }
    }
}
 
Example #16
Source File: ComponentOperator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps {@code Component.removeFocusListener(FocusListener)} through queue
 */
public void removeFocusListener(final FocusListener focusListener) {
    runMapping(new MapVoidAction("removeFocusListener") {
        @Override
        public void map() {
            getSource().removeFocusListener(focusListener);
        }
    });
}
 
Example #17
Source File: IconPanel.java    From netbeans with 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 #18
Source File: ButtonPanel.java    From netbeans with 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 #19
Source File: UIUtils.java    From netbeans with 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 #20
Source File: UIUtils.java    From netbeans with 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 #21
Source File: ButtonPanel.java    From netbeans with 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 #22
Source File: SplitterFrame.java    From ios-image-util with 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 #23
Source File: SectionInnerPanel.java    From netbeans with 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);
    }
}
 
Example #24
Source File: CollapsibleSectionPanel.java    From netbeans with 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 #25
Source File: WeakListenerImpl.java    From netbeans with 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 #26
Source File: WeakListenerImpl.java    From netbeans with 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 #27
Source File: BaseSectionNode.java    From netbeans with 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 #28
Source File: SectionInnerPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addFocusListenerRecursively(Container container, FocusListener focusListener) {
    final Component[] components = container.getComponents();
    for (int i = 0; i < components.length; i++) {
        Component component = components[i];
        if (component.isFocusable() && !(component instanceof JLabel)) {
            component.addFocusListener(focusListener);
        }
        if (component instanceof Container) {
            if (!(component instanceof SectionNodePanel)) {
                addFocusListenerRecursively((Container) component, focusListener);
            }
        }
    }
}
 
Example #29
Source File: ImportChooserInnerPanel.java    From netbeans with 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 #30
Source File: CalendarEditorPanel.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
@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();
}