Java Code Examples for javax.swing.AbstractButton#setMnemonic()

The following examples show how to use javax.swing.AbstractButton#setMnemonic() . 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: CreateBranchForm.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 2
Source File: Utilities.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Apply a shortcut to a button.
 * 
 * @param button Button.
 * @param shortcut Shortcut.
 * @param message Related message.
 */
private static void setShortcut(
    AbstractButton button,
    ShortcutProperties shortcut,
    String message) {
  if ((shortcut == null) || (shortcut.useMnemonic())) {
    int mnemonic = getMnemonic(message);
    if ((mnemonic == -1) && (shortcut != null)) {
      mnemonic = shortcut.getKey();
    }
    if (mnemonic != -1) {
      button.setMnemonic(mnemonic);
    }
  } else if (shortcut.getEnabled()) {
    String actionName = "action_" + shortcut.getName();
    InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(KeyStroke.getKeyStroke(shortcut.getKey(), shortcut.getModifiers()), actionName);
    button.getActionMap().put(actionName, new ActionClick(button));
    if (message != null) {
      int index = message.indexOf(shortcut.getKey());
      if ((index >= 0) && (index < button.getText().length())) {
        button.setDisplayedMnemonicIndex(index);
      }
    }
  }
}
 
Example 3
Source File: TeamServicesSettingsForm.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 4
Source File: CreateBranchForm.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 5
Source File: ManageWorkspacesForm.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 6
Source File: TFSVersionFilterComponent.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 7
Source File: LockItemsForm.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 8
Source File: SingleTargetProductDialog.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
protected SingleTargetProductDialog(AppContext appContext, String title, int buttonMask, String helpID, TargetProductSelectorModel model, boolean alwaysWriteOutput) {
    super(appContext.getApplicationWindow(), title, buttonMask, helpID);
    this.appContext = appContext;
    targetProductSelector = new TargetProductSelector(model, alwaysWriteOutput);
    String homeDirPath = SystemUtils.getUserHomeDir().getPath();
    String saveDir = appContext.getPreferences().getPropertyString(SaveProductAsAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, homeDirPath);
    targetProductSelector.getModel().setProductDir(new File(saveDir));
    if (!alwaysWriteOutput) {
        targetProductSelector.getOpenInAppCheckBox().setText("Open in " + appContext.getApplicationName());
    }
    targetProductSelector.getModel().getValueContainer().addPropertyChangeListener(evt -> {
        if (evt.getPropertyName().equals("saveToFileSelected") ||
                evt.getPropertyName().equals("openInAppSelected")) {
            updateRunButton();
        }
    });
    AbstractButton button = getButton(ID_APPLY);
    button.setText("Run");
    button.setMnemonic('R');
    updateRunButton();
}
 
Example 9
Source File: ProxySettingsForm.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
/**
 * @noinspection ALL
 */
private void $$$loadButtonText$$$(AbstractButton component, String text) {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == '&') {
            i++;
            if (i == text.length()) break;
            if (!haveMnemonic && text.charAt(i) != '&') {
                haveMnemonic = true;
                mnemonic = text.charAt(i);
                mnemonicIndex = result.length();
            }
        }
        result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic) {
        component.setMnemonic(mnemonic);
        component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
}
 
Example 10
Source File: Mnemonics.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Wrapper for AbstractButton.setMnemonic and JLabel.setDisplayedMnemonic
 * @param item AbstractButton/JLabel
 * @param mnem Mnemonic char to set, latin [a-z,A-Z], digit [0-9], or any VK_ code
 */
private static void setMnemonic(Object item, int mnem) {
    if (isAquaLF()) {
        // there shall be no mnemonics on macosx.
        //#55864
        return;
    }

    if ((mnem >= 'a') && (mnem <= 'z')) {
        mnem = mnem + ('A' - 'a');
    }

    if (item instanceof AbstractButton) {
        AbstractButton b = (AbstractButton) item;
        b.putClientProperty(PROP_MNEMONIC, mnem);
        b.setMnemonic(mnem);
    } else {
        ((JLabel) item).setDisplayedMnemonic(mnem);
    }
}
 
Example 11
Source File: GuiUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
/** Parse text that has the mnemonic marked with a preceding'&amp;'
    (like in Qt) and set the text and mnemonic of the button. */
public static void setTextAndMnemonic(AbstractButton button, String text)
{
    int pos = text.indexOf('&');
    text = text.replace("&", "");
    button.setText(text);
    if (pos >= 0 && pos < text.length())
    {
        String mnemonic = text.substring(pos, pos + 1).toUpperCase();
        KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic);
        int code = keyStroke.getKeyCode();
        button.setMnemonic(code);
        button.setDisplayedMnemonicIndex(pos);
    }
}
 
Example 12
Source File: Utils.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Actual setter of the text & mnemonics for the AbstractButton or
 * their subclasses. We must copy necessary code from org.openide.awt.Mnemonics
 * because org.openide.awt module is not available yet when this code is called.
 * @param item AbstractButton
 * @param text new label
 */
static void setLocalizedText (AbstractButton button, String text) {
    if (text == null) {
        button.setText(null);
        return;
    }

    int i = findMnemonicAmpersand(text);

    if (i < 0) {
        // no '&' - don't set the mnemonic
        button.setText(text);
        button.setMnemonic(0);
    } else {
        button.setText(text.substring(0, i) + text.substring(i + 1));

        if (Utilities.isMac()) {
            // there shall be no mnemonics on macosx.
            //#55864
            return;
        }

        char ch = text.charAt(i + 1);

        // it's latin character or arabic digit,
        // setting it as mnemonics
        button.setMnemonic(ch);

        // If it's something like "Save &As", we need to set another
        // mnemonic index (at least under 1.4 or later)
        // see #29676
        button.setDisplayedMnemonicIndex(i);
    }
}
 
