Solved

JAVA: How to customize  JFrame  background color?

Posted on 2009-03-29
9
1,599 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
ID: 24013118
myFrame.getContentPane().setBackground(new Color  ( 222, 222, 222 ));
0
 
LVL 12

Assisted Solution

by:basav_com
basav_com earned 50 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
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 400 total points
ID: 24013918
Can you post your full code please? Changing the panel's background usually does the job..
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
what is the difference between "sudo su" and "su - root" 6 106
servlet  URL Rewriting 1 28
Way to decrease size of apk file 9 46
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

26 Experts available now in Live!

Get 1:1 Help Now