?
Solved

JAVA: How to customize  JFrame  background color?

Posted on 2009-03-29
9
Medium Priority
?
1,648 Views
Last Modified: 2012-05-06
HI, experts:

I tried to customize the background color of JFrame as the follows:

myFrame  =  new JFrame ( );

myFrame.setBackground  (new Color  ( 222, 222, 222 ));

But it did not change the color.

Thanks a lot!
0
Comment
Question by:duta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 200 total points
ID: 24013118
myFrame.getContentPane().setBackground(new Color  ( 222, 222, 222 ));
0
 
LVL 12

Assisted Solution

by:basav_com
basav_com earned 200 total points
ID: 24013482
0
 

Author Comment

by:duta
ID: 24013593
Masters, thank you so much.

I tried this: myFrame.getContentPane().setBackground(new Color  ( 222, 222, 222 ));
But it did not work for some reason.

I checked this site: http://java2everyone.blogspot.com/2009/02/jframe-tutorial-5-change-background.html

For a total newbie like me, it is a little challenge to locate a right tip from hundreds of links.

Thanks a lot!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 1600 total points
ID: 24013918
Can you post your full code please? Changing the panel's background usually does the job..
0
 
LVL 12

Expert Comment

by:basav_com
ID: 24013921
Post your code.
0
 

Author Comment

by:duta
ID: 24014075
Masters, thank you so much. My trimmed code is below in the Code Snippet.



import    java.awt.*;
import    javax.swing.*;
import    java.awt.event.*;
 
 
 
public  class  COLOR  extends   JFrame
{
   
  private   JFrame          myFrame;
    
  
   //  Constructor
   
   public COLOR (  )
   {
      
     
      setTitle  ( "Testing" );
      
      setSize   ( 400, 300 );
      
  
    
      setDefaultCloseOperation  ( JFrame.EXIT_ON_CLOSE );
      
  
     
      
      buildPanel ( );
      
      setVisible ( true );
   }    
  
    
   
      private  void  buildPanel ( )
      {
      
      
      
         setLayout ( new BorderLayout ( ) );
         
        
         myFrame  =  new JFrame ( );
         myFrame.getContentPane( ).setBackground( new Color  ( 255, 255, 100 ) );
      
      
      }
    
      
      
   
     
      // Main function
   
      public static void main ( String [ ] args )
      {
           new COLOR( );
      }

Open in new window

0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24014237
You created two JFrames, set the colour for one and then showed the other. Try this instead.
import    java.awt.*;
import    javax.swing.*;
import    java.awt.event.*;
 
 
 
public  class  COLOR  extends   JFrame
{
   
  private   JFrame          myFrame;
    
  
   //  Constructor
   
   public COLOR (  )
   {
      
     
      setTitle  ( "Testing" );
      
      setSize   ( 400, 300 );
      
  
    
      setDefaultCloseOperation  ( JFrame.EXIT_ON_CLOSE );
      
  
     
      
      buildPanel ( );
      
      setVisible ( true );
   }    
  
    
   
      private  void  buildPanel ( )
      {
      
      
      
         setLayout ( new BorderLayout ( ) );
         
        getContentPane( ).setBackground( new Color  ( 255, 255, 100 ) );
      
      
      }
    
      
      
   
     
      // Main function
   
      public static void main ( String [ ] args )
      {
           new COLOR( );
      }
 
}

Open in new window

0
 

Author Comment

by:duta
ID: 24014242
I could change the color of panels, but not the entire frame.
Thank you so much for your kind, patient help.

Sincerely,
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24014243
Also, you can get rid of this line:

  private   JFrame          myFrame;

as it's now redundant.

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question