Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: ben_g on July 16, 2011, 10:52:48 am

Title: Setting the background of a JFrame
Post by: ben_g on July 16, 2011, 10:52:48 am
I'm trying to set the background color of an undecorated JFrame to black, but the SetBackground methot does nothing.

My Window creation code:
Code: [Select]
JFrame.setDefaultLookAndFeelDecorated(true);
    Loading = new JFrame("");
    Loading.setAlwaysOnTop(true);
    Loading.setLayout(null);
    Loading.setUndecorated(true);
    Loading.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
    JLabel LoadingText = new JLabel("Loading... - Please wait.");
    LoadingText.setBounds(250,0,500,15);
    LoadingText.setHorizontalAlignment((int)JFrame.CENTER_ALIGNMENT);
    LoadingText.setForeground(Color.WHITE);
    LoadingBar.setMinimum(0);
    LoadingBar.setMaximum(100);
    LoadingBar.setValue(0);
    LoadingBar.setForeground(Color.GREEN);
    LoadingBar.setBackground(Color.RED);
    LoadingBar.setBounds(0,30,1000,30);
    Loading.getContentPane().add(LoadingText);
    Loading.getContentPane().add(LoadingBar);
    Loading.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width/2-500,Toolkit.getDefaultToolkit().getScreenSize().height/2-100,1000,200);
    Loading.setBackground(Color.BLACK);
    Loading.setFocusable(true);
    Loading.setVisible(true);

And this is the result:
(http://picturestack.com/827/743/Kj0UntitledFnd.png)

You can see that the SetBackground method hasn't done anything. Is there a way to make the background of a JFrame black?
Title: Re: Setting the background of a JFrame
Post by: Munchor on July 16, 2011, 11:19:20 am
Code: [Select]
import javax.swing.*;
import java.awt.*;

class jframtest {
        public static void main (String[] args) {
                JFrame myFrame = new JFrame();
                myFrame.setDefaultLookAndFeelDecorated(true);
                myFrame.setBackground(Color.BLACK);
                myFrame.setSize(400,400);
                myFrame.setVisible(true);
        }
}

I tried that and it didn't work either, so I investigated on it and found out:

The method "JFrame.setBackground()" has been deprecated on Swing.

In other words, everything about the JFrame relating to it being a container has been rendered void of use. Instead, you have to call javax.swing.JFrame.getContentPane() and use that container.

I tried this and it worked:

Code: [Select]
import javax.swing.*;
import java.awt.*;

class javaframetest {
        public static void main (String[] args) {
                JFrame myFrame = new JFrame();
                myFrame.setDefaultLookAndFeelDecorated(true);
                myFrame.getContentPane().setBackground(Color.BLACK);
                myFrame.setSize(400,400);
                myFrame.setVisible(true);
        }
}
Title: Re: Setting the background of a JFrame
Post by: ben_g on July 16, 2011, 12:39:34 pm
Thanks. It works now.
Title: Re: Setting the background of a JFrame
Post by: Munchor on July 24, 2011, 09:52:41 am
Thanks. It works now.

In fact, I even made a post on it on my blog, here (http://coderecycler.wordpress.com/2011/07/17/jframe-setbackground/).

I'm glad to help :)