OMG it suddenly makes so much sense, since i was trying to use ActionListener class, which i'm pretty sure is not a safe swing utility...
Printable View
OMG it suddenly makes so much sense, since i was trying to use ActionListener class, which i'm pretty sure is not a safe swing utility...
it works indeed, the window shows up.
Small problem tho - i tried making a conditioner into the listener class, the one implementing actionlistener
this is a copy of my actionlistener class
Code:public class listener implements ActionListener{
public boolean switch_1=false;
public String content="bad statement";
public void set_switch(){
switch_1=true;
System.out.println("ALL CLEAR-set switch");
System.out.println(switch_1);
}
public void set_switch_2(){
switch_1=false;
}
public void actionPerformed(ActionEvent e){
//constructors
file objectfile=new file();
file fob=new file();
//brain
if(e.getSource()==usertext&&switch_1==false){
String usertextget=String.format(""+e.getActionCommand());
objectfile.getAnswer(usertextget);
talkativetext.setText(objectfile.returnAnswer());
}
if(e.getSource()==usertext&&switch_1==true){
//call method in file
System.out.print("ALL CLEAR-listen");
content=String.format("",e.getActionCommand());
fob.Learner(content);
}
}
}
the bad thing is that &&switch_1==false condition doesen't seem to be respected. Is there any special behaviour attributed to this kind of class or what?
It's considered bad programming style to compare a boolean variable with true or false. You just need to use the variable for a test for true or append the NOT operator (!) for a test for false. Also use white space, it makes it so much easier to read ie
Code:if ( e.getSource() == usertext && !switch_1 ) { ... }
It's more likely that usertext isn't holding a reference to the component that generated the event and so the first condition is failing.Quote:
the bad thing is that &&switch_1==false condition doesen't seem to be respected
wouldn't that mean nothing should happen?Quote:
It's more likely that usertext isn't holding a reference to the component that generated the event and so the first condition is failing.
Something happens but it's always the case ==false;
I thought maybe i should add 2 distinct fields and swap them as the program learns. Is there a way of add()-ing to a certain x,y location?
remade:
Code:if(e.getSource()==usertext){
if(!switch_1){
String usertextget=String.format(""+e.getActionCommand());
objectfile.getAnswer(usertextget);
talkativetext.setText(objectfile.returnAnswer());}
else if(switch_1){
//call method in file
content=String.format("",e.getActionCommand());
fob.Learner(content);
}}
OK, if one of the if statements is executing then usertext is holding valid reference.
You may have a threading issue. How are you setting the state of switch_1? If it's from a thread other than the event dispatch thread then you need to declare switch_1 as volatile ie:BTW why is switch_1 public - it's bad practice to have public instance variables except in very rare circumstances. Always start by declaring variables as private and only widen their scope if you know there is a legitimate reason to do so.Code:public volatile boolean switch_1=false;
Also you don't need to do:
If the first condition fails then the second has to pass so you can doCode:if(!switch_1){
...
}
else if(switch_1){
...
}
Code:if(!switch_1){
...
}
else {
...
}
i'm not quite familiar with event dispatcher or what it means. I do know tho that i declare everything, all the classes and related variables in: public class listener implements ActionListener
Just add the volatile keyword as I showed earlier and see if that solves it.
The event dispatch thread is the thread that all Swing related stuff should be performed on. If you don't know what it is then read a Swing tutorial because you shouldn't be using Swing unless you understand what this does.Quote:
i'm not quite familiar with event dispatcher or what it means
not working... Still the same result. Am i supposed to write my code in InvokeLater(new Runnabe(()?
Have you tried adding print statements just before the if statement to find out what the values of the variables actually are and whether or not the code is even being called.
got it. switch_1 was non-static
clockwork:D lovely:)