Java JCheckBox setDependency(final JCheckBox master, final JCheckBox slave)

Here you can find the source of setDependency(final JCheckBox master, final JCheckBox slave)

Description

Adds dependancy between two check boxes, so that the slave is enabled only when master is checked.

License

Open Source License

Parameter

Parameter Description
master master.
slave slave.

Declaration

public static void setDependency(final JCheckBox master, final JCheckBox slave) 

Method Source Code


//package com.java2s;
// the terms of the GNU General Public License as published by the Free Software Foundation;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    /**//from w  ww .  j  ava2 s .c om
     * Adds dependancy between two check boxes, so that the slave is enabled only when master is checked.
     *
     * @param master    master.
     * @param slave     slave.
     */
    public static void setDependency(final JCheckBox master, final JCheckBox slave) {
        if (master == null || slave == null)
            return;

        slave.setEnabled(master.isSelected());
        master.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                slave.setEnabled(master.isSelected());
            }
        });
    }
}

Related

  1. isCheckBoxModified(JCheckBox checkBox, boolean originalValue)
  2. link(final JCheckBox box, final JComponent... comps)
  3. newJCheckBox()
  4. newJCheckBox(String n, char m)
  5. onCheckBoxChange(JCheckBox field, final Runnable task)
  6. setSelectedWithClick(JCheckBox checkBox, boolean selected)
  7. update(JCheckBox box, JComponent... comps)
  8. updateCheckBoxState(final JCheckBox aCheckBox, final boolean aEnabled)