File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes setLocationRelativeTo(null) - problems with setBackground Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setLocationRelativeTo(null) - problems with setBackground" Watch "setLocationRelativeTo(null) - problems with setBackground" New topic
Author

setLocationRelativeTo(null) - problems with setBackground

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Whenever i add this 'setLocationRelativeTo(null)' my setBackground is disregarded. Why is this??

Here is the part of the class with setLocation


Here is the class where setBackground is being defined



Stephen Foy - Microsoft Application Development Consultant
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Have you tried adding



as the first line in paint?
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Yep, still the same problem though.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The call to setOpaque(true) should be moved up into the constructor; it only needs to be called once. Similarly, the call g.setColor(Color.black) can be moved up above the for loop.
It's okay to override the paint method in your JComponent class. For custom drawing in which you do not want/need to draw over child components it's better to override paintComponent

The setBackground call won't do anything in the JComponent unless you use it like this

Even easier is to extend JPanel instead of JComponent. Then you can place the setBackground call in the constructor, omit the setOpaque call and

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Thanks alot! thats spot on. Thanks for the tips also, theres always room to improve!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setLocationRelativeTo(null) - problems with setBackground