Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 May, 2013, 00:25:07 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Setting the background of a JFrame -  (Read 663 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
ben_g
LV7 Elite (Next: 700)
*******
Online Online

Gender: Male
Last Login: Today at 00:09:59
Date Registered: 08 May, 2011, 21:03:27
Location: (ix+$43)
Posts: 645


Topic starter
Total Post Ratings: +74

View Profile
« on: 16 July, 2011, 16:52:48 »
0

I'm trying to set the background color of an undecorated JFrame to black, but the SetBackground methot does nothing.

My Window creation code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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:


You can see that the SetBackground method hasn't done anything. Is there a way to make the background of a JFrame black?
Logged

Spoiler for completely unimportant stuff:
My projects
 - A base converter that also supports signed and fixed-point numbers.
 - A first person shooter with a polygon-based 3d engine.
Spoiler for latest screenshot:
- A java MORPG.
Spoiler for screenshot:
- a minecraft game in axe
Spoiler for Only open this spoiler if you want to read what's inside:
What's inside.
From when I saw this image, I never lost the game again!
Spoiler for This is [Edit:
not[/Edit] my contest entry. I hope you like it.]
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 07 May, 2013, 22:49:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Total Post Ratings: +174

View Profile
« Reply #1 on: 16 July, 2011, 17:19:20 »
+1


1
2
3
4
5
6
7
8
9
10
11
12
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:


1
2
3
4
5
6
7
8
9
10
11
12
13
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);
        }
}
« Last Edit: 16 July, 2011, 17:20:37 by ephan » Logged
ben_g
LV7 Elite (Next: 700)
*******
Online Online

Gender: Male
Last Login: Today at 00:09:59
Date Registered: 08 May, 2011, 21:03:27
Location: (ix+$43)
Posts: 645


Topic starter
Total Post Ratings: +74

View Profile
« Reply #2 on: 16 July, 2011, 18:39:34 »
0

Thanks. It works now.
Logged

Spoiler for completely unimportant stuff:
My projects
 - A base converter that also supports signed and fixed-point numbers.
 - A first person shooter with a polygon-based 3d engine.
Spoiler for latest screenshot:
- A java MORPG.
Spoiler for screenshot:
- a minecraft game in axe
Spoiler for Only open this spoiler if you want to read what's inside:
What's inside.
From when I saw this image, I never lost the game again!
Spoiler for This is [Edit:
not[/Edit] my contest entry. I hope you like it.]
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 07 May, 2013, 22:49:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Total Post Ratings: +174

View Profile
« Reply #3 on: 24 July, 2011, 15:52:41 »
0

Thanks. It works now.

In fact, I even made a post on it on my blog, here.

I'm glad to help Smiley
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.186 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.