Example 13
Source File: PlatformUtil.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set mnemonic on button in a platform dependant manner.
 *
 * @param button
 *            Button
 * @param mnemonic
 *            Mnemonic
 * @param index
 *            Index of string to underline
 */
public static void setMnemonic(AbstractButton button, char mnemonic, int index) {
	/*
	 * Only set mnemonic if not using macOS - they are not recommended by
	 * the style guidelines there and clash with established commands
	 */
	if (!OperatingSystem.isMacOs()) {
		button.setMnemonic(mnemonic);

		if (index >= 0) {
			button.setDisplayedMnemonicIndex(index);
		}
	}
}
 
Example 14
Source File: CommonMenuText.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param m the button item to change the text, short description and mnemonic
 * @param substitutes substitutes to use in a message format
 * @param prop key bundle to use
 */
public static void name(AbstractButton m, String prop, Object... substitutes)
{
	m.setText(getName(prop, substitutes));
	String shortDesc = getShortDesc(prop, substitutes);
	if (shortDesc != null && !shortDesc.isEmpty())
	{
		m.setToolTipText(shortDesc);
	}
	m.setMnemonic(getMnemonic(prop));
}
 
Example 15
Source File: ServiceConfigurationPane.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private void $$$loadButtonText$$$( AbstractButton component, String text )
{
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for( int i = 0; i < text.length(); i++ )
    {
        if( text.charAt( i ) == '&' )
        {
            i++;
            if( i == text.length() )
            {
                break;
            }
            if( !haveMnemonic && text.charAt( i ) != '&' )
            {
                haveMnemonic = true;
                mnemonic = text.charAt( i );
                mnemonicIndex = result.length();
            }
        }
        result.append( text.charAt( i ) );
    }
    component.setText( result.toString() );
    if( haveMnemonic )
    {
        component.setMnemonic( mnemonic );
        component.setDisplayedMnemonicIndex( mnemonicIndex );
    }
}
 
Example 16
Source File: Mnemonics.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void propertyChange(PropertyChangeEvent evt) {
    AbstractButton b = (AbstractButton) evt.getSource();
    if (b.getDisplayedMnemonicIndex() == -1) {
        Integer mnemonic = (Integer) b.getClientProperty(PROP_MNEMONIC);
        Integer index = (Integer) b.getClientProperty(PROP_DISPLAYED_MNEMONIC_INDEX);
        if (mnemonic != null && index != null && Utilities.compareObjects(b.getText(), b.getClientProperty(PROP_TEXT))) {
            b.setMnemonic(mnemonic);
            b.setDisplayedMnemonicIndex(index);
        }
    }
}
 
Example 17
Source File: AnnotatedString.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Localise the given AbstractButton, setting the text and optionally
 * mnemonic Note that AbstractButton includes menus and menu items.
 *
 * @param button
 *            The button to localise
 * @param key
 *            The key to look up in resource bundle
 * @param defaultString
 *            default String to use if key not found
 * @param setMnemonic
 *            whether or not to set the mnemonic. According to Sun's
 *            guidelines, default/cancel buttons should not have mnemonics
 *            but instead should use Return/Escape
 */
public static void localiseButton(AbstractButton button, String key, String defaultString, boolean setMnemonic) {
    AnnotatedString as = new AnnotatedString(L10N.getLocalString(key, defaultString));
    button.setText(as.toString());
    int mnemonic;
    if (setMnemonic && (mnemonic = as.getMnemonic()) != KeyEvent.VK_UNDEFINED) {
        button.setMnemonic(mnemonic);
        button.setDisplayedMnemonicIndex(as.getMnemonicIndex());
    }
}
 
Example 18
Source File: GuiUtils.java    From sc2gears with Apache License 2.0 3 votes vote down vote up
/**
 * Updates/sets the text of an abstract button based on the key of its text.
 * 
 * <p>The button's text will be taken from the {@link Language} specified by the <code>textKey</code>.<br>
 * If the text of the button designates a mnemonic, it will be set properly.</p>
 * 
 * @param button  abstract button whose text to be updated
 * @param textKey key of the button's text
 */
public static void updateButtonText( final AbstractButton button, final String textKey, Object... arguments ) {
	final Pair< String, Character > textAndMnemonic = Language.getTextAndMnemonic( textKey, arguments );
	
	button.setText( textAndMnemonic.value1 );
	if ( textAndMnemonic.value2 != null )
		button.setMnemonic( textAndMnemonic.value2 );
}
 
Example 19
Source File: ResourceUtils.java    From Spark with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the resources on a subclass of {@link AbstractButton}.  The common
 * classes are {@link javax.swing.JRadioButton}, {@link javax.swing.JButton},
 * and {@link javax.swing.JCheckBox}
 * <p/>
 * This method sets the text and mnemonic.
 *
 * @param button    The button on which to set the text and mnemonoic
 * @param labelText the text which contains the displayed text and mnemonic
 * @see AbstractButton#setText(String)
 * @see AbstractButton#setMnemonic(int)
 */
public static void resButton(AbstractButton button, String labelText) {
    button.setText(stripMnemonic(labelText));

    if (Spark.isWindows()) {
        button.setMnemonic(getMnemonicKeyCode(labelText));
    }
}
 
Example 20
Source File: Lang.java    From drmips with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Shortcut function to translate a button type component.
 * @param btn Button type component.
 * @param key Key of the button text string.
 * @param args Text string arguments values.
 */
public static void tButton(AbstractButton btn, String key, Object[] args) {
	btn.setText(t(key, args));
	btn.setMnemonic(mnemonic(key));
}