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.

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.JScrollPane;

import javax.swing.JViewport;

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

import javax.swing.text.JTextComponent;

public class Main {
    public static final Color MANDATORY_FOREGROUND = new Color(115, 115, 170);
    /**//  www.  j  a v  a  2s.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(new CompoundBorder(getMandatoryBorder(), ((JScrollPane) grandpa).getBorder()));
                return;
            }
        }
        if (comp.getBorder() == getMandatoryBorder())
            return;
        if ((comp.getBorder() instanceof CompoundBorder)
                && (((CompoundBorder) comp.getBorder()).getOutsideBorder() == getMandatoryBorder()))
            return;
        comp.setBorder(new CompoundBorder(getMandatoryBorder(), comp.getBorder()));
    }

    /**
     * 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(), 1, true),
                    new EmptyBorder(1, 1, 1, 1));
            //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. scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex)
  2. scrollToTop(JTextComponent edit1)
  3. setError(JTextComponent component, boolean error)
  4. setErrorBackground(JTextComponent comp)
  5. setMandatoryBackground(JTextComponent comp)
  6. setMandatoryBorder(JTextComponent comp)
  7. setText(JTextComponent textComp, String text)
  8. setTextComponentMargins(JTextComponent component)
  9. setTextComponentTransparent( JTextComponent paramJTextComponent)