6

I have a program with some gui, on the JFrame I set,

 setBackground( new Color(107, 106, 104) );

The issue is that I get a greyish color, but not the right one. If I check it in PhotoShop, it gives me the RGB values (126, 125, 123)

Ps. I have tried with HEX value, the same result.

4
  • 2
    For better help sooner, post an SSCCE. Mar 14, 2012 at 8:40
  • I'd expect that color to be grey because the RGB values are so close. Colors between 0,0,0 (black) and 255,255,255 (white) are "grey" as long as each value is "similar"
    – Adam
    Mar 14, 2012 at 8:43
  • Could it be because java uses sRGB instead of RGB? :P
    – JW_
    Mar 14, 2012 at 8:50
  • Bugreport on official bugtracker. And it is really not a bug.
    – lxbndr
    Mar 14, 2012 at 8:59

7 Answers 7

9
I have a program with some gui, on the JFrame I set,

 setBackground( new Color(107, 106, 104) );

[The problem] It gives a greyish color, but not the right one! 
If I check the gui's color in Photo Shop, it gives me the RGB 
values (126, 125, 123)

you can not set setBackground for JFrame, this is only possible for ContentPane, for example

JFrame#getContentPane.setBackground(new Color(107, 106, 104));

EDIT

enter image description here

from code

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Check extends JFrame {

    private static final long serialVersionUID = 1L;

    public void makeUI() {
        JFrame f = new JFrame();
        f.getContentPane().setBackground(new Color(107, 106, 104));
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setSize(new Dimension(300, 200));
        f.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Check().makeUI();
            }
        });
    }
}
5
  • It is in the constructor of a class that extends JFrame?
    – JW_
    Mar 14, 2012 at 9:41
  • mKorbel, I have tested your code, it works like a charm. But the way i'am doing it, it won't work and I can't see why.
    – JW_
    Mar 14, 2012 at 10:26
  • @JW_ this is reason why we asking for SSCCE, nobody knows what ... :-)
    – mKorbel
    Mar 14, 2012 at 10:37
  • @mKorbel's result verified using Zoom. I suspect an errant composite mode.
    – trashgod
    Mar 14, 2012 at 17:17
  • It's the right answer that getContentPane() needs to be called on the setBackground()
    – JW_
    Mar 14, 2012 at 19:16
1

check with Adam's comment and even if not worked then without any working code I am just guessing that this scenario is getting raised due zero ordering or saying layout of the JFrame. Actually in java swing , setting the background color needs a little bit of more attention, check Swing Java Docs.

0
0

i've tried what you explained; in awt it's no problem; in swing it seems the background is not set properly
did you check, if your background changes, e.g. with setBackground(Color.red)?

example Code:

import java.awt.*;
import javax.swing.*;

public class Tmp extends Frame { public static void main(String[] args) {
    //Frame tmp = new Frame();
    Frame tmp = new JFrame();
    tmp.setBackground(new Color(107, 106, 104));
    tmp.setSize(40,40);
    tmp.setVisible(true);
}}
0
0

http://www.tayloredmktg.com/rgb/

It looks like gray is at the top of the page riht when you open it. :) Also make sure your JFrame is opaque or you won't see your color!

setOpaque(true);
0

This worked for me. Hope it helps The, code, adds a JPanel, to current JFrame, you can further build guis on this panel. You can customize the RGB colours, on JPanel, not on JFrame.

import javax.swing.*;
import java.awt.*;

public class Main{

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        //Class class = new Class();
        frame.setSize(1920,1080);
        //frame.setTitle("XYZ");
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        panel.setBackground(new Color(51,153,255));
        //panel.add(class);
    }
}
1
  • Don't post code only, add an explanation so that readers can better understand your answer
    – Spoody
    Mar 25, 2018 at 13:57
0

First step - make an object of jFrame:

JFrame frame = new JFrame();

Second step:

frame.getContentPane().setBackground(new Color(16,144,144));
0
 if(evt.getSource() == jMenuItem11){
        getContentPane().setBackground(new Color(170, 8, 54));
    }
    if(evt.getSource() == jMenuItem12){
        getContentPane().setBackground(new Color(8, 54, 169));
    }
    if(evt.getSource() == jMenuItem13){
       getContentPane().setBackground(new Color(84, 8, 170));
    }

}

1
  • Please con't post code-only answers. You need to explain what your code does/how it fixes the problem. Even if you think it's good enough for this one question, the site requires keeping a certain minimal standard of commentary, and there are reasons for it.
    – clearlight
    Jun 21, 2019 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.