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.

JW_
  • 652
  • 1
  • 8
  • 22
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 14 '12 at 08: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 '12 at 08:43
  • Could it be because java uses sRGB instead of RGB? :P – JW_ Mar 14 '12 at 08:50
  • [Bugreport](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6384420) on official bugtracker. And it is really not a bug. – lxbndr Mar 14 '12 at 08:59

7 Answers7

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();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • It is in the constructor of a class that extends JFrame? – JW_ Mar 14 '12 at 09: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 '12 at 10:26
  • @JW_ this is reason why we asking for [SSCCE](http://sscce.org/), nobody knows what ... :-) – mKorbel Mar 14 '12 at 10:37
  • @mKorbel's result verified using [`Zoom`](http://stackoverflow.com/a/3742841/230513). I suspect an errant composite mode. – trashgod Mar 14 '12 at 17:17
  • It's the right answer that getContentPane() needs to be called on the setBackground() – JW_ Mar 14 '12 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.

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
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);
Dylan Turner
  • 322
  • 1
  • 12
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);
    }
}
BUKJACK
  • 1
  • 1
0

First step - make an object of jFrame:

JFrame frame = new JFrame();

Second step:

frame.getContentPane().setBackground(new Color(16,144,144));
Adil B
  • 14,635
  • 11
  • 60
  • 78
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));
    }

}

  • 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 '19 at 17:33
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);
}}
Hachi
  • 3,237
  • 1
  • 21
  • 29