Example usage for javax.swing JCheckBox addActionListener

List of usage examples for javax.swing JCheckBox addActionListener

Introduction

In this page you can find the example usage for javax.swing JCheckBox addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:SelectingCheckBox.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
            abstractButton.setText(newLabel);
        }/*from ww w  .  j av  a  2s  .co  m*/
    };
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }
        }
    };

    checkBox.addActionListener(actionListener);
    checkBox.addChangeListener(changeListener);
    checkBox.addItemListener(itemListener);
    checkBox.setMnemonic(KeyEvent.VK_S);
    Container contentPane = frame.getContentPane();
    contentPane.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void onCheckBoxChange(JCheckBox field, final Runnable task) {
    field.addActionListener(new ActionListener() {

        @Override/* w  w w  .j av a2s. com*/
        public void actionPerformed(ActionEvent e) {
            task.run();
        }
    });
}

From source file:Main.java

/**
 * Makes a new CheckBox with given properties.
 * //from ww  w . j  a  va 2  s.co m
 * @param name
 *            The label of the control.
 * @param command
 *            The name the action listener is looking for.
 * @param isSelected
 *            The condition on which this control is selected.
 * @param listener
 *            The action listener.
 * @return A new CheckBox with proper default properties.
 */
public static JCheckBox createCheckBox(String name, String command, boolean isSelected,
        ActionListener listener) {
    JCheckBox button;
    button = new JCheckBox(name);
    button.setActionCommand(command);
    button.setSelected(isSelected);
    button.addActionListener(listener);
    return button;
}

From source file:com.surenpi.autotest.suite.SuiteRunnerLauncher.java

/**
 * @param centerPanel/*from  ww w.j a v a2  s. c o m*/
 * @param urlList
 */
private static void createItemsPanel(JPanel centerPanel, List<URL> urlList) {
    JPanel itemsPanel = new JPanel();
    centerPanel.add(itemsPanel, BorderLayout.CENTER);

    if (CollectionUtils.isEmpty(urlList)) {
        return;
    }

    for (URL url : urlList) {
        String text = url.getFile();
        JCheckBox box = new JCheckBox(new File(text).getName());
        box.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JCheckBox source = (JCheckBox) e.getSource();
                if (source.isSelected()) {
                    runnerList.add(source.getText());
                } else {
                    runnerList.remove(source.getText());
                }
            }
        });

        itemsPanel.add(box);
    }
}

From source file:de.fhg.iais.asc.ui.i18n.LocalizedUI.java

public static JCheckBox createCheckbox(String key, ActionListener listener) {
    key = "Checkbox." + key;
    String tooltipText = Messages.getString("Tooltip." + key, "");

    JCheckBox checkBox = new JCheckBox(Messages.getString(key));
    if (!StringUtils.isEmpty(tooltipText)) {
        checkBox.setToolTipText(tooltipText);
    }//w w w  . j  a  va 2  s .  c  o  m

    if (listener != null) {
        checkBox.addActionListener(listener);
    }

    return checkBox;
}

From source file:com.diversityarrays.kdxplore.vistool.VisToolbarFactory.java

static public VisToolToolBar create(final String title, final JComponent comp, final Closure<File> snapshotter,
        final VisToolDataProvider visToolDataProvider, boolean floatable, final String[] imageSuffixes) {
    Window window = GuiUtil.getOwnerWindow(comp);

    boolean anyButtons = false;

    final JCheckBox keepOnTop;

    if (window == null) {
        keepOnTop = null;/*from ww  w . j ava  2  s .co m*/
    } else {
        anyButtons = true;
        keepOnTop = new JCheckBox(Msg.OPTION_KEEP_ON_TOP(), true);

        keepOnTop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                window.setAlwaysOnTop(keepOnTop.isSelected());
            }
        });
        window.setAlwaysOnTop(keepOnTop.isSelected());

        //         buttons.add(keepOnTop);

        final PropertyChangeListener alwaysOnTopListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                keepOnTop.setSelected(window.isAlwaysOnTop());
            }
        };
        window.addPropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                window.removeWindowListener(this);
                window.removePropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);
            }
        });
    }

    final JButton cameraButton;
    if (snapshotter == null) {
        cameraButton = null;
    } else {
        Action cameraAction = new AbstractAction(Msg.ACTION_SNAPSHOT()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chooser == null) {
                    chooser = new JFileChooser();
                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            if (!f.isFile()) {
                                return true;
                            }
                            String loname = f.getName().toLowerCase();
                            for (String sfx : imageSuffixes) {
                                if (loname.endsWith(sfx)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Msg.DESC_IMAGE_FILE();
                        }
                    });
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                }

                if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(comp)) {
                    File file = chooser.getSelectedFile();
                    snapshotter.execute(file);
                }
            }
        };

        ImageIcon icon = loadIcon("camera-24.png"); //$NON-NLS-1$
        if (icon != null) {
            cameraAction.putValue(Action.SMALL_ICON, icon);
            cameraAction.putValue(Action.NAME, null);
        }

        anyButtons = true;
        cameraButton = new JButton(cameraAction);
    }

    final JButton refreshButton;
    if (visToolDataProvider == null) {
        refreshButton = null;
    } else {
        anyButtons = true;

        refreshButton = new JButton(Msg.ACTION_REFRESH());

        ImageIcon icon = loadIcon("refresh-24.png"); //$NON-NLS-1$
        if (icon != null) {
            refreshButton.setIcon(icon);
            // don't remove the name
        }

        refreshButton.setForeground(Color.RED);
        refreshButton.setEnabled(false);

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (visToolDataProvider.refreshData()) {
                    refreshButton.setEnabled(false);
                }
            }
        });

        visToolDataProvider.addVisToolDataChangedListener(new VisToolDataChangedListener() {
            @Override
            public void visToolDataChanged(Object source) {
                refreshButton.setEnabled(true);
            }
        });
    }

    VisToolToolBar toolBar = null;

    if (anyButtons) {
        toolBar = new VisToolToolBar(keepOnTop, cameraButton, refreshButton);
        toolBar.setFloatable(floatable);
    }
    return toolBar;

}

From source file:eu.europa.ec.markt.dss.applet.util.ComponentFactory.java

/**
 * /*from   w  w  w .ja v  a2 s .  co  m*/
 * @param text
 * @param actionListener
 * @return
 */
public static JCheckBox checkButton(final String text, final ActionListener actionListener) {
    final JCheckBox check = new JCheckBox(text);
    check.addActionListener(actionListener);
    return check;
}

From source file:CheckBox.java

public CheckBox() {
    JCheckBox checkbox = new JCheckBox("Show Title", true);
    checkbox.addActionListener(this);
    add(checkbox);/*from w  w  w  .  j  a  v  a2 s  . c  o m*/

    setSize(280, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

From source file:Main.java

public Main() {
    JCheckBox checkbox = new JCheckBox("Show Title", true);
    checkbox.addActionListener(this);
    add(checkbox);//from  www  . j a v  a 2 s .c o  m

    setSize(280, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

From source file:ToolBarwithCheckBox.java

public ToolbarPanel() {
    setLayout(new BorderLayout());
    JToolBar toolbar = new JToolBar();
    for (int i = 1; i < 4; i++) {
        JCheckBox cbox = new JCheckBox("Checkbox #" + i);
        toolbar.add(cbox);/*from   w  w w  .j  av  a 2  s  .c  om*/
        cbox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JCheckBox source = (JCheckBox) (e.getSource());
                System.out.println("Toolbar " + source.getText());
            }
        });
    }

    add(toolbar, BorderLayout.NORTH);
}