File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrames and JPanel

 
Chad McAtee
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Rob Camick
Ranch Hand
Posts: 2473
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Digen Mahara
Greenhorn
Posts: 23
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic