Solved

JAVA: How to customize  JFrame  background color?

Posted on 2009-03-29
9
1,587 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 50 total points
Comment Utility
myFrame.getContentPane().setBackground(new Color  ( 222, 222, 222 ));
0
 
LVL 12

Assisted Solution

by:basav_com
basav_com earned 50 total points
Comment Utility
0
 

Author Comment

by:duta
Comment Utility
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
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 400 total points
Comment Utility
Can you post your full code please? Changing the panel's background usually does the job..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Post your code.
0
 

Author Comment

by:duta
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Also, you can get rid of this line:

  private   JFrame          myFrame;

as it's now redundant.

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now