JFrame Background Color

B

bruce

I'm new at this NetBeans/Java stuff and appreciate the help.

How do I set the background color on my initial JFrame when creating a
GUI page? I went to the properties window and set the background
property and nothing seemed to happen. No problem setting the
background on JPanels object.

What I thought of was placing a "Master JPanel" that would cover the
complete JFrame.. Seems "Hookey" but maybe that's the correct way to
do what I want to do..

Thanks...
 
K

Knute Johnson

I'm new at this NetBeans/Java stuff and appreciate the help.

How do I set the background color on my initial JFrame when creating a
GUI page? I went to the properties window and set the background
property and nothing seemed to happen. No problem setting the
background on JPanels object.

What I thought of was placing a "Master JPanel" that would cover the
complete JFrame.. Seems "Hookey" but maybe that's the correct way to
do what I want to do..

Thanks...

import java.awt.*;
import javax.swing.*;

public class test extends JFrame {
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLUE);
setSize(400,300);
setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new test();
}
});
}
}
 
B

bruce

I'm new at this NetBeans/Java stuff and appreciate the help.
How do I set the background color on my initial JFrame when creating a
GUI page? I went to the properties window and set the background
property and nothing seemed to happen. No problem setting the
background on JPanels object.
What I thought of was placing a "Master JPanel" that would cover the
complete JFrame.. Seems "Hookey" but maybe that's the correct way to
do what I want to do..
Thanks...

import java.awt.*;
import javax.swing.*;

public class test extends JFrame {
     public test() {
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         getContentPane().setBackground(Color.BLUE);
         setSize(400,300);
         setVisible(true);
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 new test();
             }
         });
     }

}

Thanks... Works like a charm... Appreciate the comeback...

Bruce
 
K

Knute Johnson

I'm new at this NetBeans/Java stuff and appreciate the help.
How do I set the background color on my initial JFrame when creating a
GUI page? I went to the properties window and set the background
property and nothing seemed to happen. No problem setting the
background on JPanels object.
What I thought of was placing a "Master JPanel" that would cover the
complete JFrame.. Seems "Hookey" but maybe that's the correct way to
do what I want to do..
Thanks...

import java.awt.*;
import javax.swing.*;

public class test extends JFrame {
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLUE);
setSize(400,300);
setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new test();
}
});
}

}

Thanks... Works like a charm... Appreciate the comeback...

Bruce

Note that the content pane for a JFrame is in fact a JPanel.

put this line in to see;

System.out.println(getContentPane().getClass());
 
J

John B. Matthews

Knute Johnson said:
I'm new at this NetBeans/Java stuff and appreciate the help.

How do I set the background color on my initial JFrame when
creating a GUI page? I went to the properties window and set the
background property and nothing seemed to happen. No problem
setting the background on JPanels object.

What I thought of was placing a "Master JPanel" that would cover
the complete JFrame.. Seems "Hookey" but maybe that's the correct
way to do what I want to do..

import java.awt.*;
import javax.swing.*;

public class test extends JFrame {
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLUE);
setSize(400,300);
setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new test();
}
});
}
}

bruce: When I chose File > New File > Swing GUI Forms > JFrame Form,
the resulting design showed "background" among the "Other Properties",
and the displayed background reflected the chosen color. I found it
instructive to compare the generated code with Knute Johnson's example.
 
B

bruce

import java.awt.*;
import javax.swing.*;
public class test extends JFrame {
     public test() {
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         getContentPane().setBackground(Color.BLUE);
         setSize(400,300);
         setVisible(true);
     }
     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 new test();
             }
         });
     }
}

bruce: When I chose File > New File > Swing GUI Forms > JFrame Form,
the resulting design showed "background" among the "Other Properties",
and the displayed background reflected the chosen color. I found it
instructive to compare the generated code with Knute Johnson's example.

John: Before I made my original message post, I tried to change the
background property, under other properties. No Joy!!

I tried to follow your senerio: File > New File? Swing GUI Forms ?
JFrame Form > Next > Finish. I then went to the properties, found
backgound color under "Other Properties" and set it to 0,0,255. Save.
Again, No joy..

No sure how you were able to get it work an I can't..

