Here you can find the source of imply(final JCheckBox checked, final boolean checkedState, final JCheckBox changed, final boolean impliedState)
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 |
public static void imply(final JCheckBox checked, final boolean checkedState, final JCheckBox changed, final boolean impliedState)
//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); } }