NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
zamani
Joined: 27 Feb 2009 Posts: 22
|
Posted: Fri Mar 13, 2009 1:29 am Post subject: Non static variable cannot be referred to static method in NB6.5 GUI development.. help pls |
|
|
Hello,
I'm very new to Java.. at least tried for few days to solve the issue before post here.
I'm using NB 6.5 for GUI development. I'm using a custom library that is using an infinite loop to get a String out as the output. The string is needed to be displayed to the JText Field. I put the library in the main section and i understand that main is a static function and JTextField is a non static. Yes, it will not work.
this is the code
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RackApp().setVisible(true);
}
});
new start();
MReader_yit y=new MReader_yit();
Thread t=new Thread(y);
t.start();
try{
Socket s=new Socket("localhost",9999);
DataInputStream dis=new DataInputStream(s.getInputStream());
//infinte loop start work like waiting the input from keyboard
while(true){
ID=dis.readUTF();
IDENTITY=dis.readUTF();
Date=dis.readUTF();
Time=dis.readUTF();
//jTextField4.setText(IDENTITY); //this is the string and cause issue System.out.println(ID);
System.out.println(IDENTITY);
//do the proccessing
}
}
catch(Exception e){e.printStackTrace();}
}
Tried to run the library by using a button but the button look hang because it is an infinite loop. Due to this, the GUI is not responsive to any input causing it to hang.
Tried to use timer to start it also, received the same result.
Tried to pass the string out from the main function to the other method, still not successful because of the static and non static condition.
How can i solve this problem?
1) how to get the string out from the main? and how to really resolve the static and non static issue overhere.
2) Or how can i create the infinite loop to run at the background as separate process and can send me the string without causing anything .
hope that any expert in the forum could spend some time on this.
thanks very much
z |
|
| Back to top |
|
 |
MatEngel
Joined: 03 Mar 2009 Posts: 5
|
Posted: Fri Mar 13, 2009 2:34 pm Post subject: |
|
|
That's a basic question to the Java language, not really to Java EE users.
You do not show where you defined jTextField4; probably you have a declaration
| Code: | | private TextField jTextField4; |
Just set a modifier "static" there, and you have avoided the compiler message. The line becomes
| Code: | | private static TextField jTextField4; |
Maybe, your program will work then, but possibly you will have other problems that I do not see now.
Another way that I prefer is that you instantiate your class in the main () method, and do all work in the constructor. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|