Java Code Examples for java.awt.event.FocusEvent

The following examples show how to use java.awt.event.FocusEvent. 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
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 2
Source Project: Java8CN   Source File: KeyboardFocusManager.java    License: Apache License 2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 3
Source Project: JDKSourceCode1.8   Source File: KeyboardFocusManager.java    License: MIT License 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 4
public JComponent createGroupComponent(GPOptionGroup group) {
  GPOption<?>[] options = group.getOptions();
  final JComponent optionsPanel = createGroupComponent(group, options);
  if (group.isTitled()) {
    UIUtil.createTitle(optionsPanel, myi18n.getOptionGroupLabel(group));
  }
  JPanel result = new JPanel(new BorderLayout());
  result.add(optionsPanel, BorderLayout.NORTH);
  result.addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      optionsPanel.requestFocus();
    }
  });
  return result;
}
 
Example 5
Source Project: netbeans   Source File: BaseTable.java    License: Apache License 2.0 6 votes vote down vote up
public void focusGained(FocusEvent fe) {
    Component c = fe.getOppositeComponent();

    /*
    //handy for debugging
    System.out.println("Focus gained to " + (fe.getComponent().getName() == null ? fe.getComponent().getClass().getName() : fe.getComponent().getName()) + " temporary: " + fe.isTemporary()
    + " from " + (fe.getOppositeComponent() == null ? "null" :
        (fe.getOppositeComponent().getName() == null ? fe.getOppositeComponent().getClass().getName() : fe.getOppositeComponent().getName()))
    );
     */
    PropUtils.log(BaseTable.class, fe);

    if (!isKnownComponent(c)) {
        fireChange();
    }

    if (!inEditRequest() && !inEditorRemoveRequest() && (fe.getComponent() == this)) {
        //            System.out.println("Painting due to focus gain " + fe.getComponent());
        //            repaint(0,0,getWidth(),getHeight());
        paintSelectionRow();
    }
}
 
Example 6
Source Project: MeteoInfo   Source File: JDayChooser.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * JDayChooser is the FocusListener for all day buttons. (Added by Thomas
 * Schaefer)
 * 
 * @param e
 *            the FocusEvent
 */
/*
 * Code below commented out by Mark Brown on 24 Aug 2004. This code breaks
 * the JDateChooser code by triggering the actionPerformed method on the
 * next day button. This causes the date chosen to always be incremented by
 * one day.
 */
public void focusGained(FocusEvent e) {
	// JButton button = (JButton) e.getSource();
	// String buttonText = button.getText();
	//
	// if ((buttonText != null) && !buttonText.equals("") &&
	// !e.isTemporary()) {
	// actionPerformed(new ActionEvent(e.getSource(), 0, null));
	// }
}
 
Example 7
Source Project: cuba   Source File: TableFocusManager.java    License: Apache License 2.0 6 votes vote down vote up
public void processFocusEvent(FocusEvent e) {
//TODO: CUBA 7 Rework TableFocusManager for Java 9
//https://github.com/cuba-platform/cuba/issues/893
//        if (e.getID() == FocusEvent.FOCUS_GAINED) {
//            if (e instanceof CausedFocusEvent) {
//                if (((CausedFocusEvent) e).getCause() == CausedFocusEvent.Cause.TRAVERSAL_FORWARD) {
//                    if (impl.getModel().getRowCount() > 0) {
//                        // if focus from cell editor
//                        if (e.getSource() == impl && impl.getSelectedRow() >= 0) {
//                            int selectedColumn = impl.getSelectedColumn();
//                            focusTo(impl.getSelectedRow(), selectedColumn >= 0 ? selectedColumn : 0);
//                        } else
//                            moveToStart(0, 0);
//                    } else
//                        impl.transferFocus();
//
//                } else if (((CausedFocusEvent) e).getCause() == CausedFocusEvent.Cause.TRAVERSAL_BACKWARD) {
//                    if (impl.getModel().getRowCount() > 0) {
//                        moveToEnd(impl.getRowCount() - 1, impl.getColumnCount() - 1);
//                    } else
//                        impl.transferFocusBackward();
//                }
//            }
//        }
    }
 
Example 8
Source Project: jaamsim   Source File: GUIFrame.java    License: Apache License 2.0 5 votes vote down vote up
private void addSnapToGridField(JToolBar buttonBar, Insets margin) {

		gridSpacing = new JTextField("1000000 m") {
			@Override
			protected void processFocusEvent(FocusEvent fe) {
				if (fe.getID() == FocusEvent.FOCUS_LOST) {
					GUIFrame.this.setSnapGridSpacing(this.getText().trim());
				}
				else if (fe.getID() == FocusEvent.FOCUS_GAINED) {
					gridSpacing.selectAll();
				}
				super.processFocusEvent( fe );
			}
		};

		gridSpacing.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				GUIFrame.this.setSnapGridSpacing(gridSpacing.getText().trim());
				controlStartResume.requestFocusInWindow();
			}
		});

		gridSpacing.setMaximumSize(gridSpacing.getPreferredSize());
		int hght = snapToGrid.getPreferredSize().height;
		gridSpacing.setPreferredSize(new Dimension(gridSpacing.getPreferredSize().width, hght));

		gridSpacing.setHorizontalAlignment(JTextField.RIGHT);
		gridSpacing.setToolTipText(formatToolTip("Snap Grid Spacing",
				"Distance between adjacent grid points, e.g. 0.1 m, 10 km, etc."));

		gridSpacing.setEnabled(snapToGrid.isSelected());

		buttonBar.add(gridSpacing);
	}
 
