Setting background color for a JFrame
JavaSwingGraphicsAwtJava Problem Overview
Just how do you set the background color for a JFrame?
Java Solutions
Solution 1 - Java
Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color.
Example:
myJFrame.getContentPane().setBackground( desiredColor );
Solution 2 - Java
To set the background color for JFrame:
getContentPane().setBackground(Color.YELLOW); //Whatever color
Solution 3 - Java
using:
setBackground(Color.red);
doesn't work properly.
use
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
or
myJFrame.getContentPane().setBackground( Color.red );
Solution 4 - Java
To set the background color for the JFrame try this:
this.getContentPane().setBackground(Color.white);
Solution 5 - Java
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.
Solution 6 - Java
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)
Solution 7 - Java
You can use a container like so:
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
You must of course import java.awt.Color
for the red color constant.
Solution 8 - Java
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
.
Solution 9 - Java
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);
}
Solution 10 - Java
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());
}
Solution 11 - Java
You can use this code block for JFrame background color.
JFrame frame = new JFrame("Frame BG color");
frame.setLayout(null);
frame.setSize(1000, 650);
frame.getContentPane().setBackground(new Color(5, 65, 90));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
Solution 12 - Java
public nameOfTheClass() {
final Container c = this.getContentPane();
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.white);
}
}
Solution 13 - Java
Resurrecting a thread from stasis.
In 2018 this solution works for Swing/JFrame in NetBeans (should work in any IDE :):
this.getContentPane().setBackground(Color.GREEN);
Solution 14 - Java
import java.awt.*;
import javax.swing.*;
public class MySimpleLayout extends JFrame {
private Container c;
public MySimpleLayout(String str) {
super(str);
c=getContentPane();
c.setLayout(null);
c.setBackground(Color.WHITE);
}
}
Solution 15 - Java
Probably the SIMPLEST method is this:
super.setBackground(Color.CYAN);
You must extend JFrame in the class before doing this!