Example usage for javax.swing AbstractButton getText

List of usage examples for javax.swing AbstractButton getText

Introduction

In this page you can find the example usage for javax.swing AbstractButton getText.

Prototype

public String getText() 

Source Link

Document

Returns the button's text.

Usage

From source file:RadioButtonSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            boolean selected = aButton.getModel().isSelected();
            System.out.println(actionEvent.getActionCommand() + " - selected? " + selected);
        }//from  w  ww. j  a v a 2s.  c o m
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton aButton = (AbstractButton) itemEvent.getSource();
            int state = itemEvent.getStateChange();
            String selected = ((state == ItemEvent.SELECTED) ? "selected" : "not selected");
            System.out.println(aButton.getText() + " - selected? " + selected);
        }
    };

    JFrame frame = new JFrame("Radio Menu Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    ButtonGroup buttonGroup = new ButtonGroup();
    menu.setMnemonic(KeyEvent.VK_M);

    JRadioButtonMenuItem emptyMenuItem = new JRadioButtonMenuItem();
    emptyMenuItem.setActionCommand("Empty");
    emptyMenuItem.addActionListener(actionListener);
    buttonGroup.add(emptyMenuItem);
    menu.add(emptyMenuItem);

    JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem("Partridge");
    oneMenuItem.addActionListener(actionListener);
    buttonGroup.add(oneMenuItem);
    menu.add(oneMenuItem);

    JRadioButtonMenuItem twoMenuItem = new JRadioButtonMenuItem("Turtle Doves", true);
    twoMenuItem.addActionListener(actionListener);
    buttonGroup.add(twoMenuItem);
    menu.add(twoMenuItem);

    JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem("French Hens", threeIcon);
    threeMenuItem.addItemListener(itemListener);
    buttonGroup.add(threeMenuItem);
    menu.add(threeMenuItem);

    JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem("Calling Birds", fourIcon, true);
    fourMenuItem.addActionListener(actionListener);
    buttonGroup.add(fourMenuItem);
    menu.add(fourMenuItem);

    JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(fiveIcon);
    fiveMenuItem.addActionListener(actionListener);
    fiveMenuItem.setActionCommand("Rings");
    buttonGroup.add(fiveMenuItem);
    menu.add(fiveMenuItem);

    JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(sixIcon, true);
    sixMenuItem.addActionListener(actionListener);
    sixMenuItem.setActionCommand("Geese");
    buttonGroup.add(sixMenuItem);
    menu.add(sixMenuItem);

    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void putNemonicKey(AbstractButton button, int mnemonicKey) {
    String text = button.getText();
    if ('A' <= mnemonicKey && mnemonicKey <= 'Z') {
        button.setText(getLabelName(text, (char) mnemonicKey, false));
    }/*from w w w  .j av a  2  s.  com*/
    button.setMnemonic(mnemonicKey);
}

From source file:Main.java

public static void putNemonicKey(AbstractButton button, char mnemonicKey) {
    String text = button.getText();
    if (text.indexOf(mnemonicKey) >= 0) {
        button.setText(getLabelName(text, mnemonicKey, false));
    }/*  w ww  . j a v a2 s .  c om*/
    button.setMnemonic(mnemonicKey);
}

From source file:Main.java

/** 
 * Uses the value returned via the component's getText() method to set a Mnemonic key.
 * The character following the first '&' charcater is used as Mnemonic key,
 * but this only works for characters in the range a..Z
 * If a Mnemonic key is found, the '&' character is removed from the text.
 * @param textComponent/*from   www  .  j a v a 2s . co m*/
 */
public static void setMnemonic(AbstractButton textComponent) {

    String label = textComponent.getText();
    if (label == null || label.isEmpty() || !label.contains("&") || label.indexOf('&') == label.length() - 1) {

        return;
    }
    char ch = label.charAt(label.indexOf('&') + 1);
    if (!Character.isLetter(ch)) {
        return;
    }
    int ke = getKeyEvent(ch);
    if (ke != Integer.MIN_VALUE) {
        label = label.substring(0, label.indexOf('&'))
                + label.substring(label.indexOf('&') + 1, label.length());
        textComponent.setText(label);
        textComponent.setMnemonic(ke);
    }
}

From source file:JRadioButtonSelectedElements.java

public static Enumeration<String> getSelectedElements(Container container) {
    Vector<String> selections = new Vector<String>();
    Component components[] = container.getComponents();
    for (int i = 0, n = components.length; i < n; i++) {
        if (components[i] instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) components[i];
            if (button.isSelected()) {
                selections.addElement(button.getText());
            }/*w  ww  .j  av  a 2 s.  c om*/
        }
    }
    return selections.elements();
}

From source file:Main.java

public static String layoutCustomButton(AbstractButton b, Icon icon, Rectangle viewRect, Rectangle iconRect,
        Rectangle textRect) {//from w  ww .j  a  v  a  2s. co  m
    return layoutComponent(b, b.getText(), icon, b.getIconTextGap(), b.getVerticalAlignment(),
            b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect,
            iconRect, textRect);
}

From source file:GroupRadio.java

public static Enumeration getSelectedElements(Container container) {
    Vector selections = new Vector();
    Component components[] = container.getComponents();
    for (int i = 0, n = components.length; i < n; i++) {
        if (components[i] instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) components[i];
            if (button.isSelected()) {
                selections.addElement(button.getText());
            }//from   w w w.  j  av a2s .  c  o m
        }
    }
    return selections.elements();
}

From source file:Main.java

public static String getText(final AbstractButton abstractButton) {
    return abstractButton != null ? runInEDT(() -> abstractButton.getText(), null, "") : "";
}

From source file:Main.java

public static String getText(final AbstractButton abstractButton) {
    if (abstractButton != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return abstractButton.getText();
        } else {//from   w  w w  .ja  v  a  2  s  .c om
            final String[] text = { "" };

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        text[0] = abstractButton.getText();
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            if (text[0] == null) {
                text[0] = "";
            }

            return text[0];
        }
    } else {
        return "";
    }
}

From source file:ToggleButtonCheckBoxRadioButton.java

public TogglePanel() {
    JToggleButton tog = new JToggleButton("Toggle");
    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            AbstractButton src = (AbstractButton) (e.getSource());
            System.out.println("Toggle: " + src.getText());
        }//from  w w  w  .j  a  v  a  2s .  co  m
    };
    tog.addItemListener(listener);
    add(tog);

    JCheckBox cbox = new JCheckBox("Checkbox");
    cbox.addItemListener(listener);
    add(cbox);
    ButtonGroup btngroup = new ButtonGroup();
    for (int i = 1; i <= 3; i++) {
        JRadioButton radio = new JRadioButton("Radio " + i);
        btngroup.add(radio);
        radio.addItemListener(listener);
        add(radio);
    }
}