• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane background color  RSS feed

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have a JScrollPane that contains a JLabel. The function of this section of the program is to open an image onto that JLabel. But what I need is two things;
1. I need to have the area inside the scroll pane to be white before the image loads.
2. Once the image has loaded, I need the area around the image to be white.

Here is the code that I've tried to do it, but it doesn't work, the background stays gray.


JPanel compressedImagePanel = new JPanel(new BorderLayout());
compressedImagePanel.setBackground(Color.WHITE);
JLabel compressedImageLabel = new JLabel();
compressedImageLabel.setBackground(Color.WHITE);

JScrollPane comPane = new JScrollPane(compressedImageLabel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
comPane.setBackground(Color.WHITE);

compressedImagePanel.add(comPane);

As you can see, I've set EVERYTHING to white. Am I doing it in the wrong order or is there another trick to it?

Thanks
Rachel
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, a couple of things.

1. The background of a JLabel is not opaque or transparent. So you can set the background color all day long but unless you call setOpaque(true) on the JLabel you will never see it.

2. When you use a JScrollPane what you see as the background is not actually the JScrollPane background. You see the ViewPort. So you have to change the background color of the viewport, not the scrollpane.

scroll.getViewPort().setBackground(Color.white);

BTW - I didn't know this about the JScrollPane until I investigated your question, however I learned this from reading the API for the JScrollPane. Becoming familiar with the API is essential to becoming a good Java developer.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the solution. Usually I'm very good with the API, but I guessed that day I just skipped down to the methods and didn't think that I was trying it wrong! I'll be more carefull next time.

Thanks again!
Rachel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!