Example 9
@Override
public void installListeners() {
    this.focusListener = new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            SwingUtilities.invokeLater(() -> {
                if (WidgetUtilities.hasTextFocusSelectAllProperty(jcomp)
                        && jcomp.isEditable())
                    jcomp.selectAll();
            });
        }
    };
    this.jcomp.addFocusListener(this.focusListener);
}
 
Example 10
/**
 * Restoring native behavior. We should sets the selection range to zero,
 * when component lost its focus.
 *
 * @param e the focus event
 */
@Override
void handleJavaFocusEvent(final FocusEvent e) {
    if (e.getID() == FocusEvent.FOCUS_LOST) {
        // In order to de-select the selection
        setCaretPosition(0);
    }
    super.handleJavaFocusEvent(e);
}
 
Example 11
Source Project: darklaf   Source File: ColorValueFormatter.java    License: MIT License 5 votes vote down vote up
public void focusGained(final FocusEvent event) {
    Object source = event.getSource();
    if (source instanceof JFormattedTextField) {
        this.text = (JFormattedTextField) source;
        SwingUtilities.invokeLater(() -> {
            if (this.text != null) {
                this.text.selectAll();
            }
        });
    }
}
 
Example 12
/**
 *  Revalidates minimum/maximum range.
 *
 *  @param event  the event.
 */
@Override
public void focusLost(FocusEvent event) {
    if (event.getSource() == this.minimumRangeValue) {
        validateMinimum();
    }
    else if (event.getSource() == this.maximumRangeValue) {
        validateMaximum();
    }
}
 
Example 13
Source Project: javamoney-examples   Source File: FormDateHandler.java    License: Apache License 2.0 5 votes vote down vote up
public
void
focusLost(FocusEvent event)
{
  JTextField field = (JTextField)event.getSource();

  field.setText(UI_DATE_FORMAT.format(getDate(field)));
}
 
Example 14
Source Project: netbeans   Source File: URLPatternStep.java    License: Apache License 2.0 5 votes vote down vote up
public void focusGained(FocusEvent evt) {
    if(evt.getSource() == urlPatternPanel.depthComboBox && 
       ! urlPatternPanel.useSubfolderRadioButton.isSelected()) 
    {
        urlPatternPanel.useSubfolderRadioButton.setSelected(true);
    }        
}
 
Example 15
@Override
protected void onWizardStarting(@NotNull ModelWizard.Facade wizard) {
  FlutterProjectModel model = getModel();
  myKotlinCheckBox.setText(FlutterBundle.message("module.wizard.language.name_kotlin"));
  mySwiftCheckBox.setText(FlutterBundle.message("module.wizard.language.name_swift"));
  myBindings.bindTwoWay(new SelectedProperty(myUseAndroidxCheckBox), getModel().useAndroidX());

  TextProperty packageNameText = new TextProperty(myPackageName);

  Expression<String> computedPackageName = new DomainToPackageExpression(model.companyDomain(), model.projectName()) {
    @Override
    public String get() {
      return super.get().replaceAll("_", "");
    }
  };
  BoolProperty isPackageSynced = new BoolValueProperty(true);
  myBindings.bind(packageNameText, computedPackageName, isPackageSynced);
  myBindings.bind(model.packageName(), packageNameText);
  myListeners.listen(packageNameText, value -> isPackageSynced.set(value.equals(computedPackageName.get())));

  // The wizard changed substantially in 3.5. Something causes this page to not get properly validated
  // after it is added to the Swing tree. Here we check that we have to validate the tree, then do so.
  // It only needs to be done once, so we remove the listener to prevent possible flicker.
  focusListener = new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      super.focusGained(e);
      Container parent = myRoot;
      while (parent != null && !(parent instanceof JBLayeredPane)) {
        parent = parent.getParent();
      }
      if (parent != null) {
        parent.validate();
      }
      myPackageName.removeFocusListener(focusListener);
    }
  };
  myPackageName.addFocusListener(focusListener);
}
 
Example 16
Source Project: rest-client   Source File: UrlComboBox.java    License: Apache License 2.0 5 votes vote down vote up
public UrlComboBox() {
    setToolTipText("URL");
    setEditable(true);
    final JTextField editorComponent = (JTextField) getEditor().getEditorComponent();
    editorComponent.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            editorComponent.selectAll();
        }
    });
    
    AutoCompletion ac = new AutoCompletion(this);
    ac.setStrict(false);
    ac.setStrictCompletion(false);
}
 
Example 17
@Override
public void focusLost(FocusEvent focusEvent) {
    String controllerName = getControllerName();
    if (controllerName == null) {
        return;
    }
    form.getControllerNameBox().getChildComponent().getEditor().setItem(controllerName);

}
 
Example 18
@Override
public void focusGained(FocusEvent e) {
    if (e.getSource() instanceof JTextField) {
        JTextField tf = (JTextField) e.getSource();
        if (!tf.getText().isEmpty()) {
            tf.setText("");
            tf.setForeground(new Color(50, 50, 50));
        }
    }
}
 
Example 19
Source Project: netbeans   Source File: QueryController.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void focusGained(FocusEvent e) {
    if(panel.changedFromTextField.getText().equals("")) {                   // NOI18N
        String lastChangeFrom = BugzillaConfig.getInstance().getLastChangeFrom();
        panel.changedFromTextField.setText(lastChangeFrom);
        panel.changedFromTextField.setSelectionStart(0);
        panel.changedFromTextField.setSelectionEnd(lastChangeFrom.length());
    }
}
 
Example 20
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
private MainPanel() {
  super(new GridLayout(2, 1));

  JTextField field = new JTextField("aaaaaaaaa");
  field.addFocusListener(new FocusAdapter() {
    @Override public void focusGained(FocusEvent e) {
      ((JTextComponent) e.getComponent()).selectAll();
    }
  });

  add(makeTitledPanel("focusGained: selectAll", field));
  add(makeTitledPanel("default", new JTextField("bbbbbbbbb")));
  setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 21
Source Project: gcs   Source File: PageField.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void processFocusEvent(FocusEvent event) {
    super.processFocusEvent(event);
    if (event.getID() == FocusEvent.FOCUS_GAINED) {
        selectAll();
    }
}
 
Example 22
Source Project: mylizzie   Source File: ChangeMoveDialog.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void handleSpinnerMoveNumberEditorFocusGained(FocusEvent e) {
    Component component = e.getComponent();

    if (component instanceof JTextComponent) {
        final JTextComponent textComponent = (JTextComponent) component;
        Lizzie.miscExecutor.schedule(() -> SwingUtilities.invokeLater(textComponent::selectAll), 250, TimeUnit.MILLISECONDS);
    }
}
 
Example 23
Source Project: 3DSoftwareRenderer   Source File: Input.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Updates state when the window loses focus */
public void focusLost(FocusEvent e) {
	for (int i = 0; i < keys.length; i++)
		keys[i] = false;
	for (int i = 0; i < mouseButtons.length; i++)
		mouseButtons[i] = false;
}
 
Example 24
Source Project: netbeans   Source File: WeakListener.java    License: Apache License 2.0 5 votes vote down vote up
/** Delegates to the original listener.
*/
public void focusGained(FocusEvent ev) {
    FocusListener l = (FocusListener) super.get(ev);

    if (l != null) {
        l.focusGained(ev);
    }
}
 
Example 25
Source Project: swing-material   Source File: MaterialFormattedTextField.java    License: MIT License 5 votes vote down vote up
@Override
protected void processFocusEvent(FocusEvent e) {
    super.processFocusEvent(e);
    floatingLabel.update();
    line.update();
    repaint();
}
 
Example 26
Source Project: netbeans   Source File: ButtonFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void processFocusEvent(FocusEvent fe) {
    super.processFocusEvent(fe);
    if (fe.getID() == FocusEvent.FOCUS_LOST) {
        stopTimer();
    }
}
 
Example 27
@SuppressWarnings("deprecation")
public static boolean deliverFocus(Component lightweightChild,
                                   Component target,
                                   boolean temporary,
                                   boolean focusedWindowChangeAllowed,
                                   long time,
                                   CausedFocusEvent.Cause cause,
                                   Component currentFocusOwner) // provided by the descendant peers
{
    if (lightweightChild == null) {
        lightweightChild = target;
    }

    Component currentOwner = currentFocusOwner;
    if (currentOwner != null && currentOwner.getPeer() == null) {
        currentOwner = null;
    }
    if (currentOwner != null) {
        FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST,
                                             false, lightweightChild, cause);

        if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
            focusLog.finer("Posting focus event: " + fl);
        }
        SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);
    }

    FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,
                                         false, currentOwner, cause);

    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        focusLog.finer("Posting focus event: " + fg);
    }
    SunToolkit.postEvent(SunToolkit.targetToAppContext(lightweightChild), fg);
    return true;
}
 
Example 28
public void init() {
    robot = Util.createRobot();
    kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent event) {
            System.out.println("--> " + event);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
}
 
Example 29
@Override
public void clearGlobalFocusOwner(Window activeWindow) {
    if (activeWindow != null) {
        Component focusOwner = activeWindow.getFocusOwner();
        if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
            focusLog.fine("Clearing global focus owner " + focusOwner);
        }
        if (focusOwner != null) {
            FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
                                                 CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
            SunToolkit.postPriorityEvent(fl);
        }
    }
}
 
Example 30
/**
 * Called when component receives focus
 */
public void focusGained(FocusEvent e) {
    if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
        focusLog.fine("{0}", e);
    }
    bHasFocus = true;
}