i'created jlabel should display "texta" if variable count == -1,
"text b" if variable count == 0 , "textc" if variable count == 1.
i've used swing create interface, can see below
the red rectangle shows jlabel should be.
i have tried creating 3 jlabels , changing setvisible(boolean) whenever variable count value condition applies. didn't work because got following error:
exception in thread "main" java.lang.nullpointerexception @ tempconverterui.tempconverter.main(tempconverter.java:354) c:\users\x\appdata\local\netbeans\cache\8.1\executor-snippets\run.xml:53: java returned: 1
and jlabels not placed in same location in gui (overlapping not possible).
i've tried using jlabel.settext() change text displayed in jlabel, whenever variable condition applied. got similar error 1 above (if not same).
i've read other posts , researched further , found people suggested actionlisteners set unsure these work simple variable, opposed component in gui.
my code follows:
package tempconverterui; import javax.swing.joptionpane; import messageboxes.userdata; import com.sun.jna.library; import com.sun.jna.native; import com.sun.jna.wstring; public class tempconverter extends javax.swing.jframe { public interface somelib extends library { public int engstart(); public int endstop(); public int engcount(); public wstring enggetlasterror(); public int engsetattribute(wstring aszattributeid, wstring aszvalue); } /** * creates new form tempconverter */ public tempconverter() { initcomponents(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() {
the layout created here, followed temperature convertion methods , unrelated component's functionality (which believe not relevant in case)
/** * @param args command line arguments */ public static void main(string args[]) { /**this login form gets created*/ userdata.popup(); /**after library functions called, return variable count value*/ somelib lib = (somelib) native.loadlibrary("somelib", somelib.class); int startresult = lib.engstart(); system.out.println(startresult); if (startresult < 0) { system.out.println(lib.enggetlasterror()); } system.out.println(userdata.getacinput()); int setatresult = lib.engsetattribute(new wstring("code"), userdata.getacinput()); system.out.println(setatresult); if (setatresult < 0) { system.out.println(lib.enggetlasterror()); }
and next piece of code should control jlabel text display
int count = lib.engcount(); system.out.println(count); if (count == -1) { system.out.println(lib.enggetlasterror()); } else if (count == 0) { } else { } new tempconverter().setvisible(true); } // variables declaration - not modify private javax.swing.jpanel bottompanel; private javax.swing.jbutton convertbutton; private static javax.swing.jbutton button; private javax.swing.jtextfield from; private javax.swing.jcombobox<string> fromcombo; private javax.swing.jlabel fromlabel; private javax.swing.jlabel title; private javax.swing.jtextfield to; private javax.swing.jcombobox<string> tocombo; private javax.swing.jlabel tolabel; private javax.swing.jpanel toppanel; // end of variables declaration
}
any appreciated. if include simple code example well, fantastic new java (and programming, in general).
issues:
- don't set jlabel visible, rather add gui, leave visible default, , set text via
settext(...)
. - give class holds jlabel public methods allow outside classes ability set label's text.
public void setlabeltext(string text)
, , in method callsettext(text)
on jlabel. - debug nullpointerexception other npe -- @ stacktrace, find line throws it, , code see why key variable on line null.
- when , how change jlabel depend on event want listen for. if user input, want respond input, actionlistener added jbutton or jtextfield, or itemlistener added jradiobutton.
- if want instead listen change in state of variable, no matter how variable changed, make "bound property" (tutorial) using propertychangesupport , propertychangelistener.
for example of latter:
import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import javax.swing.*; import javax.swing.event.swingpropertychangesupport; @suppresswarnings("serial") public class showcount extends jpanel { private static final int timer_delay = 1000; private jlabel countlabel = new jlabel(" "); private countmodel model = new countmodel(); public showcount() { model.addpropertychangelistener(countmodel.count, new modellistener(this)); setpreferredsize(new dimension(250, 50)); add(new jlabel("count:")); add(countlabel); timer timer = new timer(timer_delay, new timerlistener(model)); timer.start(); } public void setcountlabeltext(string text) { countlabel.settext(text); } public static void main(string[] args) { swingutilities.invokelater(() -> createandshowgui()); } private static void createandshowgui() { showcount mainpanel = new showcount(); jframe frame = new jframe("showcount"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } }
class countmodel { public static final string count = "count"; // count "property" // support object notify listeners of change private swingpropertychangesupport support = new swingpropertychangesupport(this); private int count = 0; public int getcount() { return count; } public void setcount(int count) { int oldvalue = this.count; int newvalue = count; this.count = count; // notify listeners count has changed support.firepropertychange(count, oldvalue, newvalue); } // 2 methods allow listeners register support object public void addpropertychangelistener(propertychangelistener listener) { support.addpropertychangelistener(listener); } public void addpropertychangelistener(string propertyname, propertychangelistener listener) { support.addpropertychangelistener(propertyname, listener); } }
class modellistener implements propertychangelistener { private showcount showcount; public modellistener(showcount showcount) { super(); this.showcount = showcount; } @override public void propertychange(propertychangeevent evt) { int newvalue = (int) evt.getnewvalue(); showcount.setcountlabeltext(string.format("%03d", newvalue)); } }
class timerlistener implements actionlistener { private countmodel model; public timerlistener(countmodel model) { super(); this.model = model; } @override public void actionperformed(actionevent e) { int oldcount = model.getcount(); int newcount = oldcount + 1; model.setcount(newcount); } }
Comments
Post a Comment