Search code examples
javaswingjpaneljlabel

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?


Solution

  • 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!