Skip to content
Advertisement

How to use getContentPane in independent class?

I want to set background color by key pressed event. This is my code (using independent class)

JavaScript
JavaScript

But I can’t use getContentPane with an error “Cannot make a static reference to the non-static method getContentPane() from the type JFrame” How can I solve this problem?

Advertisement

Answer

Your KeyCharEx.getContentPane() is trying to call a static method in the KeyCharEx class that does not exist. Well, it exists, but it’s non-static, therefore only accessible when used on an instance of the class. In this case, it might be best to pass the pane as an argument:

JavaScript

If getContentPane() is a public method, you could also pass your KeyCharEx over in a similar way and use keyCharEx.getContentPane() instead. You’d be calling the method on your KeyCharEx object, not the class itself.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement