java - trouble in overriding JFrame.setBackground() -
i have class called mainui extends jframe, has code :
//constructor public mainui(){ // components/panels defined , initialized here. setbackground(color.decode("#eff4e4")); } @override public void setbackground(color colorbg){ //a method set same background color components ave getcontentpane().setbackground(colorbg); decisionpanel.setbackground(colorbg); adbradio.setbackground(colorbg); fastbootradio.setbackground(colorbg); commandradio.setbackground(colorbg); pushpanel.setbackground(colorbg); uninstallpanel.setbackground(colorbg); pcpanel.setbackground(colorbg); phonepanel.setbackground(colorbg); }
however, when compile, gives nullpointerexception @ line [ decisionpanel.setbackground(colorbg); ]
i tried not overriding setbackground method , renamed , code worked fine, don't know why overriding setbackground method causes problem?
i'm sure panels/components initialized before call method, it's obvious since code did work ater i've renamed method.
this snip of code jframe class doing unrecommended call of overridable method constructor happens overridden version executed before class has been created "and before fields initialized , no way initialize fields before super's constructor finishes work" have few options either avoid referring subclass fields in overridden method/s or did making new method stuff want
public jframe(string title, graphicsconfiguration gc) { super(title, gc); frameinit(); } /** called constructors init <code>jframe</code> properly. */ protected void frameinit() { enableevents(awtevent.key_event_mask | awtevent.window_event_mask); setlocale( jcomponent.getdefaultlocale() ); setrootpane(createrootpane()); setbackground(uimanager.getcolor("control")); setrootpanecheckingenabled(true); if (jframe.isdefaultlookandfeeldecorated()) { boolean supportswindowdecorations = uimanager.getlookandfeel().getsupportswindowdecorations(); if (supportswindowdecorations) { setundecorated(true); getrootpane().setwindowdecorationstyle(jrootpane.frame); } } sun.awt.suntoolkit.checkandsetpolicy(this); }
or can override frameinit()
@override protected void frameinit() { //initialize fields here super.frameinit(); }
Comments
Post a Comment