user interface - setting text to bold using java -
i creating gui jcheckbuttons (bold italic), these buttons turn text in text field bold, italic or both when selected through use of action listeners. here how implement functionality in action listener methods (this bold method, have similar methods italic , bold , italic).
class bold implements actionlistener { private final fontsetter fontsetter; private final jtextfield textfield; bold(fontsetter fontsetter, jtextfield textfield) { this.fontsetter = fontsetter; this.textfield = textfield; } font font = new font(textfield.gettext(), font.bold,12); public void actionperformed(actionevent e) { fontsetter.setbold(); textfield.setfont(font); } }
error: blank final field textfield may not have been initialised,how can fix this?
you have initialize textfield
variable first before use it. since have defined font
instance variable, reference textfield
may not have been initialized yet.
one possible approach initialize font
on construction since dependent on textfield
:
private final jtextfield fontsetter; private final jtextfield textfield; private final font font; bold(jtextfield fontsetter, jtextfield textfield) { this.fontsetter = fontsetter; this.textfield = textfield; this.font = new font(textfield.gettext(), font.bold, 12); }
Comments
Post a Comment