Java Utililty Methods JTextComponent

List of utility methods to do JTextComponent

Description

The list of methods to do JTextComponent are organized into topic(s).

Method

voidsetErrorBackground(JTextComponent comp)
Sets the text component's background to a color that shall indicate that the component's content has is invalid with error severity.
comp.setBackground(ERROR_BACKGROUND);
voidsetMandatoryBackground(JTextComponent comp)
Sets the text component's background to a color that shall indicate that the component's content is mandatory.
comp.setBackground(MANDATORY_BACKGROUND);
voidsetMandatoryBorder(JTextComponent comp)
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.

Container parent = comp.getParent();
if (parent instanceof JViewport) {
    Container grandpa = parent.getParent();
    if (grandpa instanceof JScrollPane) {
        ((JScrollPane) grandpa).setBorder(getMandatoryBorder());
        return;
comp.setBorder(getMandatoryBorder());
voidsetMandatoryBorder(JTextComponent comp)
Sets the text component's border to use a new border that shall indicate that the component's content is mandatory.
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()));
voidsetText(JTextComponent textComp, String text)
sets text of textComp without moving its caret.
if (text == null)
    text = "";
if (textComp.getCaret() instanceof DefaultCaret) {
    DefaultCaret caret = (DefaultCaret) textComp.getCaret();
    int updatePolicy = caret.getUpdatePolicy();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    try {
        textComp.setText(text);
...
voidsetTextComponentMargins(JTextComponent component)
set Text Component Margins
setTextComponentMargins(component, DEFAULT_TEXT_COMPONENT_VERTICAL_MARGIN,
        DEFAULT_TEXT_COMPONENT_HORIZONAL_MARGIN);
voidsetTextComponentTransparent(JTextComponent paramJTextComponent)
set Text Component Transparent
paramJTextComponent.setOpaque(false);
paramJTextComponent.putClientProperty("Synthetica.opaque", Boolean.valueOf(false));
paramJTextComponent.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.valueOf(false));
paramJTextComponent.putClientProperty("Nimbus.Overrides", new UIDefaults());
voidsetTextComponentTransparent(JTextComponent textComponent)
set Text Component Transparent
textComponent.setOpaque(false);
textComponent.putClientProperty("Synthetica.opaque", Boolean.valueOf(false));
textComponent.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.valueOf(false));
textComponent.putClientProperty("Nimbus.Overrides", new UIDefaults());
voidsetTextComponentTransparent(JTextComponent textComponent)
Sets the text component transparent.
textComponent.setOpaque(false);
textComponent.putClientProperty("Synthetica.opaque", false);
textComponent.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
textComponent.putClientProperty("Nimbus.Overrides", new UIDefaults());
voidsetTextComponetIntegerValid(final JTextComponent c)
Set the text component in which only numerics (positive decimal integers with character 0-9).
c.addKeyListener(new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() != KeyEvent.VK_BACK_SPACE)
            try {
                Integer.valueOf(c.getText() + e.getKeyChar());
            } catch (NumberFormatException nfe) {
                e.consume();
...