java - Unable to access jlabel across the program -


i new java, hence basic question.

i having trouble in accessing field (jlabel in case) throughout program. code follows :

import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*;  public class accessvariable {      public static void main(string[] args) {          //basic setup of display frame         jframe frame=new jframe();         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setsize(400, 400);         jpanel panel=new jpanel();          //text field take user input         jtextfield txt= new jtextfield(10);          //adding action listener text field         txt.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 if (true) { //if condition not checking here, make enter loop                     jlabel j =new jlabel("access successful"); //a jlabel created inside                      system.out.println("inside j : "+j.gettext()); //statement check whether jlabel accessible here                 }             }         });          system.out.println("outside j : "+j.gettext()); //statement check whether jlabel accessible here          //basic setup final display         panel.add(txt);         frame.getcontentpane().add(panel);         frame.setvisible(true);     } } 

the error on line:

system.out.println("outside j : "+j.gettext()); 

if comment out line, works fine. inside j displayed correctly. if don't comment it, getting error:

exception in thread "exception in thread "main" java.lang.error: unresolved compilation problem: j cannot resolved" 

to correct this, have made j instance variable follows:

private jlabel j;  

however above generating new error:

cannot make static reference non-static field j 

i understand problem single line:

system.out.println("outside j : "+j.gettext()); 

how can fix above problem if works correctly, output when enter text in text field should follows:

inside j : access successful outside j : access successful 

wait. stop. start over.

your program nothing more static main method, fine "hello world" type programs, if desiring create more powerful, want to, no need to learn object-oriented concepts , how apply them in java programs. instance, start project should create @ least 1 class has non-static fields , methods, including field jlabel (by allow non-static methods of class access field), , should build gui, meaning add components containers, outside of main method, , more in non-static init() method or constructor. main method should creating objects , setting them action , little else. again importantly, learn oop concepts , how relate java programming language. decent book bruce eckel's "thinking in java".

another concept hit hard scope including variable scope. if have variable, here jlabel variable, j, buried , declared not inside of method, in method buried inside of anonymous inner actionlistener class, impossible other class code interact variable. if instance field, visible throughout non-static parts of class holds it.

for example:

import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*;  @suppresswarnings("serial") public class accessvariable extends jpanel {     private static final int pref_w = 400;     private static final int pref_h = pref_w;     private jlabel mylabel = new jlabel();     private jtextfield textfield = new jtextfield(10);      public accessvariable() {         add(textfield);         add(mylabel);          textfield.addactionlistener(new mylistener());     }      @override     public dimension getpreferredsize() {         if (ispreferredsizeset()) {             return super.getpreferredsize();         }         return new dimension(pref_w, pref_h);     }      private class mylistener implements actionlistener {         @override         public void actionperformed(actionevent e) {             string text = textfield.gettext();             mylabel.settext("text is: " + text);              textfield.requestfocusinwindow();             textfield.selectall();         }     }      private static void createandshowgui() {         accessvariable mainpanel = new accessvariable();          jframe frame = new jframe("access variable");         frame.setdefaultcloseoperation(jframe.dispose_on_close);         frame.getcontentpane().add(mainpanel);         frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 createandshowgui();             }         });     } } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -