Here you can find the source of setMandatoryBorder(JTextComponent comp)
Note: The component foreground and border colors are managed by the look&feel implementation.
Parameter | Description |
---|---|
comp | the component that gets a new border |
public static void setMandatoryBorder(JTextComponent comp)
//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&feel implementation. Many l&fs will honor a * custom foreground color and custom border configuration. However, some * l&fs may ignore these custom settings. It is recommended to check * the appearance in all l&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&feel implementation. Many l&fs will honor a * custom foreground color and custom border configuration. However, some * l&fs may ignore these custom settings. It is recommended to check * the appearance in all l&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; } }