File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JFrames and JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrames and JPanel" Watch "JFrames and JPanel" New topic
Author

JFrames and JPanel

Chad McAtee
Greenhorn

Joined: Feb 23, 2011
Posts: 4
When I add a Jpanel to my JFrame I lose the background color of the JFrame. Also every article I have read never mentioned having to make a Component object to getContantPane() before being able to change the background color of a JFrame. If anyone could shead some light on the logic behind this please explain. I am really new to programming so take it easy on me

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Welcome to the Ranch

The paintComponent() method should have protected access.
You are colouring the contentPane, which is part of the Frame, and adding the panel on top of it. So you will see the colour of the panel, not the black of the content pane.

I shall move this discussion to our GUIs forum, where we usually discuss such questions.
Digen Mahara
Greenhorn

Joined: Dec 23, 2011
Posts: 23

Chad McAtee wrote:When I add a Jpanel to my JFrame I lose the background color of the JFrame. Also every article I have read never mentioned having to make a Component object to getContantPane() before being able to change the background color of a JFrame. If anyone could shead some light on the logic behind this please explain. I am really new to programming so take it easy on me

[code=java]public static void main(String[] args) {

drawPracticeField addField = new drawPracticeField();


JFrame backGroundPane = new JFrame();

backGroundPane.setSize(760, 760);
backGroundPane.setUndecorated(true);
backGroundPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set background Color to black
Container con = backGroundPane.getContentPane();


con.setBackground(Color.black);
backGroundPane.setVisible(true);

backGroundPane.add(addField);



public class drawPracticeField extends JPanel{

public drawPracticeField() {

/*
Create Window 760 to 760 size
*/

JPanel newField = new JPanel();

}


/*
Draw outside boarders
Draw hash marks
Draw 10 yard lines
Add Line numbers Line of scrimage to 40 yard line
*/



public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(50,40,600,600);
Color greenField = new Color (45, 96, 32);
g.setColor(greenField);
g.fillRect(50,40,600,600);



}



Ok to be more presize

use the setPrefferedSize(width,height) method of JPanel to set the Size of the JPanel

Note : set its size inside the class which extends JFrame i.e where you want to add the panel

like this you can even see the Background of the JFrame




To every Tunnel there is always a way out
So keep Trying...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
The "content pane" is just a JPanel. That is why you can add any component to it the way you add components to a JPanel you create.

The layout manager used by the content pane is a BorderLayout. So when you add a component to the content pane without specifying a constraint the panel goes to the CENTER and is automatically resized to fill all the space available to the content pane. Since a JPanel is opaque by default you see the default color of your panel and not the background of the content pane panel.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Digen Mahara wrote:either you fix the size of the JPanel in the JFrame or instead color the JPanel itself

As meaningless as some of your other posts. And please stop inserting empty code tags all over your responses.


luck, db
There are no new questions, but there may be new answers.
Digen Mahara
Greenhorn

Joined: Dec 23, 2011
Posts: 23

Darryl Burke wrote:
Digen Mahara wrote:either you fix the size of the JPanel in the JFrame or instead color the JPanel itself

As meaningless as some of your other posts. And please stop inserting empty code tags all over your responses.


Okay I admit, but I'm new to code ranch and still learing and searching its features.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i recall doing this before. if i remember right, i passed a reference to "this"(the JFrame) to the JPanel(which called it parent), then said
set background(parent.getBackground());
or something like that


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrames and JPanel
 
Similar Threads
Basic swing application; class extending JPanel won't update graphics
Layered JPanels Transparency Problem.
updating JPanel in a JFrame
How to Draw a Line in Java?