Java JTextField implyDisabled(final JCheckBox checked, final boolean checkedState, final JTextField changed)

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

Description

Checks state of the checked checkbox and if state is checkedState than to disable changed text field and clean it.

License

Apache License

Parameter

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

Declaration

public static void implyDisabled(final JCheckBox checked, final boolean checkedState,
        final JTextField changed) 

Method Source Code


//package com.java2s;
/*//  w w  w .  j av a  2 s.c om
 * Copyright 2000-2009 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}
     * 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 JTextField 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);
    }
}

Related

  1. createFieldWithLabel(String label, JTextField textField)
  2. createOutputPanel(JLabel outputFileNameLabel, JButton chooseOutputFileButton, JTextField outputFileTextField, String borderTitle)
  3. dateCompleteOnlyFormat(JTextField pJTextField, KeyEvent e)
  4. doAction(JTextField textField)
  5. fieldSetErrorVisual(final JTextField field)
  6. isActiveComponents(JTextField... textFields)
  7. isEmptyStr(javax.swing.JTextField input)
  8. isInHighlight(MouseEvent e, JTextField label, Highlighter h)
  9. isJTextFieldNotEmpty(javax.swing.JTextField field)