I have a client/server MMORPG I am working on and I want to do things correctly(as impossible as it is). My questions is: What is the best way to pass references to my GUI items(JInternalFrame, JPanels, etc.) to my class that listens to commands/replies from the server. Right now this is what I have going on, which will soon grow quite large.
MyCommandReciever(DataInputStream commandIn, DefaultListModel modelUserList, JInternalFrame skillsFrame, Container skillsPanelHeader, Container skillsPanelContent, JInternalFrame characterFrame){
this.commandIn = commandIn;
this.modelUserList = modelUserList;
this.skillsFrame = skillsFrame;
this.skillsPanelHeader = skillsPanelHeader;
this.skillsPanelContent = skillsPanelContent;
this.characterFrame = characterFrame;
}
I don’t want to keep passing more and more JInternalFrames to MyCommandReciever.
Does it make sense to do something like this??
Component[] c = panel.getComponents();
for(int i = 0; i < c.length; i++) {
System.out.println(c[i].getClass().getSimpleName());
}
Or is there a way that I can create an instance of my Main class(which includes my GUI), from within my main class, and pass that to MyCommandReciever so it has access to everything(maybe like this):
MyCommandReciever(DataInputStream commandIn, DefaultListModel modelUserList, MyClient m){
If this last approach is the way to go(which I’m hoping it isn’t, do I need to pass it from the class that creates an instance of it(Character.java creates instance of MyClient.java), to itself(MyClient.java), and then pass it from MyClient.java to MyCommandReciever.java?
Sorry if that last question was too confusing… let me break it down:
Inside Character.java:
if (playSuccess.contains("true")){
z++; //to turn off the commandIn functionality in Character screen when MyClient is running
frame.dispose();
new MyClient(accountName, playWho, commandIn, messageIn, commandOut, messageOut, sM, sC);
Inside MyClient.java:
commandReciever = new MyCommandReciever(commandIn, modelUserList, skillsFrame, skillsPanelHeader, skillsPanelContent, characterFrame);
so in the character.java I would have to create instance of MyClient(and assign to variable) but at the same time pass that assigned variable to MyClient. (now that I’m typing this it doesn’t even seem possible).
Please guide me 😀 Clearly I wrapped my brain on this last idea.
This is the approach that I decided to go with. Thanks for the other advice however it seemed a bit complex for my needs:
In my main gui class:
In my command receiever class(class that talks to server):
Example of how I edit my main gui class’s components from my command receiver class: