Java Code Examples for java.awt.event.FocusAdapter

The following examples show how to use java.awt.event.FocusAdapter. 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: darklaf   Source File: DarkFileChooserUI.java    License: MIT License 6 votes vote down vote up
protected JComponent createFileNamePanel(final JFileChooser fileChooser) {
    JPanel fileNamePanel = new JPanel();
    fileNamePanel.setLayout(new BoxLayout(fileNamePanel, BoxLayout.LINE_AXIS));
    fileNameLabel = new AlignedLabel();
    populateFileNameLabel();
    fileNamePanel.add(fileNameLabel);
    fileNameTextField = new FileTextField();
    fileNamePanel.add(fileNameTextField);
    fileNameLabel.setLabelFor(fileNameTextField);
    fileNameTextField.addFocusListener(new FocusAdapter() {
        public void focusGained(final FocusEvent e) {
            if (!getFileChooser().isMultiSelectionEnabled()) {
                filePane.clearSelection();
            }
        }
    });
    if (fileChooser.isMultiSelectionEnabled()) {
        setFileName(fileNameString(fileChooser.getSelectedFiles()));
    } else {
        setFileName(fileNameString(fileChooser.getSelectedFile()));
    }
    return fileNamePanel;
}
 
Example 2
Source Project: netbeans   Source File: SpecialkeyPanel.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates new form SpecialkeyPanel */
public SpecialkeyPanel(final Popupable parent, JTextField target) {
    this.parent = parent;
    this.target = target;
    initComponents();

    target.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent e) {
            parent.hidePopup();
        }
    });

    downButton.addActionListener(this);
    enterButton.addActionListener(this);
    escButton.addActionListener(this);
    leftButton.addActionListener(this);
    rightButton.addActionListener(this);
    tabButton.addActionListener(this);
    upButton.addActionListener(this);
    wheelUpButton.addActionListener(this);
    wheelDownButton.addActionListener(this);
}
 
Example 3
Source Project: netbeans   Source File: EditorUI.java    License: Apache License 2.0 6 votes vote down vote up
/** Construct extended UI for the use with a text component */
public EditorUI() {
    focusL = new FocusAdapter() {
                 public @Override void focusGained(FocusEvent evt) {
                     /* Fix of #25475 - copyAction's enabled flag
                      * must be updated on focus change
                      */
                     stateChanged(null);
                     if (component!=null){
                        BaseTextUI ui = (BaseTextUI)component.getUI();
                        if (ui!=null) ui.refresh();
                     }
                 }

                 @Override
                 public void focusLost(FocusEvent e) {
                     // see #222935, update actions before menu activates
                     if (e.isTemporary()) {
                         doStateChange(true);
                     }
                 }
             };

    getToolTipSupport();
}
 
Example 4
SystemSelectionAWTTest() {
    frame = new Frame();
    frame.setSize(200, 200);

    tf1 = new TextField();
    tf1.addFocusListener( new FocusAdapter() {
        public void focusGained(FocusEvent fe) {
            fe.getSource();
        }
    });

    tf2 = new TextField();

    frame.add(tf2, BorderLayout.NORTH);
    frame.add(tf1, BorderLayout.CENTER);

    frame.setVisible(true);
    frame.toFront();
    tf1.requestFocus();
    tf1.setText("Selection Testing");
}
 
Example 5
SystemSelectionSwingTest() {
    jframe = new JFrame();
    jframe.setSize(200, 200);

    jtf1 = new JTextField();
    jtf1.addFocusListener( new FocusAdapter() {
        public void focusGained(FocusEvent fe) {
            fe.getSource();
        }
    });

    jtf2 = new JTextField();

    jframe.add(jtf2, BorderLayout.NORTH);
    jframe.add(jtf1, BorderLayout.CENTER);

    jframe.setVisible(true);
    jframe.toFront();
    jtf1.requestFocus();
    jtf1.setText("Selection Testing");
}
 
Example 6
Source Project: workcraft   Source File: TextActionCellEditor.java    License: MIT License 6 votes vote down vote up
public TextActionCellEditor() {
    leftButton.setFocusable(false);
    leftButton.setVisible(false);
    leftButton.setMargin(PropertyHelper.BUTTON_INSETS);

    text.setFocusable(true);
    text.setBorder(GuiUtils.getTableCellBorder());
    text.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                stopCellEditing();
            }
        }
    });

    rightButton.setFocusable(false);
    rightButton.setVisible(false);
    rightButton.setMargin(PropertyHelper.BUTTON_INSETS);

    panel.add(leftButton, BorderLayout.WEST);
    panel.add(text, BorderLayout.CENTER);
    panel.add(rightButton, BorderLayout.EAST);
    panel.setFocusable(false);
}
 
Example 7
public AutoCompletionComboBox(boolean caseSensitive, int preferredWidth, int preferredHeight, boolean wide,
		ComboBoxModel<E> model) {
	super(preferredWidth, preferredHeight, wide, model);

	this.caseSensitive = caseSensitive;

	setEditable(true);
	setEditor(getEditor());

	addFocusListener(new FocusAdapter() {

		@Override
		public void focusLost(FocusEvent e) {
			setSelectedItem(((JTextField) getEditor().getEditorComponent()).getText());
			actionPerformed(new ActionEvent(this, 0, "editingStopped"));
		}
	});
}
 
Example 8
Source Project: otroslogviewer   Source File: SuggestDecorator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Add popup with suggestion to text component
 * @param textComponent  text component
 * @param suggestionSource source of suggestions
 * @param suggestionRenderer renderer for suggestions
 * @param selectionListener suggestion listener to be executed after suggestion is selected
 * @param clearFocusAfterSelection true if text selection should be removed and caret set to end of text after selecting suggestion
 * @param <T> Suggestion type
 */
public static <T> void decorate(final JTextComponent textComponent,
                                SuggestionSource<T> suggestionSource,
                                SuggestionRenderer<T> suggestionRenderer,
                                SelectionListener<T> selectionListener,
                                boolean clearFocusAfterSelection) {

  Document document = textComponent.getDocument();
  SuggestionDocumentListener<? extends T> listener = new SuggestionDocumentListener<>(textComponent, suggestionSource, suggestionRenderer, selectionListener);
  document.addDocumentListener(listener);
  if (clearFocusAfterSelection) {
    textComponent.addFocusListener(new FocusAdapter() {
      @Override
      public void focusGained(FocusEvent e) {
        //do not select all on OSX after suggestion is selected
        if (e.getOppositeComponent() == null) {
          clearTextFieldSelectionAsync(textComponent);
        }
      }
    });
  }
}
 
Example 9
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 10
Source Project: ganttproject   Source File: UIUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
void attachOnFocusLost(final ActionListener onSuccess) {
  myTextEditor.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent e) {
      try {
        tryCommit();
        onSuccess.actionPerformed(new ActionEvent(myDatePicker, ActionEvent.ACTION_PERFORMED, ""));
        return;
      } catch (ValidationException | ParseException ex) {
        // We probably don't want to log parse/validation exceptions
        // If user input is not valid we reset value to the initial one
        resetValue();
      }
    }
  });
}
 
Example 11
Source Project: Pixelitor   Source File: LayerNameEditor.java    License: GNU General Public License v3.0 6 votes vote down vote up
public LayerNameEditor(LayerButton layerButton) {
    super(layerButton.getLayer().getName());

    // TODO setting up a tool tip would show an
    // annoying GRAY "disabled tooltip"
    // One solution would be to put
    // UIManager.put("ToolTip[Disabled].backgroundPainter", UIManager.get("ToolTip[Enabled].backgroundPainter"));
    // at the beginning (not ideal), another would be
    // to create a custom tooltip specifically
    // for this component

    // setToolTipText("Double-click to rename this layer.");

    this.layerButton = layerButton;
    disableEditing();

    addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            finishEditing();
        }
    });

    // disable if enter pressed
    addActionListener(e -> finishEditing());
}
 
Example 12
private void init() {
  if ( dateChooserPanel == null ) {
    dateChooserPanel = new DateChooserPanel( Calendar.getInstance(), true );
    dateChooserPanel.addPropertyChangeListener( DateChooserPanel.PROPERTY_DATE, new InternalDateUpdateHandler() );

    dateField.addPropertyChangeListener( "value", new PropertyChangeListener() {
      public void propertyChange( final PropertyChangeEvent evt ) {
        Date newValue = (Date) evt.getNewValue();
        newValue = newValue == null ? null : DateConverter.convertToDateType( newValue, dateType );
        dateChooserPanel.setDate( newValue, false );
        dateChooserPanel.setDateSelected( true );
      }
    } );
    dateField.addFocusListener( new FocusAdapter() {
      public void focusGained( final FocusEvent e ) {
        dateChooserPanel.setDateSelected( false );
      }
    } );
    if ( dateField.getFormatterFactory() == null ) {
      setDateFormat( createDateFormat( DEFAULT_FORMAT, Locale.getDefault(), TimeZone.getDefault() ) );
    }
  }
}
 
Example 13
Source Project: consulo   Source File: DesktopEditorWindow.java    License: Apache License 2.0 6 votes vote down vote up
TComp(@Nonnull DesktopEditorWindow window, @Nonnull DesktopEditorWithProviderComposite editor) {
  super(new BorderLayout());
  myEditor = editor;
  myWindow = window;
  add(editor.getComponent(), BorderLayout.CENTER);
  addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      ApplicationManager.getApplication().invokeLater(() -> {
        if (!hasFocus()) return;
        final JComponent focus = myEditor.getSelectedEditorWithProvider().getFileEditor().getPreferredFocusedComponent();
        if (focus != null && !focus.hasFocus()) {
          IdeFocusManager.getGlobalInstance().requestFocus(focus, true);
        }
      });
    }
  });
}
 
Example 14
Source Project: plugins   Source File: SkillCalculator.java    License: GNU General Public License v3.0 5 votes vote down vote up
private FocusAdapter buildFocusAdapter(Consumer<FocusEvent> focusLostConsumer)
{
	return new FocusAdapter()
	{
		@Override
		public void focusLost(FocusEvent e)
		{
			focusLostConsumer.accept(e);
		}
	};
}
 
Example 15
@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 16
Source Project: netbeans   Source File: ResolveDeclarationsPanel.java    License: 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 17
Source Project: ramus   Source File: DFrame.java    License: GNU General Public License v3.0 5 votes vote down vote up
public DFrame(final Control control) {
    super(new BorderLayout());
    setFocusable(true);
    setFocusCycleRoot(true);
    this.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            control.focusGained(DFrame.this);
        }
    });
    control.addDFrame(this);
}
 
Example 18
public IndependenceSwingTest() {

        frame = new JFrame();
        frame.setSize(200, 200);

        // This textfield will be used to update the contents of clipboards
        tf1 = new JTextField();
        tf1.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent fe) {
                tf1.setText("Clipboards_Independance_Testing");
            }
        });

        // TextFields to get the contents of clipboard
        tf2 = new JTextField();
        tf3 = new JTextField();

        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.add(tf2, BorderLayout.NORTH);
        panel.add(tf3, BorderLayout.SOUTH);

        frame.add(tf1, BorderLayout.NORTH);
        frame.add(panel, BorderLayout.CENTER);

        frame.setVisible(true);
        tf1.requestFocus();
    }
 
Example 19
Source Project: workcraft   Source File: GenericCellEditor.java    License: MIT License 5 votes vote down vote up
public GenericCellEditor() {
    textField = new JTextField();
    textField.setFocusable(true);
    textField.setBorder(GuiUtils.getTableCellBorder());
    textField.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                stopCellEditing();
            }
        }
    });
}
 
Example 20
@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 21
Source Project: runelite   Source File: SkillCalculator.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
private FocusAdapter buildFocusAdapter(Consumer<FocusEvent> focusLostConsumer)
{
	return new FocusAdapter()
	{
		@Override
		public void focusLost(FocusEvent e)
		{
			focusLostConsumer.accept(e);
		}
	};
}
 
Example 22
/**
 * Creates an uneditable JTextField with the given text
 *
 * @param text
 *            The content of the JTextField
 * @return
 */
private static JTextField makeTextField(String text) {
	JTextField urlField = new JTextField(text);
	urlField.setEditable(false);

	urlField.addFocusListener(new FocusAdapter() {

		@Override
		public void focusGained(java.awt.event.FocusEvent evt) {
			urlField.getCaret().setVisible(true);
			urlField.selectAll();
		}
	});
	return urlField;
}
 
Example 23
private void initComponents() {
    myEnvironmentForm = new MavenEnvironmentForm();

    Project project = myProjectOrNull == null ? ProjectManager.getInstance().getDefaultProject() : myProjectOrNull;
    myEnvironmentForm.getData(MavenProjectsManager.getInstance(project).getGeneralSettings().clone());

    myEnvironmentPanel.add(myEnvironmentForm.createComponent(), BorderLayout.CENTER);

    //AS TODO: If we keep on using the archetype properties we might add a description to the Required Properties
    //AS TODO: but then we need to copy this class over and add the description to the dialog.
    myMavenPropertiesPanel = new MavenPropertiesPanel(myAvailableProperties);
    myPropertiesPanel.add(myMavenPropertiesPanel);

    doFillIn.setSelected(true);
    artifactName.addKeyListener(
        new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent keyEvent) {
                super.keyReleased(keyEvent);
                if(doFillIn.isSelected()) {
                    updateProperties();
                }
            }
        }
    );
    artifactName.addFocusListener(
        new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent focusEvent) {
                super.focusLost(focusEvent);
                if(doFillIn.isSelected()) {
                    updateProperties();
                }
            }
        }
    );
}
 
Example 24
Source Project: mars-sim   Source File: SwingDialog.java    License: GNU General Public License v3.0 5 votes vote down vote up
public JFXPanelEx(){
    addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            hidePopUps();
        }
    });
}
 
Example 25
Source Project: stendhal   Source File: SwingClientGUI.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void setupChatEntry() {
	final KeyListener tabcompletion = new ChatCompletionHelper(chatText,
			World.get().getPlayerList().getNamesList(),
			SlashActionRepository.getCommandNames());
	chatText.addKeyListener(tabcompletion);
	/*
	 * Always redirect focus to chat field
	 */
	screen.addFocusListener(new FocusAdapter() {
		@Override
		public void focusGained(final FocusEvent e) {
			chatText.getPlayerChatText().requestFocus();
		}
	});
}
 
Example 26
public JComponent createGroupComponent(GPOptionGroup group, GPOption<?>... options) {
  JPanel optionsPanel = new JPanel();

  int hasUiCount = 0;
  for (int i = 0; i < options.length; i++) {
    GPOption<?> nextOption = options[i];
    if (!nextOption.hasUi()) {
      continue;
    }
    hasUiCount++;
    final Component nextComponent = createOptionComponent(group, nextOption);
    if (needsLabel(group, nextOption)) {
      Component nextLabel = createOptionLabel(group, options[i]);
      optionsPanel.add(nextLabel);
      optionsPanel.add(nextComponent);
    } else {
      optionsPanel.add(nextComponent);
      optionsPanel.add(new JPanel());
    }
    if (i == 0) {
      optionsPanel.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
          super.focusGained(e);
          nextComponent.requestFocus();
        }

      });
    }
  }
  if (hasUiCount > 0) {
    myLayoutApi.layout(optionsPanel, hasUiCount);
  }
  return optionsPanel;
}
 
Example 27
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 28
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
protected TabTitleEditListener(JTabbedPane tabbedPane) {
  super();
  this.tabbedPane = tabbedPane;
  editor.setBorder(BorderFactory.createEmptyBorder());
  editor.addFocusListener(new FocusAdapter() {
    @Override public void focusLost(FocusEvent e) {
      renameTabTitle.actionPerformed(new ActionEvent(tabbedPane, ActionEvent.ACTION_PERFORMED, RENAME));
    }
  });
  InputMap im = editor.getInputMap(JComponent.WHEN_FOCUSED);
  ActionMap am = editor.getActionMap();
  im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL);
  am.put(CANCEL, cancelEditing);
  im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), RENAME);
  am.put(RENAME, renameTabTitle);
  editor.getDocument().addDocumentListener(this);
  // editor.addKeyListener(new KeyAdapter() {
  //   @Override public void keyPressed(KeyEvent e) {
  //     if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  //       renameTabTitle();
  //     } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  //       cancelEditing();
  //     } else {
  //       editor.setPreferredSize(editor.getText().length() > len ? null : dim);
  //       tabbedPane.revalidate();
  //     }
  //   }
  // });
  tabbedPane.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), START);
  tabbedPane.getActionMap().put(START, startEditing);
}
 
Example 29
@Override
public void addChangeListener(final ChangeListener l) {
    final GenericTagEditor t = this;
    editor.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent e) {
            l.change(t);
        }

    });
}
 
Example 30
@Override
public void addChangeListener(final ChangeListener l) {
    final GenericTagEditor t = this;
    addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent e) {
            l.change(t);
        }

    });
}