Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setBackground(); isn't working  RSS feed

 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class the extends JComponent. I try to use the setBackground(new Color(0,153,0)) method inside the constructor that I have for it. It does not seem to be working. Any ideas?
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Noah,
what component is it you're trying to change the background. if its a JFrame/JInternalFrame you must set the background of the container (ContentPane). if its a scrollpane you need to set the background of the viewport.
chantal
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an applet that has a JPanel on the NORTH side, in the middle is a jcomponent that i wrote, on the SOUTH is another JPanel. I want to be able to change the background color of my jcomponent class, but it doesn't work.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah,
if you for instance want to set the background of a JLabel or JButton (both JComponents) you have to add the line
setOpaque(true) first. Else you will not see any difference when you run your program.
Hope this helps )
Patrick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!