I'm using: Product Version: NetBeans IDE 6.7.1 (Build 200907230233)

Bruce
 
J

John B. Matthews

bruce said:
Not sure how you were able to get it work an I can't..

I'm using: Product Version: NetBeans IDE 6.7.1 (Build 200907230233)

If it makes a difference, I'm using NetBeans 6.9.1. I had to edit the
projects' properties to make sure I was running the new JFrame's main()
method. I added a smaller JPanel with a different background, just to
see the effect. My JFrame's initComponents() then included these lines:

setBackground(new java.awt.Color(0, 255, 255));
colorPanel.setBackground(new java.awt.Color(255, 153, 0));

You might verify that at least some of the JFrame's background is
showing.
 
K

Knute Johnson

If it makes a difference, I'm using NetBeans 6.9.1. I had to edit the
projects' properties to make sure I was running the new JFrame's main()
method. I added a smaller JPanel with a different background, just to
see the effect. My JFrame's initComponents() then included these lines:

setBackground(new java.awt.Color(0, 255, 255));
colorPanel.setBackground(new java.awt.Color(255, 153, 0));

You might verify that at least some of the JFrame's background is
showing.

John:

What is initComponents()? Is that something that NetBeans makes creates
to initialize the frame? I'm curious how making a setBackground() call
in that method is directed to the content pane.

Thanks,
 
M

markspace

What is initComponents()? Is that something that NetBeans makes creates
to initialize the frame?
Yes.

I'm curious how making a setBackground() call
in that method is directed to the content pane.


He does it manually: he creates a JPanel, sets its background color and
then adds it to content pane:

colorPanel.setBackground(new java.awt.Color(255, 0, 255));
colorPanel.setPreferredSize(new java.awt.Dimension(320, 240));
getContentPane().add(colorPanel);

He also switches layouts on the content pane just before this:

getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(),
javax.swing.BoxLayout.PAGE_AXIS));

I think he meant to post this link in this thread, not the other one:

<http://pastebin.com/nWHciPh0>

Bruce: see the value of an SSCCE? Now all is crystal clear.
 
J

John B. Matthews

markspace said:
He does it manually: he creates a JPanel, sets its background color and
then adds it to content pane:

colorPanel.setBackground(new java.awt.Color(255, 0, 255));
colorPanel.setPreferredSize(new java.awt.Dimension(320, 240));
getContentPane().add(colorPanel);

He also switches layouts on the content pane just before this:

getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(),
javax.swing.BoxLayout.PAGE_AXIS));

I think he meant to post this link in this thread, not the other one:

<http://pastebin.com/nWHciPh0>

Bruce: see the value of an SSCCE? Now all is crystal clear.

Right on all counts; in the other thread, the example showed how to let
nested components determine the frame's size.

In this context "manually" means editing settings in the designer; the
code in the fold is unaltered. Most people just use the Free Design
(GroupLayout), but there's a nice editor for customizing GridBagLayout
constraints.
 
J

John B. Matthews

Knute Johnson said:
John:

What is initComponents()? Is that something that NetBeans makes creates
to initialize the frame? I'm curious how making a setBackground() call
in that method is directed to the content pane.

Just to clarify, in <http://pastebin.com/nWHciPh0>, initComponents() is
generated from settings in the GUI designer. The generated code is
delimited by "editor-fold" tags, similarly used to collapse comments,
etc. If you change a designer property like background color, you can
see the corresponding change in the code. There are a number of pre- and
post-code generator events that let you effect changes to the generated
code. The complementary designer metadata, Main.form, is here:
<http://pastebin.com/8aQBkmaz>.
 
K

Knute Johnson

Just to clarify, in<http://pastebin.com/nWHciPh0>, initComponents() is
generated from settings in the GUI designer. The generated code is
delimited by "editor-fold" tags, similarly used to collapse comments,
etc. If you change a designer property like background color, you can
see the corresponding change in the code. There are a number of pre- and
post-code generator events that let you effect changes to the generated
code. The complementary designer metadata, Main.form, is here:
<http://pastebin.com/8aQBkmaz>.

I guess I'm going to have to play with NB some or I'm going to get left
behind :).

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top