How do you set the background color for a JFrame
?
10 Answers
Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color.
Example:
myJFrame.getContentPane().setBackground( desiredColor );

- 24,881
- 6
- 47
- 71
To set the background color for JFrame:
getContentPane().setBackground(Color.YELLOW); //Whatever color

- 551
- 5
- 11
using:
setBackground(Color.red);
doesn't work properly.
use
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
or
myJFrame.getContentPane().setBackground( Color.red );
-
5Why to answer if there is already the same answer made by other users? – Filipe Brito Sep 18 '15 at 02:53
-
his first statement makes sense i.e setBackGround doesn't work properly. – crackerplace Mar 01 '16 at 16:55
To set the background color for the JFrame try this:
this.getContentPane().setBackground(Color.white);
-
I feel this is a suggestion for a solution which may or may not be the answer. If the OP tries this and finds it to be working, they can ask you to add as answer to mark it so. This is how I usually work. – midhunhk Jan 11 '17 at 13:54
-
Thanks for this great solution. It seems that the "C" in Color is case-sensitive. – Ahm23 Mar 17 '19 at 17:06
This is the simplest and the correct method. All you have to do is to add this code after initComponents();
getContentPane().setBackground(new java.awt.Color(204, 166, 166));
That is an example RGB color, you can replace that with your desired color. If you dont know the codes of RGB colors, please search on internet... there are a lot of sites that provide custom colors like this.

- 1,455
- 14
- 19
Hello There I did have the same problem and after many attempts I found that the problem is that you need a Graphics Object to be able to draw, paint(setBackgroundColor).
My code usually goes like this:
import javax.swing.*;
import java.awt.*;
public class DrawGraphics extends JFrame{
public DrawGraphics(String title) throws HeadlessException {
super(title);
InitialElements();
}
private void InitialElements(){
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// This one does not work
// getContentPane().setBackground(new Color(70, 80, 70));
}
public void paint(Graphics draw){
//Here you can perform any drawing like an oval...
draw.fillOval(40, 40, 60, 50);
getContentPane().setBackground(new Color(70,80,70));
}
}
The missing part on almost all other answers is where to place the code. Then now you know it goes in paint(Graphics G)

- 12,507
- 5
- 54
- 54
-
we __must not__ change the state of the component during the paint process. Here it might appear to work because it changes the state of a child - which also is a no-no-never, its working or not depends on implememtation details (f.i. if you call super.paint - which is mandatory but missing here .. for shortness I assume - before/after changing child state). Unrelated nit: please stick to java naming conventions when showing java code publicly. – kleopatra Mar 06 '23 at 11:25
Here's another method:
private void RenkMouseClicked(java.awt.event.MouseEvent evt) {
renk = JColorChooser.showDialog(null, "Select the background color",
renk);
Container a = this.getContentPane();
a.setBackground(renk);
}
I'm using netbeans ide. For me, JFrame.getContentPane()
didn't run. I used JFrame.getContentPane()
's class equivalent this.getContentPane
.

- 9,474
- 36
- 90
- 105

- 29
- 1
you can override the paint method of JFrame and then fill that by your favorite color like this:
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}

- 126
- 1
- 5
I had trouble with changing the JFrame background as well and the above responses did not solve it entirely. I am using Eclipse. Adding a layout fixed the issue.
public class SampleProgram extends JFrame {
public SampleProgram() {
setSize(400,400);
setTitle("Sample");
getContentPane().setLayout(new FlowLayout());//specify a layout manager
getContentPane().setBackground(Color.red);
setVisible(true);
}

- 19
- 1