I have a Lanterna terminal app.
With threads I would like to fill up a window with different Panels and read key input accordingly.
Using:
thread1 = new ModuleThreads(screen,"Locations","addRightPanel");
thread2 = new ModuleThreads(screen,"Commands","addCenterPanel");
thread1.start();
thread2.start();
The first thread shows in the rightPanel the "Locations" menu. the menu contains: "press a for exit." The thread works. The menu appears, and pressing a exits the program.
Now the second Thread doesn't appear nor listens to the matching key pressed:"press b to exit." Also I used reflection to be able to change menu contents easily.(Its a personal project anyway...:)) The problem is that only the first thread1 is shown in the ui. The reflection just works. On itself thread2 works just fine. But just as a single thread.
public class ModuleThreads extends Thread{
Screen screen;
String myclass,panel;
Class noparams[]={};
Class moduleClass,panelClass;
Class[] paramScreen;
Class[] myPanelClass;
Constructor constructor,moduleConstructor;
Object moduleObject,panelObject;
Method panelGetView,panelMethod,moduleMethod;
Panel mypanel;
private Thread thread1;
public ModuleThreads(Screen screen,String myclass,String panel){
this.screen=screen;
this.myclass=myclass;
this.panel=panel;
//class aanmaken van Screen.class
paramScreen = new Class[1];
paramScreen[0] = Screen.class;
myPanelClass = new Class[1];
myPanelClass[0] = Panel.class;
thread1 = new Thread(this);
thread1.start();
try{
moduleClass = Class.forName("app.modules."+this.myclass);
//package name er voor plakken.
moduleObject = moduleClass.newInstance();
moduleMethod =
moduleClass.getDeclaredMethod("readInput",paramScreen);
moduleConstructor = moduleClass.getConstructor();
panelClass = Class.forName("app.view.MainView");
constructor = panelClass.getConstructor(Screen.class);
panelObject= constructor.newInstance(this.screen);
panelGetView = moduleClass.getDeclaredMethod("getView",noparams);
panelMethod =
panelClass.getDeclaredMethod(this.panel,myPanelClass);
mypanel = (Panel) panelGetView.invoke(moduleObject,null);
panelMethod.invoke(panelObject, mypanel);
moduleMethod.invoke(moduleObject,this.screen);
}catch(Exception e){
e.printStackTrace();
}
}
public void start(){
}
@Override
public void run(){
try{
mypanel = (Panel) panelGetView.invoke(moduleObject,null);
panelMethod.invoke(panelObject, mypanel);
moduleMethod.invoke(moduleObject,this.screen);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
What am I doing wrong? Why does only thread 1 or thread 2 appear, and not both, with both key listeners?