Reputation: 175
I have created a class which extends JFrame, which by default will have a JPanel created with it. I have tried this:
public class Main extends JFrame {
JPanel pane;
private static final long serialVersionUID = 1L;
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int WIDTH = 500, HEIGHT = 500;
setBackground(Color.LIGHT_GRAY);
setSize(WIDTH, HEIGHT);
setTitle("Window");
setLocationRelativeTo(null);
pane = getContentPane();
}
I'm using Eclipse, and it's giving an error, suggesting that I cast getContentPane()
to return a JPanel
, but to my understanding, this is what it should be returning already. I inserted System.out.println(getContentPane());
and it said in the console that it was returning a JPanel
, so I'm a little confused as to why it needs to be cast. Is there something I'm missing here? Thanks.
Upvotes: 1
Views: 226
Reputation: 17454
it's giving an error, suggesting that I cast getContentPane()to return a JPanel, but to my understanding, this is what it should be returning already
You get the error because you called:
getContentPane();
from a JFrame where the return type is a Container
, and you are trying to assign a Container
into a JPanel
:
JPanel pane;
pane = getContentPane();
If you do not get the idea why the above is not allowed, think about this.
Since all Dogs are Animal, so in terms of code, you can always do this:
Animal animal = new Dog(); //Assign dog as Animal (O.K)
However if I have an Animal. Can I say it is definitely a Dog?
Dog dog= new Animal(); //Assign animal as dog (NOT O.K)
The answer is no, unless you specifically tell me "trust me, this animal is a Dog. Let me assign this Dog object to a Dog variable.".
This idea is the same as casting in programming:
Animal animal = new Animal();
Dog dog = (Dog)animal; //Tell Java this animal is indeed a Dog (O.K)
Now, your scenario of using the JPanel and Container is the same.
pane = getContentPane(); //Assign Container into JPanel (NOT O.K)
pane = (JPanel)getContentPane(); //Assign Container (casted as JPanel into JPanel) (O.K)
Ah ok. It's weird though cause I've seen in different code snippets around the web examples where it works without being cast. Check this out for example
The codes from your given link works because, the code only uses a .add()
method from getContentPane()
.
Note: getContentPane()
returns a Container
object.
If you look at the documentation of Container, .add()
is a method of Container
class.
Since it is invoking a method which belongs to Container
class. It will definitely work and no casting is needed.
Casting is needed unless you invoke a method from a subclass of the Container (such as a JPanel). For example,
getContentPane.add(whatever); //Casting NOT needed, add() is a method of Container
((JPanel)getContentPane()).getUI(); //Casting needed, getUI() is a method of JPanel
Upvotes: 3
Reputation: 347314
If you read the JavaDocs for JFrame#getContentPane
you will see it returns an instance of Container
, before you can assign it to a JPanel
variable, you must cast it
In many cases you really should test to see if the return result is actually an instance (or is assignable to) a JPanel
using instanceof
in an if
statement
Upvotes: 4
Reputation: 1091
Container
is a parent in JPanel
's inheritance hierarchy
getContentPane()
returns a Container
, which in this case, is a JPanel
object. Therefore, a cast is required to cast the Container
to its more specific child type, as shown below:
pane = (JPanel) getContentPane();
Upvotes: 6