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

Here you can find the source of implyDisabled(final JCheckBox checked, final boolean checkedState, final JTextComponent 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 JTextComponent changed) 

Method Source Code

//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);
    }
}

Related

  1. getPreferredScrollableViewportSize(javax.swing.text.JTextComponent t, Dimension ans)
  2. getRenderedLineOfChar(JTextComponent comp, int charIdx)
  3. getStoredBackground(JTextComponent comp)
  4. getStoredBackground(JTextComponent comp)
  5. highlighterIsNext(JTextComponent textComponent, boolean forwards, Highlighter.Highlight highlight)
  6. inScrollPane(JTextComponent textComponent, boolean autoscroll)
  7. installUi(JTextComponent txt)
  8. installUndoManager(JTextComponent textComponent)
  9. isCompletelyVisible(final JTextComponent scrollableComponent, final int pos, final Rectangle visibleRect)