bigleftie
bigleftie

Reputation: 453

Disabling java borders

I would like to disable any borders that a Component has when the Component is disabled.

When you run the code below as-is, you'll see a red LineBorder around one of the text areas. I'd like to have that border disabled when it's TextArea is disabled.

Is there code that can be placed in the useGenericSolution block in the code below that will work for all Borders?

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class EnableBorder extends JFrame
{
    private static final long   serialVersionUID    = 1L;
    private static JFrame       _instance;

    public EnableBorder()
    {
        setResizable(false);
        setTitle("My Frame");
        setSize(300, 300);
        getContentPane().add(new MyPanel());
        _instance = this;
    }

    private class MyPanel extends JPanel
    {
        private static final long   serialVersionUID    = 1L;

        public MyPanel()
        {
            setLayout(new FlowLayout());
            JButton btnDisableAll = new JButton("Disable everything");
            btnDisableAll.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    EnableBorder.setEnabledAllComponents(_instance, false);
                }
            });
            add(btnDisableAll);

            // Use TitledBorder
            JTextArea titledTA = new JTextArea("This is some text that should be wide enough ");
            titledTA.setSize(400, 50);
            titledTA.setBorder(new TitledBorder("A titled border"));
            add(titledTA);

            // Use LineBorder
            JTextArea lineBorderTA = new JTextArea("This is just some more text ...");
            lineBorderTA.setSize(400, 50);
            lineBorderTA.setBorder(new LineBorder(Color.RED, 1, true));
            add(lineBorderTA);
        }
    }

    /**
     * Enables or disables a Container and all Components within a Container.
     * 
     * @param b
     *            - The value to set the 'enabled' flag to
     */
    public static void setEnabledAllComponents(Container cont, boolean b)
    {
        for (Component c : cont.getComponents())
        {
            setEnabledComponent(c, b);
        }
        setEnabledComponent(cont, b);
    }

    /**
     * Enables or disables a Component.
     * <p>
     * If the component is a Container, all of it the Components within the Container will also be modified.
     * 
     * @param c
     *            - The Component to modify
     * @param b
     *            - The value to set the 'enabled' flag to
     */
    public static void setEnabledComponent(Component c, boolean b)
    {

        if (c instanceof Container)
        {
            for (Component containerComp : ((Container) c).getComponents())
            {
                setEnabledComponent(containerComp, b);
            }
        }

        c.setEnabled(b);

        if (c instanceof JComponent)
        {

            Border border = ((JComponent) c).getBorder();
            if (border != null)
            {
                boolean useGenericSolution = true;
                if (useGenericSolution)
                {
                    Insets insets = border.getBorderInsets(c);
                    System.out.println("Insets: " + insets);
                    Graphics g = c.getGraphics();

                    if (g != null)
                    {
                        /*
                         * TODO: Is it possible to get foreground color from the current object
                         * and force the border to be painted using that color 
                         */

                        Color color = g.getColor();

                        border.paintBorder(c, g, c.getX() - 5 - insets.left, c.getY() - insets.top, c.getWidth() + insets.left + insets.right,
                                c.getHeight() + insets.top + insets.bottom);
                    }
                }
                else
                {
                    // TitledBorders can be done this way... but, a generic solution would be better
                    if (border instanceof TitledBorder)
                    {
                        if (b)
                        {
                            // Change border colors to color found in an enabled label
                            ((TitledBorder) border).setTitleColor((Color) UIManager.get("Label.enabledForeground"));
                        }
                        else
                        {
                            // Change border colors to color found in a disabled label
                            ((TitledBorder) border).setTitleColor((Color) UIManager.get("Label.disabledForeground"));
                        }
                    }
                }
            }
        }

    }

    private static void createAndShowGUI()
    {
        // Create and set up the window.
        EnableBorder frame = new EnableBorder();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    createAndShowGUI();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    System.exit(0);
                }
            }
        });
    }

}

Upvotes: 2

Views: 1025

Answers (1)

Reg
Reg

Reputation: 11215

Just add the following line in the generic Solution block

if (useGenericSolution) {
    ((JComponent) c).setBorder(BorderFactory.createEmptyBorder());
}

Upvotes: 1

Related Questions