Java JCheckBox imply(final JCheckBox checked, final boolean checkedState, final JCheckBox changed, final boolean impliedState)

Here you can find the source of imply(final JCheckBox checked, final boolean checkedState, final JCheckBox changed, final boolean impliedState)

Description

Checks state of the checked checkbox and if state is checkedState than to disable changed checkbox and change its state to impliedState .

License

Apache License

Parameter

Parameter Description
checked the checkbox to monitor
checkedState the state that triggers disabling changed state
changed the checkbox to change
impliedState the implied state of checkbox

Declaration

public static void imply(final JCheckBox checked,
        final boolean checkedState, final JCheckBox changed,
        final boolean impliedState) 

Method Source Code

//package com.java2s;
/*//ww w .  j  a  v a  2s.  co m
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.*;

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

public class Main {
    /**
     * Checks state of the {@code checked} checkbox and if state is {@code checkedState} than to disable {@code changed}
     * checkbox and change its state to {@code impliedState}. When the {@code checked} checkbox changes states to other state,
     * than enable {@code changed} and restore its state. Note that the each checkbox should be implied by only one other checkbox.
     *
     * @param checked      the checkbox to monitor
     * @param checkedState the state that triggers disabling changed state
     * @param changed      the checkbox to change
     * @param impliedState the implied state of checkbox
     */
    public static void imply(final JCheckBox checked,
            final boolean checkedState, final JCheckBox changed,
            final boolean impliedState) {
        ActionListener l = new ActionListener() {
            Boolean previousState;

            public void actionPerformed(ActionEvent e) {
                if (checked.isSelected() == checkedState) {
                    if (previousState == null) {
                        previousState = changed.isSelected();
                    }
                    changed.setEnabled(false);
                    changed.setSelected(impliedState);
                } else {
                    changed.setEnabled(true);
                    if (previousState != null) {
                        changed.setSelected(previousState);
                        previousState = null;
                    }
                }
            }
        };
        checked.addActionListener(l);
        l.actionPerformed(null);
    }
}

Related

  1. createCheckBox(final String title, final Preferences node, final String pref_name, final boolean default_val)
  2. createCheckBox(String name, String command, boolean selected)
  3. createCheckBox(String s, boolean b)
  4. createJCheckBox(Rectangle bounds)
  5. exclusive(final JCheckBox first, final boolean firstState, final JCheckBox second, final boolean secondState)
  6. initJCheckBox(JCheckBox box, String element)
  7. isCheckBoxModified(JCheckBox checkBox, boolean originalValue)
  8. link(final JCheckBox box, final JComponent... comps)
  9. newJCheckBox()