Pumpernickel
Pumpernickel

Reputation: 47

Background color not being painted underneath JTextArea

I'm using Swing to create a GUI. I have a JPanel, PanelOne, which has a background color of blue. On the panel, I have a JTextArea, instructions, that also has a background color of blue and has opaque set to true.

Upon starting the program, everything looks good, there is a blue background with black text for the instructions. In the code, I then call instructions.setVisible(false) when I no longer want the instructions to be displayed. However, when I do this, a big empty gray box is left behind where the label had been. From what I read, I thought making the label opaque would fix this issue, but that doesn't seem to be the case. How do I ensure that the area under the label is also painted blue?

Upvotes: 1

Views: 104

Answers (3)

Pumpernickel
Pumpernickel

Reputation: 47

I was mistaken, it was actually a JTextArea inside of a JScrollPane that was leaving behind the gray box, not a JLabel as I originally thought. I've updated the question and title to reflect this. The solution was very simple - I had to set opaque to true for the JScrollPane that contained my JTextArea. Thank you to everyone who answered for the tips!

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

Don't set the label opaque or try to hide it. Once the text is irrelevant, call label.setText("");. Done!

A label with no text, visible border or icon is invisible.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class HideLabel {

    private JComponent ui = null;

    HideLabel() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout());
        ui.setBorder(new EmptyBorder(4,4,4,4));
        ui.setBackground(Color.CYAN);
        
        final JLabel label = new JLabel("Click the button!");
        ui.add(label);
        AbstractAction action = new AbstractAction("'Hide' the label") {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("");
            }
        };
        JButton hideButton = new JButton(action);
        ui.add(hideButton, BorderLayout.LINE_END);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            HideLabel o = new HideLabel();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 3

Zues The God
Zues The God

Reputation: 21

If you no longer want the instructions to be displayed you can do

PanelOne.remove(instructions);
PanelOne.revalidate();
PanelOne.repaint();

Upvotes: -1

Related Questions