Java JTextComponent setMandatoryBorder(JTextComponent comp)

Here you can find the source of setMandatoryBorder(JTextComponent comp)

Description

Sets the text component's border to use a new border that shall indicate that the component's content is mandatory.

Note: The component foreground and border colors are managed by the look&feel implementation.

License

Open Source License

Parameter

Parameter Description
comp the component that gets a new border

Declaration

public static void setMandatoryBorder(JTextComponent comp) 

Method Source Code


//package com.java2s;
import java.awt.Color;

import java.awt.Container;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;

import javax.swing.plaf.basic.BasicBorders;
import javax.swing.text.JTextComponent;

public class Main {
    private static final Color MANDATORY_FOREGROUND = new Color(70, 70, 210);
    /**/*from w  ww.j a va  2  s  . c o  m*/
     * Holds a cached Border that is used to indicate mandatory text components.
     * It will be lazily created in method {@link #getMandatoryBorder()}.
     * 
     * @see #getMandatoryBorder()
     * @see #setMandatoryBorder(JTextComponent)
     */
    private static Border mandatoryBorder;

    /**
     * Sets the text component's border to use a new border that shall indicate
     * that the component's content is mandatory.<p>
     * 
     * <strong>Note:</strong> The component foreground and border colors are 
     * managed by the look&amp;feel implementation. Many l&amp;fs will honor a
     * custom foreground color and custom border configuration. However, some 
     * l&amp;fs may ignore these custom settings. It is recommended to check
     * the appearance in all l&amp;fs available in an application.
     * 
     * @param comp   the component that gets a new border
     * 
     * @see #setMandatoryBackground(JTextComponent)
     * @see #getMandatoryBorder()
     */
    public static void setMandatoryBorder(JTextComponent comp) {
        Container parent = comp.getParent();
        if (parent instanceof JViewport) {
            Container grandpa = parent.getParent();
            if (grandpa instanceof JScrollPane) {
                ((JScrollPane) grandpa).setBorder(getMandatoryBorder());
                return;
            }
        }
        comp.setBorder(getMandatoryBorder());
    }

    /**
     * Lazily creates and returns a {@link Border} instance that is used
     * to indicate that a component's content is mandatory.
     * 
     * @return a <code>Border</code> that is used to indicate that 
     *     a component's content is mandatory
     */
    public static Border getMandatoryBorder() {
        if (mandatoryBorder == null) {
            mandatoryBorder = new CompoundBorder(new LineBorder(getMandatoryForeground()),
                    new BasicBorders.MarginBorder());
        }
        return mandatoryBorder;
    }

    /**
     * Returns a default foreground color that can be used as the component 
     * foreground for components with mandatory content. Typically this
     * color will be used with instances of {@link JTextComponent}.<p>
     * 
     * <strong>Note:</strong> The component foreground and border colors are 
     * managed by the look&amp;feel implementation. Many l&amp;fs will honor a
     * custom foreground color and custom border configuration. However, some 
     * l&amp;fs may ignore these custom settings. It is recommended to check 
     * the appearance in all l&amp;fs available in an application.
     * 
     * @return a foreground color useful for components with mandatory content
     * 
     * @see #getMandatoryBackground()
     * @see #getMandatoryBorder()
     */
    public static Color getMandatoryForeground() {
        return MANDATORY_FOREGROUND;
    }
}

Related

  1. scrollToTop(JTextComponent edit1)
  2. setError(JTextComponent component, boolean error)
  3. setErrorBackground(JTextComponent comp)
  4. setMandatoryBackground(JTextComponent comp)
  5. setMandatoryBorder(JTextComponent comp)
  6. setText(JTextComponent textComp, String text)
  7. setTextComponentMargins(JTextComponent component)
  8. setTextComponentTransparent( JTextComponent paramJTextComponent)
  9. setTextComponentTransparent( JTextComponent textComponent)