Here you can find the source of implyDisabled(final JCheckBox checked, final boolean checkedState, final JTextComponent changed)
Parameter | Description |
---|---|
checked | the checkbox to monitor |
checkedState | the state that triggers disabling changed state |
changed | the checkbox to change |
public static void implyDisabled(final JCheckBox checked, final boolean checkedState, final JTextComponent changed)
//package com.java2s; /*// w ww .jav a2 s . c o 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 javax.swing.text.JTextComponent; 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} * text field and clean it. When the {@code checked} checkbox changes states to other state, * than enable {@code changed} and restore its state. Note that the each text field 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 */ public static void implyDisabled(final JCheckBox checked, final boolean checkedState, final JTextComponent changed) { ActionListener l = new ActionListener() { String previousState; public void actionPerformed(ActionEvent e) { if (checked.isSelected() == checkedState) { if (previousState == null) { previousState = changed.getText(); } changed.setEnabled(false); changed.setText(""); } else { changed.setEnabled(true); if (previousState != null) { changed.setText(previousState); previousState = null; } } } }; checked.addActionListener(l); l.actionPerformed(null); } }